使用 boost::program_options 时,如何设置参数名称?
使用 boost::program_options
时,如何设置 boost::program_options::value<>()
的参数名称?
#include <iostream>
#include <boost/program_options.hpp>
int main()
{
boost::program_options::options_description desc;
desc.add_options()
("width", boost::program_options::value<int>(),
"Give width");
std::cout << desc << std::endl;
return 0;
}
上面的代码给出:
--width arg Give width
我想要的是将 arg
名称替换为更具描述性的名称,例如 NUM
:
--width NUM Give width
When using boost::program_options
, how do I set the name of an argument for boost::program_options::value<>()
?
#include <iostream>
#include <boost/program_options.hpp>
int main()
{
boost::program_options::options_description desc;
desc.add_options()
("width", boost::program_options::value<int>(),
"Give width");
std::cout << desc << std::endl;
return 0;
}
The above code gives:
--width arg Give width
What I want is to replace the arg
name with something more descriptive like NUM
:
--width NUM Give width
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
可以通过全局变量将
arg
替换为不同的东西boost::program_options::arg
:但由于这是一个全局变量,当多个选项可能需要不同的参数时,它对解决问题没有多大帮助。
One can replace
arg
with something different via the global variableboost::program_options::arg
:But as that is a global variable, it doesn't help much to fix the problem when multiple option might require different arguments.
daminetreg 的答案有效,但当用于大量选项条目时可能会有点冗长。 我刚刚为
value( T* v )
模板编写了一个重载,以使用附加的value_name
构造我的value_sematic
:使用您可以像这样创建并添加一个新的program_option:(
注意:这并不能解决所有其他构造模板,尤其是OP想要使用的默认
value()
构造函数)daminetreg's answer works, but it can be a little verbose when used for lots of option entries. I just hacked together an overload for the
value<T>( T* v )
template to construct myvalue_sematic
s with an additionalvalue_name
: usingyou can create and add a new program_option like this:
(Note: this doesn't address all the other construction templates, especially not the default
value()
constructor that the OP wants to use)Codebender 提供的方法是您唯一可以使用的方法。 这实际上是故意的——使用“NUM”作为参数名称似乎是不值得做的微优化。 对选项的良好描述还应该说明预期的参数类型。
The approach given by Codebender is the only one you can use. This is actually intentional -- using "NUM" for name of argument appears to be micro-optimization that is not worth doing. A good description of the option should also say what kind of argument is expected.
在最新版本的 Boost(仅针对 >= 1.61 进行测试)中,这是完全支持的。 下面对教程中的第一个示例进行了稍微修改,其中打印“LEVEL”而不是“arg”:
实例
In recent versions of Boost (only tested for >= 1.61) this is fully supported. Below a slight modification of the first example in the tutorial, where "LEVEL" is printed instead of "arg":
Live Example
program_options::value_semantic
类不会参数化参数名称,因此我认为您必须定义自己的类。 像这样的东西:这应该给出类似的东西:
The
program_options::value_semantic
class doesn't parameterize the argument name, so I think you will have to define your own class. Something like this:This should give something like:
在当前版本的 boost (1.53) 中,您不再需要像 Tim Sylvester 提议的那样创建自己的类。 可以使用:boost::program_options::typed_value。 可以在其中配置value_name。
将显示配置的参数名称:
In the current version of boost (1.53) you don't need anymore to make your own class as Tim Sylvester proposed. It's possible to use : boost::program_options::typed_value. On which value_name can be configured.
Will display a configured argument name :