[Boost.Program_options] zero or one tokens with zero_tokens()?

View: New views
3 Messages — Rating Filter:   Alert me  

[Boost.Program_options] zero or one tokens with zero_tokens()?

by Marti Cuquet :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

I am trying to use an option that can have either zero or one arguments.

I have seen an example in the documentation claiming that the following
code would allow the user to provide either zero or one tokens to option
'verbose', but instead it throws an exception "extra parameter" when one
token is given.

So the question is, does zero_tokens() allow the user to specify a value?

options_description desc;
desc.add_options()
    ("help", "produce help message")
    ("compression", value<string>(), "compression level")
    ("verbose", value<string>()->zero_tokens(), "verbosity level")
    ("email", value<string>()->multitoken(), "email to send to")
    ;

Thanks,
Marti
_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: [Boost.Program_options] zero or one tokens with zero_tokens()?

by Bryan Green-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Marti Cuquet writes:

> Hello,
>
> I am trying to use an option that can have either zero or one arguments.
>
> I have seen an example in the documentation claiming that the following
> code would allow the user to provide either zero or one tokens to option
> 'verbose', but instead it throws an exception "extra parameter" when one
> token is given.
>
> So the question is, does zero_tokens() allow the user to specify a value?
>
> options_description desc;
> desc.add_options()
>     ("help", "produce help message")
>     ("compression", value<string>(), "compression level")
>     ("verbose", value<string>()->zero_tokens(), "verbosity level")
>     ("email", value<string>()->multitoken(), "email to send to")
>     ;

There is an undocumented "implicit_value" feature that may be what you are
looking for.  It allows zero or one token in the style of
getopt/getopt_long:

options_description desc;
desc.add_options()
    ("help", "produce help message")
    ("verbose,v", po::value<int>()->implicit_value(1),
                      "enable verbosity (optionally specify level)")


-bryan

_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: [Boost.Program_options] zero or one tokens with zero_tokens()?

by Marti Cuquet :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Bryan Green wrote:

> Marti Cuquet writes:
>> Hello,
>>
>> I am trying to use an option that can have either zero or one arguments.
>>
>> I have seen an example in the documentation claiming that the following
>> code would allow the user to provide either zero or one tokens to option
>> 'verbose', but instead it throws an exception "extra parameter" when one
>> token is given.
>>
>> So the question is, does zero_tokens() allow the user to specify a value?
>>
>> options_description desc;
>> desc.add_options()
>>     ("help", "produce help message")
>>     ("compression", value<string>(), "compression level")
>>     ("verbose", value<string>()->zero_tokens(), "verbosity level")
>>     ("email", value<string>()->multitoken(), "email to send to")
>>     ;
>
> There is an undocumented "implicit_value" feature that may be what you are
> looking for.  It allows zero or one token in the style of
> getopt/getopt_long:
>
> options_description desc;
> desc.add_options()
>     ("help", "produce help message")
>     ("verbose,v", po::value<int>()->implicit_value(1),
>                       "enable verbosity (optionally specify level)")
>
>

Thanks, that is exactly what I was looking for.

Maybe the documentation should be updated as it says "For the third
option, the user may either provide a single token for the value, or no
token at all", refering to the

("verbose", value<string>()->zero_tokens(), "verbosity level")

Marti
_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users