Bbunghole Ccornholio
Bbunghole Ccornholio

Reputation: 31

c++ boost program options allow long short option

I am trying to allow "long-short" options using boost (version 1.46). I was under the impression that enabling the *disguise* boost style would allow such a thing. Given this code:

  // Declare the supported options.
  boost::program_options::options_description desc( "Allowed options" );
  desc.add_options()
    ( "help,h", "produce help message" )
    ( "RunTimeE,rtttt", boost::program_options::value<std::string>(), "RunTimeE Version" )
    ;

  boost::program_options::variables_map vm;
  boost::program_options::store( boost::program_options::command_line_parser( argc, argv )
                                 .options( desc )
                                 .style( boost::program_options::command_line_style::unix_style
                                         | boost::program_options::command_line_style::allow_long_disguise )
                                 .run(),
                                 vm );

  boost::program_options::notify( vm );    

  if (vm.count("help")) {
    cout << desc << "\n";
    return 1;
  }
  if (vm.count("RunTimeE")) {
    cout << vm["RunTimeE"].as<std::string>() << endl;
    return 1;
  }

Will produce:

./output  --help
Allowed options:
  -h [ --help ]         produce help message
  -r [ --RunTimeE ] arg RunTimeE Version

But I want it to produce:

./output  --help
Allowed options:
  -h [ --help ]             produce help message
  -rtttt [ --RunTimeE ] arg RunTimeE Version

Can anyone help with this?

Thanks.

Upvotes: 2

Views: 3641

Answers (1)

sehe
sehe

Reputation: 393134

Is it an option to create two long options?

If you don't think it's nice to have two values associated with the same setting int he variable map, refer it to the same setting:

Live On Coliru

Output:

echo ------; ./a.out -h
echo ------; ./a.out -rtttt asdasdads
echo ------; ./a.out -RunTimeE qwe7
echo ------; ./a.out --rtttt 99
------
Allowed options:
  -h [ --help ]         produce help message
  --RunTimeE arg        RunTimeE Version
  --rtttt arg           RunTimeE Version

------
vm[rtttt]:    asdasdads
optRunTimeE: asdasdads
------
vm[RunTimeE]: qwe7
optRunTimeE: qwe7
------
vm[rtttt]:    99
optRunTimeE: 99

Listing:

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

namespace po = boost::program_options;

int main(int argc, char **argv) {
    // Declare the supported options.
    boost::program_options::options_description desc("Allowed options");

    std::string optRunTimeE;
    desc.add_options()
        ("help,h", "produce help message")
        ("RunTimeE", boost::program_options::value<std::string>(&optRunTimeE), "RunTimeE Version")
        ("rtttt",    boost::program_options::value<std::string>(&optRunTimeE), "RunTimeE Version")
        ;

    boost::program_options::variables_map vm;
    boost::program_options::store(
            boost::program_options::command_line_parser(argc, argv)
            .options(desc)
            .style(
                boost::program_options::command_line_style::unix_style |
                boost::program_options::command_line_style::allow_long_disguise)
            .run(),
            vm);

    boost::program_options::notify(vm);

    if (vm.count("help")) {
        std::cout << desc << "\n";
        return 1;
    }

    if (vm.count("RunTimeE")) std::cout << "vm[RunTimeE]: " << vm["RunTimeE"].as<std::string>() << std::endl;
    if (vm.count("rtttt"))    std::cout << "vm[rtttt]:    " << vm["rtttt"].as<std::string>()    << std::endl;

    std::cout << "optRunTimeE: " << optRunTimeE << std::endl;
}

Upvotes: 1

Related Questions