boost::program_options 中带参数和不带参数的参数
我编写了一个使用 boost::program_options 进行命令行解析的小应用程序。 我想要一些选项,如果存在参数,则设置一个值;如果给出参数但不存在参数,则交替打印当前值。所以“set-mode”看起来像:
dc-ctl --brightness 15
而“get mode”将是:
dc-ctl --brightness
brightness=15
问题是,我不知道如何在不捕获此异常的情况下处理第二种情况:
error: required parameter is missing in 'brightness'
是否有一种简单的方法来避免它抛出该错误?一旦参数被解析,它就会发生。
I wrote a small app that uses boost::program_options for command-line parsing.
I'd like to have some options that set a value if the argument is present, and alternately prints the current value if the parameter is given but no argument is present. So "set-mode" would look like:
dc-ctl --brightness 15
and "get mode" would be:
dc-ctl --brightness
brightness=15
The problem is, I don't know how to handle the second case without catching this exception:
error: required parameter is missing in 'brightness'
Is there an easy way to avoid having it throw that error? It happens as soon as the arguments are parsed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我从 如何接受 boost::program_options 中的空值得到了部分解决方案 建议对那些可能存在或不存在参数的参数使用implicit_value 方法。
因此,我对初始化“亮度”参数的调用如下所示:
然后,我迭代变量映射,对于任何字符串参数,我检查它是否为空,如果是,则打印当前值。该代码如下所示:
更新:这更改了上述语法,当使用隐式值时,现在给出的参数必须采用以下形式:
而不是
希望这对其他人有帮助。
I got a partial solution from How to accept empty value in boost::program_options which suggests using the implicit_value method on those parameters that may or may not have arguments present.
So my call to initialize the "brightness" parameter looks like this:
I then iterate over the variable map and for any argument that's a string, I check if it's empty and if so I print the current value. That code looks like this:
UPDATE: this changes the aforementioned syntax, when using implicit values, now arguments, when given, must be of the form:
instead of
Hope this is helpful to someone else.