Boost.Program_options-自由值(没有选项的值)
Boost.Program_options - free value (value without an option)
我需要为程序使用以下语法:
myprogram config.ini --option1 value --option2 value2
我使用的东西如下:
namespace po = boost::program_options;
po::options_description desc("Allowed options");
desc.add_options()
("option1", po::value<std::string>()->required(), "option 1")
("option2", po::value<uint16_t>()->required(), "option 2")
("help", "this message");
po::variables_map opts;
po::store(po::command_line_parser(argc, argv).options(desc).run(), opts);
if (opts.count("help")) {
show_help(desc);
return 0;
}
po::notify(opts);
是否可以使用Boost.Program_options捕获第一个参数(config.ini
)?或者任何没有选项说明符的值?
根据文档,这些可以用位置参数处理。
您可以在指定位置选项下找到另一个很好的例子。
如果我理解您想要的功能,以下是您将如何将其结合在上面的示例中工作。
namespace po = boost::program_options;
po::options_description desc( "Allowed options" );
desc.add_options( )
( "option1", po::value<std::string>( )->required( ), "option 1" )
( "option2", po::value<uint16_t>( )->required( ), "option 2" )
// this flag needs to be added to catch the positional options
( "config-file", po::value<std::string>( ), ".ini file" )
( "help", "this message" );
po::positional_options_description positionalDescription;
// given the syntax, "config.ini" will be set in the flag "config-file"
positionalDescription.add( "config-file", -1 );
po::variables_map opts;
po::store(
po::command_line_parser( argc, argv )
.options( desc )
// we chain the method positional with our description
.positional( positionalDescription )
.run( ),
opts
);
if (opts.count( "help" ))
{
show_help( desc );
return 0;
}
po::notify( opts );
相关文章:
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- Win32编译器选项和内存分配
- C/C++预处理器是否可以检测一些编译器选项
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- 将--whole archive链接器选项与CMake和具有其他库依赖项的库一起使用
- 通过选项卡的文本设置QTabWidget顺序
- 通过ccmake在cmake中缓存依赖选项
- 如何传递多个 std::文件系统选项?
- 基于编译器选项的编译二进制路径
- "perf_event_attr"结构的"read_format"属性的选项到底是什么?
- 如何应用 libcurl 的持久连接选项
- 文件中.dat Dlib 选项
- LLVM | codegen 用于带有命令行选项的程序输入功能
- 编译 Boost 时在 OS X 上的"ld:未知选项:-soname"
- 按钮悬停在 QT 中垂直布局的选项卡小部件中不起作用
- Visual Studio C++ 它只构建选项卡中显示的文件吗?
- 在C++不适用于猜数字游戏的情况下再次播放选项
- Qt5 用户界面编译器:-i 选项不可用
- CMake - 更改共享库链接选项
- Boost-program-options:无值选项的通知器