【发布时间】:2014-12-24 11:22:46
【问题描述】:
我正在对现有程序进行更新。 我正在用 boost::program_options 替换 Posix 的 getopt_long()。 但是我的工作并没有按我应该的那样工作:我想阅读以下论点:
-server=www.example.com
-c config.txt
我尝试了 boost::program_options::command_line_style 的许多可能性,但我找不到可以提供与 getopt_long 相同的行为的组合。
我发现对于参数:
-server=www.example.com
我需要旗帜:
command_line_style::allow_long_disguise | command_line_style::long_allow_adjacent
但我在创建以下标志时遇到问题:
-c config.txt
我发现了标志:
command_line_style::allow_short | command_line_style::allow_dash_for_short | command_line_style::short_allow_next
给我几乎我想要的。几乎是因为:
ProgramOptionsParserTest.cpp:107: Failure
Value of: params.config
Actual: " config.txt"
Expected: expectedParams.config
Which is: "config.txt"
所以在使用 boost::algorithm::trim() 之后,它将如我所愿。
所以我的问题是:是否可以处理诸如 -c 配置文件 有 boost::program_options 但没有 boost::algorithm::trim()?
编辑 我注意到上面的标志不适用于未注册的参数。我已经注册了选项:
programOptionsDescription.add_options()
("help,h", "display help message")
("config,c", value<std::string>(), "use configfile")
("server,s", value<std::string>(), "server")
("ipport,p", value<uint16_t>(), "server port");
但是当我使用未注册的选项时(是的,我有 basic_command_line_parser::allow_unregistered()):
-calibration=something
我明白了:
the argument ('alibration=something') for option '-config' is invalid
我在编辑后的问题是:如何处理使用 getopt_long 和 boost::program_options 的参数?
【问题讨论】:
-
注意:getopt 是 POXIX,getopt_long 不是 POSIX,而是 GNU。不同的许可证 - getopt_long 是 GPL 的。
标签: c++ boost boost-program-options getopt-long