c ++ boost程序选项允许长短选项

时间:2014-10-18 11:58:33

标签: c++ boost arguments options boost-program-options

我试图使用boost(版本1.46)允许“long-short”选项。我的印象是,启用*伪装*提升风格将允许这样的事情。鉴于此代码:

  // 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;
  }

将产生:

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

但我想要它产生:

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

任何人都可以帮忙吗?

感谢。

1 个答案:

答案 0 :(得分:0)

是否可以选择创建两个长选项?

如果您认为在变量映射中将两个值与相同设置相关联并不好,请将其引用到相同的设置:

Live On Coliru

输出:

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

清单:

#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;
}