boost-program-options: notifier for options with no value

前端 未结 3 444
终归单人心
终归单人心 2020-12-06 04:17

One can use notifier for parsed options only if they have value_semantic. What is the best way for no-value options to be automatically handled by the given notifier?

3条回答
  •  温柔的废话
    2020-12-06 04:38

    Use the zero_tokens modifier. It seems you also need to use implicit_value, but anything provided after the option name won't be consumed by the option parser. Instead, when the option is noticed on the command line, the implicit value will be assigned to the option, triggering the option's notifier (so make sure to provide a notifier function). Apparently, it's also important for the option's value type to be string. I'm not clear on why.

    void got_foo(std::string const&);
    
    desc.add_options()
      ("foo", 
       po::value()
         ->implicit_value("")
         ->zero_tokens()
         ->notifier(&got_foo),
       "foo description")
    ;
    

提交回复
热议问题