Boost logo

Boost Users :

From: Steven Mackenzie (boost_at_[hidden])
Date: 2007-07-24 03:11:10


Vladimir Prus wrote:
> Steven Mackenzie wrote:
>
>> Ian McCulloch wrote:
>>> Bryan Green wrote:
>>> [snip]
>>>> While I'm on the subject, you sometimes see something like the
>>>> following:
>>>>
>>>> ./prog -vvv
>>>> or
>>>> ./prog -v -v -v

>> Something like
>>
>> void VerbosityWatcher( bool val )
>> {
>> ++verbosityLevel;
>> }
>> ...
>> desc.add_options()
>> ("verbose,v",
>> po::bool_switch()
>> ->notifier( &VerbosityWatcher ),
>> "print extra information")
>> ;

> Does this actually work? I think the plan is for notifier
> functions to be called only when you call 'notify' on
> variables_map. So, notifier will be called only once.

No, it doesn't.

In fact, with program_options from 1.34.1 I can't even construct an
options_descripton which allows multiple bool_switch arguments in the command line.

The closest I could come up with is support for ./prog -v 1 -v 1 -v 1 by using
value<vector<bool> >. Using the notifier approach for this is quick and simple,
but of course doesn't actually meet the original requirement (unless the
optional arguments code that Bryan Green suggested could actually be implemented!).

I'm not expressing an opinion about command lines that accept that syntax!

===

// silly example showing a notifier for multiple values of an option
void ActionObserverBool( vector<bool >const & val )
{
        if( val.size() )
        {
                cout << "We received an option value " <<
                        val.size() << " times." << endl;
        }
}

int main(int argc, char **argv)
{
...
        actionOptions.add_options()
                ( "verbosity,v",
                        // collecting the command line input in to a vector
                        // type allows multiple occurence of the option on
                        // command line.
                        po::value< vector< bool > >()
                        ->notifier( ActionObserverBool )
                        ,
                        "Progressively increase the diagnostic detail." )
        ;
        
        store(po::command_line_parser(argc, argv).
                options(actionOptions).run(), vm);

    // run the notifier commands and store values in args.
    notify(vm);
}


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