boost::program_options 中带参数和不带参数的参数

发布于 2024-08-21 06:03:47 字数 407 浏览 11 评论 0原文

我编写了一个使用 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 技术交流群。

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

发布评论

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

评论(1

月下伊人醉 2024-08-28 06:03:47

我从 如何接受 boost::program_options 中的空值得到了部分解决方案 建议对那些可能存在或不存在参数的参数使用implicit_value 方法。
因此,我对初始化“亮度”参数的调用如下所示:

 ("brightness,b", po::value<string>()->implicit_value(""),

然后,我迭代变量映射,对于任何字符串参数,我检查它是否为空,如果是,则打印当前值。该代码如下所示:

    // check if we're just printing a feature's current value
    bool gotFeature = false;
    for (po::variables_map::iterator iter = vm.begin(); iter != vm.end(); ++iter)
    {
        /// parameter has been given with no value
        if (iter->second.value().type() == typeid(string))
            if (iter->second.as<string>().empty())
            {
                gotFeature = true;
                printFeatureValue(iter->first, camera);
            }
    }

    // this is all we're supposed to do, time to exit
    if (gotFeature)
    {
        cleanup(dc1394, camera, cameras);
        return 0;
    }

更新:这更改了上述语法,当使用隐式值时,现在给出的参数必须采用以下形式:

./dc-ctl -b500

而不是

./dc-ctl -b 500

希望这对其他人有帮助。

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:

 ("brightness,b", po::value<string>()->implicit_value(""),

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:

    // check if we're just printing a feature's current value
    bool gotFeature = false;
    for (po::variables_map::iterator iter = vm.begin(); iter != vm.end(); ++iter)
    {
        /// parameter has been given with no value
        if (iter->second.value().type() == typeid(string))
            if (iter->second.as<string>().empty())
            {
                gotFeature = true;
                printFeatureValue(iter->first, camera);
            }
    }

    // this is all we're supposed to do, time to exit
    if (gotFeature)
    {
        cleanup(dc1394, camera, cameras);
        return 0;
    }

UPDATE: this changes the aforementioned syntax, when using implicit values, now arguments, when given, must be of the form:

./dc-ctl -b500

instead of

./dc-ctl -b 500

Hope this is helpful to someone else.

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