Boost 程序选项语法
我正在使用 boost::program_options 读取用户的输入命令行参数。它工作得非常好,允许我输出有用的使用消息并正确验证输入。但是,默认情况下,长选项名称必须位于双破折号之后,例如 --my_long_option
,短选项名称位于单破折号之后并且必须是单个字符,例如; -m
。
有没有办法...
- 在单个
-
之后允许长选项? - 允许短选项有多个字符吗?
因此,允许我拥有看起来像这样的命令行。
./a.out -myopt1 foo -myopt2 bar
两种可能性应该具有相同的效果,尽管从编程的角度来看,第一种会更好。我查看了 boost::program_options::command_line_style 但看起来它不能满足我的需要。
谢谢
编辑:除了下面接受的答案之外,要使其使用该样式,必须添加以下代码(遵循 boost 文档的命名约定)
po::store(
po::command_line_parser(ac,av)
.options(desc)
.style(
po::command_line_style::unix_style
| po::command_line_style::allow_long_disguise)
.run(),
vm);
I'm using boost::program_options to read the users' input from the command line argument. It works very nicely and allows me to output helpful usage messages and validate input properly. However, by default long option names must come after a double-dash for example --my_long_option
and short options come after a single dash and must be a single character, example; -m
.
Is there a way to either...
- Allow long options after a single
-
? - Allow short options to have more than one character?
Thus allowing me to have command lines which looks like
./a.out -myopt1 foo -myopt2 bar
The two possibilities should have the same effect though from a programming point of view the first would be better. I had a look at boost::program_options::command_line_style but it doesn't look like it can do what I need.
Thanks
Edit: Further to accepted answer below to get it to use that style one must add the following code (following the naming convention of the boost docs)
po::store(
po::command_line_parser(ac,av)
.options(desc)
.style(
po::command_line_style::unix_style
| po::command_line_style::allow_long_disguise)
.run(),
vm);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据定义,短选项只有一个字符。如果他们有更多,他们就会有很多选择。
要允许长选项以单个破折号开头,请包含
allow_long_disguise
命令行样式,如您链接到的文档页面上所述:Short options by definition have just one character. If they had more, they'd be long options.
To allow long options to start with a single dash, include the
allow_long_disguise
command-line style, as described on the documentation page you linked to: