Boost logo

Boost :

Subject: Re: [boost] [program_options] Feature request for untyped variables
From: Sascha Ochsenknecht (s.ochsenknecht_at_[hidden])
Date: 2009-11-17 09:28:54


I think I managed to do what you want with implicit_value(). Implicit
value is the value which will be applied if not additional token is
followed by the option on command line:

   bool helpflag = false;

   options_description desc;
        ("help,h", value<bool>(&helpflag)->implicit_value(true), "help");

Anyhow, implicit_value is not described in documentation and this is
already mentioned in Ticket#1132


Dave Johnson wrote:
> I usually store option values in a global structure so that I can access them by field name rather than by doing a map lookup with a string. This has the benefit that typos in option names are caught at compile time rather than run time. For example, I can use Options.input instead of vm["input"].
> For untyped options, typically used as boolean flags (i.e. --help, --verbose), there is no way to specify where to store a value (or a count). This means I need to access them by map lookup or manually copy them to my global options structure. I'm not happy with either one. Ideally I'd like some way to specify that this option does not have a value, but I'd like to give the parser a pointer to where I want it to put a count of its instances in the command line or maybe a pointer to a bool instead.
> Example:
> // Global struct for command line options
> struct {
> bool help;
> bool verbose;
> string inputFile;
> ...
> } Options;
> // Parse command line
> options_description desc("Allowed options");
> desc.add_options()
> ("help" , "produce help message")
> ("verbose" , "create more detailed output")
> ("input", po::value<string>(&Options.inputFile),
> "Name of the input file")
> ...
> ;
> variables_map vm;
> store(po::parse_command_line(argc, argv, desc), vm);
> notify(vm);
> // Hack! Need to manually assign bool options!
> // Options.input is automatically assigned
> = vm.count("help");
> Options.verbose = vm.count("verbose");
> _______________________________________________
> Unsubscribe & other changes:

Boost list run by bdawes at, gregod at, cpdaniel at, john at