-
Notifications
You must be signed in to change notification settings - Fork 362
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
To string and default option revamp #242
Conversation
952bee9
to
d5c8a5f
Compare
Codecov Report
@@ Coverage Diff @@
## master #242 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 12 12
Lines 2771 2781 +10
=====================================
+ Hits 2771 2781 +10
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@
## master #242 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 12 12
Lines 2212 2206 -6
=====================================
- Hits 2212 2206 -6
Continue to review full report at Codecov.
|
d5c8a5f
to
29c5297
Compare
I agree the defaulted flag is not the most intuitive of arguments. If I understand it correctly turning that flag to true when defining an option specifies that whatever value I have currently in the value I loaded should be used as the default value and when the App is executed if that particular option is not used the default_val is loaded into the variable? So use cases There may be times I would want a option/flag callback to be called regardless of whether the flag is specified or not. A way of making sure that happens with a default value would be nice, as well as turning that on or off. If I am using CLI11 as a text program processor, then I might want variables reset to a default value each time, so CLI11 should replace the current value with the default if no option was specified. So I think if I were redesigning it I would want a defaulted() toggle to change the behavior of replacement or callback execution with no arguments. And I would want a default(T val) function to specify the default later and change it if necessary. Along with appropriate getter functions. With the default function, I would like it to be a template so it can just convert whatever type I give it whatever the internal representation in CLI11 is. In this case I would think if it can't convert a compile error is probably good, and silently doing a conversion is probably bad. Taking the storage value and using it as the default seems to me it might a little too tricky, and then using that in some sort of generator function, I just have a little trouble seeing the use case. |
The "default value" is only a help printout. CLI11 never changes the value of the variable if it is not passed (for options, at least) - callbacks do not even run if the option is empty. Since the variable is just a plain variable, the programmer is responsible for initializing it if is to be used later. The only change for adding "true" (currently) or int value = 2;
// If value is uninitialized, this is still valid
app.add_option("--opt", value, "A value");
// Help is:
// --opt=INT A value
// If value is uninitialized, this is not valid (there is no good reason to do this)
app.add_option("--opt", value, "A value", false);
// Help is:
// --opt INT A value
// The default value is added to the help
app.add_option("--opt", value, "A value", true);
// Help is:
// --opt INT=2 A value In all cases, if the option is not passed, nothing is touched. And you can always set your own default string with Adding a callback that would run to reset things to defaults is beyond the scope of this PR. I'm not sure CLI11 should ever meddle with defaults, since a programmer can just as easily and more transparently set the values back to defaults (they are just plain types after all). In that case, one might want the delayed default checking. |
I like shorter names for something (at least I) use often, but |
Ok, I haven't messed too much with the default stuff in our use cases, so I can't say I fully understood what was going on. I guess I can see how the capture by reference thing would work now. |
759266d
to
bcdbd16
Compare
bcdbd16
to
6e7535f
Compare
bb6b63f
to
64fae40
Compare
Even at the somewhat lengthy name of Good change though, however it gets worked out! |
Using to_string instead Switching to new backend Moving to capture function for defaults Rename capture_default + _str defaultval -> default_str, added always_capture_default Fix style
64fae40
to
e15ef57
Compare
89b0c63
to
dbbd793
Compare
@djv, I don't think |
This adds
to_string
, which automatically selects the best way to convert a type to a string, and returns an empty string if the type is unconvertable. This removes the two overloads fordefaulted
, and has the side effect that now capturing a default can be done later through the option, the way all other option settings (except name and variable) work. This was also implemented in->capture_default_str()
. Here's the before and after:The bool in the add_option constructor be deprecated (no warning in 1.8, warning in 1.9, removed in 2.0)? It was a workaround for missing technology. The description string stays (even though it can also be set later) since adding a description should be done 95% of the time, and it's clear what it is from looking at it. A random
true
at the end is not clear at all.Remaining questions:
capture_default_str()
the best name?default
is a C++ keyword, so that's out. It's a bit long for something that used to be four chars (true
).Todo:
TestRemove the delayed behavior of the default capturealways_default_capture()
settingNote there is one minor drawback. If a type supports streaming into a stream and has a declaration, but does not have the streaming definition available in the included header (like in Boost::Optional), you will need to add the streaming header to add options. Since adding the streaming enhances CLI11, I think this is an acceptable drawback. Unstreamable types are supported (and tested now).
This closes #241.
Features:
->capture_default_str()
, which stores the current value of the attached variable and will show it in the help string.->always_capture_default()
(INHERITABLE), which will cause the default to always be captured. Really only useful on the template option. Includesget_ always_capture_default()
.always_capture_default
or the "true" option captures the default string at the point of definition, rather than later.default_function
has been added, and is settable.get_defaultval
renamed toget_default_str