Boost logo

Boost :

Subject: [boost] [program_options] Feature request for untyped variables
From: Dave Johnson (not_enough_spam_at_[hidden])
Date: 2009-11-17 04:53:40


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
    Options.help = vm.count("help");
    Options.verbose = vm.count("verbose");

      


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk