Boost logo

Boost :

From: Joe Gottman (jgottman_at_[hidden])
Date: 2007-10-16 21:41:39


    I recently used the program_options library for the first time and
found it very useful. However, there's one easily implemented feature
that's missing: the ability to set the name of a parameter that's
associated with a particular option. In the first example in the
tutorial we have the code

        po::options_description desc("Allowed options");
        desc.add_options()
             ("help", "produce help message")
             ("compression", po::value<int>(), "set compression level")
        ;

When desc is then sent to cout, the output then looks like

        Allowed options:
          --help : produce help message
          --compression arg : set compression level

    But suppose we wanted the name of the parameter associated with the
--compression option to be something other than arg. As program_options
is now written there's no way to do this. This is unfortunate, because
replacing arg with a more relevant argument name could make it clearer
to the user what type of values should be passed in. I suggest adding
the following member to the typed_value template class:
        typed_value *argument_name(const std::string &);

This function would change the name of the arguement associated with the
option from "arg" to whatever string was passed in. If it isn't called
then the argument name can remain as "arg".

If this function were included, then the code above could be rewritten as

        po::options_description desc("Allowed options");
        desc.add_options()
             ("help", "produce help message")
             ("compression", po::value<int>()->argument_name("level-number"),
"set compression level")
        ;

and then the output would be

        Allowed options:
          --help : produce help message
          --compression level-number : set compression level

Joe Gottman


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