Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty vector #660

Merged
merged 9 commits into from
Feb 9, 2022
Merged

Empty vector #660

merged 9 commits into from
Feb 9, 2022

Conversation

phlptp
Copy link
Collaborator

@phlptp phlptp commented Oct 24, 2021

this is a follow-on to #649.

What I did was fix a few of the bugs in the parsing with flag like variables.
add a check in the processing of vector outputs, that catches a
"{}" as the empty vector and returns it.

So in a config file "", [], {} will trigger the default value for flag like options( expected_min==0) so to get an empty vector as an option for a given option. the default value must be an empty vector, or the default_str set to "{}"

There are also some checks to prevent an empty vector from getting generated on an output with a minimum expected greater than 0.

this needs more testing to make sure it works from the command line as well, and I think there are a few edge cases that need to be dealt with yet, but @puchneiner is correct in that there does need to be a mechanism for generating empty vectors as results. I don't think it needs to be easy, as if it was too easy it could cause seg faults if people are not expecting an empty vector to be allowed. Basically if you are adding an option and do not explicitly specify a min expected as 0 and specify a default value as empty, there should be no way to generate an empty vector as an output.

@phlptp phlptp marked this pull request as ready for review October 25, 2021 14:45
std::vector<std::string> two, three;
std::vector<std::string> nzero, zero1, zero2, one, two, three;
app.add_option("--zero1", zero1)->required()->expected(0, 99)->default_str("{}");
app.add_option("--zero2", zero2)->required()->expected(0, 99)->default_val(std::vector<std::string>{});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if you add ->expected(0, 99) but don't set default_str? Can whatever happens for that be tested?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a test for that. But that is basically the previous behavior and will result in a vector with a single empty string.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea here is that there does need to be a way to get an empty vector as an output, but it shouldn't be default or easy.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like adding an empty string from a TOML [] is also problematic. Would is be possible to make this an error if a default is not specified? [""] would be a valid default to get the old behavior?

@henryiii
Copy link
Collaborator

henryiii commented Feb 9, 2022

At this point, I think this is a strict improvement, though making []/{} an error rather than equivalent to [""] seems like a valid improvement eventually. Just need a rebase to fix the README conflict.

@henryiii henryiii merged commit e29cb3f into CLIUtils:main Feb 9, 2022
@henryiii henryiii deleted the empty_vector branch February 9, 2022 18:12
@github-actions github-actions bot added the needs changelog Hasn't been added to the changelog yet label Feb 9, 2022
@henryiii henryiii removed the needs changelog Hasn't been added to the changelog yet label Mar 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants