-
Notifications
You must be signed in to change notification settings - Fork 361
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
Empty vector #660
Conversation
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>{}); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
At this point, I think this is a strict improvement, though making |
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 thedefault_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.