Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

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

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?

The simple approach is to make a dummy value_semantic with implicit assignment, so a user can pass the option without a value. This leads to a possibility of explicitly provided values. One can add a run-time check if the value was provided and throw an error.

Update: BUT, this doesn't work in presence of positional options, because a positional option's value can follow no-value option raising an exception as s given value to it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

One guy OXPEHOMETP on a russian programmers forum gave me a pice of advice to use boost::program_options::bool_switch().

When defining an option with no value via value-permitted interface, one must pass not boost::program_options::typed_value() as semantics, but bool_switch(). This means that no value can be explicitly taken for this option from the command line. One can find info about it at http://www.boost.org/doc/libs/release/doc/html/boost/program_options/bool_switch.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...