cxxopts
cxxopts copied to clipboard
Negating booleans with short options
With the following:
options.add_options()
("p,profiling" , "enable/disable profiling events" , cxxopts::value<bool>()->default_value("true"))
;
It isn't clear to me how to disable this option on the command line using the short form.
-o false and -o 0 don't seem to do anything. --profiling=false does work.
Am I missing something? Is there a way to negate a short option?
You've written -o false
. Did you mean -p false
? I'm not sure if that works. I'll have a look and get back to you.
Whoops, my bad. It should be -p false
.
I think -p false
or -p 0
would be nice to support.
One other thought: many GNU tools are fond of providing boolean long options like --profiling
and -no-profiling
or --enable-profiling
and --disable-profiling
. These don't take values but assume true/false values, with the last in command-line order taking precedence. Since the order of non-positional arguments seems to be hidden but cxxopts, I think it isn't possible to support this currently. Do you agree?
That wouldn't be possible at the moment.
Treating 0
as false wouldn't be difficult, I can do that.
Actually multiple arguments might work, just not with --no-*
or --disable-*
at the moment. But since the arguments are parsed in order, I think the last one will be the value used, but the count of the argument will be greater than zero.
I would have to check if that works because I can't remember off the top of my head.
The last value of an option is the one used, so an option can be provided multiple times the same way as other option parsers handle.
It is difficult to support negating boolean short options, only because a boolean option is essentially an option with an implicit value of true
. This means that there is no way to pass an argument after a boolean option.
For example, if -f
were an option for a file, then -f filename
works, because -f
has no meaningful implicit value, but -a
by itself automatically means true
, and -a true
is parsed as -a
and then a positional parameter true
. The problem is only that there is no syntax. So if -a
is for --apple
, then --apple=true
works, but --apple true
doesn't for the same reason.