Search code examples
c++linuxcommand-line-arguments

How to use boolean flag with cxxopts and command line?


I defined:

options.add_options()
  ("m, monitor", "Monitor current state", cxxopts::value<bool>()->default_value("true"));

How do I use flag m in command line to run test executable on Linux?

This:

./test -m "false"
./test -m false

doesn't work.


My case has true as default value for m and I want to run test with m having false value. Is it possible?


Solution

  • This behavior is described in cxxopts docs. Proper way is

    ./test --monitor=false
    

    Note, that this syntax is valid only for long options, so there is no way to use it with a short option. Boolean option argument cannot be separated by a space, because the option needs to be used without an argument to specify true value for a flag, making its argument indistinguishable from positional argument.