Search code examples
c++boostboost-program-options

When using boost::program_options, how does one set the name of the argument?


When using boost::program_options, how do I set the name of an argument for boost::program_options::value<>()?

#include <iostream>
#include <boost/program_options.hpp>

int main()
{
  boost::program_options::options_description desc;

  desc.add_options()
    ("width", boost::program_options::value<int>(),
     "Give width");

  std::cout << desc << std::endl;

  return 0;
}

The above code gives:

  --width arg           Give width

What I want is to replace the arg name with something more descriptive like NUM:

  --width NUM           Give width

Solution

  • In recent versions of Boost (only tested for >= 1.61) this is fully supported. Below a slight modification of the first example in the tutorial, where "LEVEL" is printed instead of "arg":

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

    Live Example