-
Notifications
You must be signed in to change notification settings - Fork 242
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
Support boolean argument with the form like '--enable-hack=true/false' #313
Comments
亲,来信已经收到!祝你快乐!
|
In the current library, this can be done by writing a custom action for each type of "true" "false" string that you want handled. int main(int argc, char *argv[])
{
argparse::ArgumentParser program("program_name");
program.set_assign_chars("=:");
program.add_argument("--enable-hack")
.default_value(false)
.action([](const auto& str) {
return (str == "true");
});
try {
program.parse_args(argc, argv);
} catch (const std::runtime_error &err) {
std::cerr << "argparse failed with:" << err.what() << "\n";
std::cerr << program;
return 1;
}
std::cout << "Hack enabled: " << std::boolalpha << program.get<bool>("--enable-hack") << "\n";
return 0;
} foo:bar $ ./main
Hack enabled: false foo:bar $ ./main --enable-hack=true
Hack enabled: true |
Just update to the latest version 3.0, find that this behavior may not be the desired one: For a piece of code like this:
And for general boolean argument like this:
It would be better to have a native boolean argument to handle the flexible switch via let me see, is it possible to have a boolean argument defined like this:
and support the following equivalent arguments:
and the opposite, like:
|
This explicit switch can be useful when worked with config file.
Settings from argument generally take more priority over config file.
When it's not given explicitly via argument, the config file takes over.
For boolean argument, provide the argument means switch true/false, but just 1 state.
If the argument default is false, and setting in config file is true, Once I want to turn off the switch,
I have to change the config file.
For example, a case like this:
--enable-hack
set totrue
orfalse
in config file.--enable-hack
with default set to 'false' is givenIf I want to enable the hack, I can pass arg
--enable-hack
, then the setting in config file will be ignored.But once I want to disable the hack temporary, I have to change the config file as no argument provided
means using config file. Or I can add another arg
--disable-hack
, and then pass the argument to explicitlydisable the hack, which does not seem to be good.
It would be better if a boolean argument can also accept the form of
--enable-hack=true/false
The text was updated successfully, but these errors were encountered: