Boost logo

Boost Users :

Subject: [Boost-users] Question regarding boost::program_options
From: Ankur Sinha (sanjay.ankur_at_[hidden])
Date: 2012-01-12 14:43:01


Hello,

I've just started using boost. I'm making use of the program_options to
take arguments for my tool. I've written the following:

> mDesc.add_options()
> ("help,h", "Print this usage message.")
> ...
> ....
> ..
> ("v-level,v",boost::program_options::value<int>(&mVerboseLevel),"Debug level: 1,2,3\n(default: 0)")
> ("version",boost::program_options::value<std::string>()->zero_tokens(),"Package information: version etc.")
>

and then, I have a method, which I call in main(int argc, char **argv):

> /*
> *--------------------------------------------------------------------------------------
> * Class: Session
> * Method: Session :: ParseCommandLine
> * Description: Parse the command line
> *--------------------------------------------------------------------------------------
> */
> int
> Session::ParseCommandLine (int argc, char **argv)
> {
> boost::program_options::store(boost::program_options::parse_command_line(argc, argv, mDesc), mVariableMap);
> /* notify required to save the values into my variables. */
> /* TODO: confirm this behaviour */
> boost::program_options::notify(mVariableMap);
>
> if (mVariableMap.count("help"))
> {
> std::cout << mDesc << std::endl;
> return -1;
> }
> return 0;
> } /* ----- end of method Session::ParseCommandLine ----- */
>

However, when I invoke my tool, I get an exception (which I admit I
haven't handled yet):

> [ankur_at_ankur dump]$ ./src/test --version
> terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::program_options::validation_error> >'
> what(): in option 'version': at least one value required
> Aborted (core dumped)
> [ankur_at_ankur dump]$
>

I'm referring this document[1] which says that zero_tokens() implies an
argument may or may not be provided. Why is the exception coming up
saying I need "at least one value"? What am I missing?

Another query: is notify() necessary to save the values provided on the
command line to the variables I pass in the add_options() from the
variable map (to mVerboseLevel for instance)?

[1]
http://www.boost.org/doc/libs/1_42_0/doc/html/program_options/overview.html

-- 
Thanks, 
Regards,
Ankur: "FranciscoD"
http://fedoraproject.org/wiki/User:Ankursinha
http://dodoincfedora.wordpress.com/



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net