使用 boost::program_options 时,如何设置参数名称?

发布于 2024-07-30 07:37:46 字数 643 浏览 14 评论 0原文

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

帝王念 2024-08-06 07:37:46

可以通过全局变量将 arg 替换为不同的东西
boost::program_options::arg

boost::program_options::arg = "NUM";

但由于这是一个全局变量,当多个选项可能需要不同的参数时,它对解决问题没有多大帮助。

One can replace arg with something different via the global variable
boost::program_options::arg:

boost::program_options::arg = "NUM";

But as that is a global variable, it doesn't help much to fix the problem when multiple option might require different arguments.

深海少女心 2024-08-06 07:37:46

daminetreg 的答案有效,但当用于大量选项条目时可能会有点冗长。 我刚刚为 value( T* v ) 模板编写了一个重载,以使用附加的 value_name 构造我的 value_sematic:使用

template<class T>
typed_value<T>*
value(T* v, const char * value_typename)
{
    typed_value<T>* r = new typed_value<T>(v);
    r->value_name( value_typename );

    return r;        
}

您可以像这样创建并添加一个新的program_option:(

int width;
desc.add_options()
    ("width", boost::program_options::value<int>( &width, "NUM"),
     "Give width");

注意:这并不能解决所有其他构造模板,尤其是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 my value_sematics with an additional value_name: using

template<class T>
typed_value<T>*
value(T* v, const char * value_typename)
{
    typed_value<T>* r = new typed_value<T>(v);
    r->value_name( value_typename );

    return r;        
}

you can create and add a new program_option like this:

int width;
desc.add_options()
    ("width", boost::program_options::value<int>( &width, "NUM"),
     "Give width");

(Note: this doesn't address all the other construction templates, especially not the default value() constructor that the OP wants to use)

忘羡 2024-08-06 07:37:46

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.

小兔几 2024-08-06 07:37:46

在最新版本的 Boost(仅针对 >= 1.61 进行测试)中,这是完全支持的。 下面对教程中的第一个示例进行了稍微修改,其中打印“LEVEL”而不是“arg”:

po::options_description desc("Allowed options");
desc.add_options()
    ("help", "produce help message")
    ("compression", po::value<int>()->value_name("LEVEL"), "set compression level")
;

实例

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":

po::options_description desc("Allowed options");
desc.add_options()
    ("help", "produce help message")
    ("compression", po::value<int>()->value_name("LEVEL"), "set compression level")
;

Live Example

一百个冬季 2024-08-06 07:37:46

program_options::value_semantic 类不会参数化参数名称,因此我认为您必须定义自己的类。 像这样的东西:

struct my_arg_type
    : public boost::program_options::typed_value<int>
{
    my_arg_type(std::string const& name)
        : boost::program_options::typed_value<int>(&my_value)
        , my_name(name)
        , my_value(0)
    {
    }
    std::string name() const { return my_name; }
    std::string my_name;
    int my_value;
};

boost::program_options::options_description desc;

my_arg_type arg("foo");
desc.add_options()
    ("width", &arg, "give width");

这应该给出类似的东西:

--witdh foo    give width

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:

struct my_arg_type
    : public boost::program_options::typed_value<int>
{
    my_arg_type(std::string const& name)
        : boost::program_options::typed_value<int>(&my_value)
        , my_name(name)
        , my_value(0)
    {
    }
    std::string name() const { return my_name; }
    std::string my_name;
    int my_value;
};

boost::program_options::options_description desc;

my_arg_type arg("foo");
desc.add_options()
    ("width", &arg, "give width");

This should give something like:

--witdh foo    give width
从此见与不见 2024-08-06 07:37:46

在当前版本的 boost (1.53) 中,您不再需要像 Tim Sylvester 提议的那样创建自己的类。 可以使用:boost::program_options::typed_value。 可以在其中配置value_name。

#include <iostream>
#include <boost/program_options.hpp>
using boost::program_options::typed_value;
using boost::program_options::options_description;

int main(int argc, char **argv) {
    options_description desc("Usage");

    int someValue;
    auto someOption = new typed_value<decltype(someValue)>(&someValue);
    someOption->value_name("NUM");
    desc.add_options()
        ("some-option,s", someOption, "The option\n");

    std::cout << desc << std::endl;
    return 0;
}

将显示配置的参数名称:

Usage:
-s [ --some-option ] NUM The option

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.

#include <iostream>
#include <boost/program_options.hpp>
using boost::program_options::typed_value;
using boost::program_options::options_description;

int main(int argc, char **argv) {
    options_description desc("Usage");

    int someValue;
    auto someOption = new typed_value<decltype(someValue)>(&someValue);
    someOption->value_name("NUM");
    desc.add_options()
        ("some-option,s", someOption, "The option\n");

    std::cout << desc << std::endl;
    return 0;
}

Will display a configured argument name :

Usage:
-s [ --some-option ] NUM The option
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文