boost-program-options:无值选项的通知程序

发布于 2024-12-01 01:49:23 字数 227 浏览 3 评论 0原文

仅当解析选项具有 value_semantic 时,才可以使用通知程序。 给定通知程序自动处理无值选项的最佳方式是什么?

简单的方法是使用隐式赋值创建一个虚拟 value_semantic,以便用户可以传递没有值的选项。 这导致了明确提供值的可能性。 如果提供了该值,可以添加运行时检查并抛出错误。

更新: 但是,这在存在位置选项的情况下不起作用,因为位置选项的值可以跟随无值选项,并在给定值时引发异常。

One can use notifier for parsed options only if they have value_semantic.
What is the best way for no-value options to be automatically handled by the given notifier?

The simple approach is to make a dummy value_semantic with implicit assignment, so a user can pass the option without a value.
This leads to a possibility of explicitly provided values.
One can add a run-time check if the value was provided and throw an error.

Update:
BUT, this doesn't work in presence of positional options, because a positional option's value can follow no-value option raising an exception as s given value to it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

我不咬妳我踢妳 2024-12-08 01:49:23

俄罗斯程序员论坛上的一位 OXPEHOMETP 给了我使用 boost::program_options::bool_switch() 的建议。

当通过允许值的接口定义一个没有值的选项时,必须传递的不是 boost::program_options::typed_value() 作为语义,而是 bool_switch()。这意味着无法从命令行显式获取此选项的值。
人们可以在 http://www.boost.org 找到有关它的信息/doc/libs/release/doc/html/boost/program_options/bool_switch.html

One guy OXPEHOMETP on a russian programmers forum gave me a pice of advice to use boost::program_options::bool_switch().

When defining an option with no value via value-permitted interface, one must pass not boost::program_options::typed_value() as semantics, but bool_switch(). This means that no value can be explicitly taken for this option from the command line.
One can find info about it at http://www.boost.org/doc/libs/release/doc/html/boost/program_options/bool_switch.html

一世旳自豪 2024-12-08 01:49:23

这是提供一个标志作为选项来补充当前答案的完整示例:

#include <iostream>

#include <boost/program_options.hpp>

using namespace std;

namespace po = boost::program_options;


int main(int ac, char* av[])
{
    po::options_description desc("Allowed options");

    desc.add_options()
        ("help", "produce help message")
        ("overwrite,o", po::bool_switch()->default_value(false),
                        "enable file overwrite");

    po::variables_map vm;
    po::store(po::parse_command_line(ac, av, desc), vm);

    boolalpha(cout);  // display true and false for bool
    cout << "overwrite is: " << vm["overwrite"].as<bool>() << endl;

    return 0;
}

我的 qmake pro 文件(我使用 Qt 5.4):

TEMPLATE = app
CONFIG += console
CONFIG += c++14
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += main.cpp


include(deployment.pri)
qtcAddDeployment()

INCLUDEPATH += /opt/boost_1_57_0

unix:!macx: LIBS += -L/opt/boost_1_57_0/stage/lib -lboost_program_options

在没有任何选项的情况下运行程序会导致:

./untitled4
overwrite is: false

但是使用 '-o' 选项/标志运行会给出:

./untitled4 -o
overwrite is: true

This is full example on providing a flag as an option to complement current answers:

#include <iostream>

#include <boost/program_options.hpp>

using namespace std;

namespace po = boost::program_options;


int main(int ac, char* av[])
{
    po::options_description desc("Allowed options");

    desc.add_options()
        ("help", "produce help message")
        ("overwrite,o", po::bool_switch()->default_value(false),
                        "enable file overwrite");

    po::variables_map vm;
    po::store(po::parse_command_line(ac, av, desc), vm);

    boolalpha(cout);  // display true and false for bool
    cout << "overwrite is: " << vm["overwrite"].as<bool>() << endl;

    return 0;
}

Mine qmake pro file (I use Qt 5.4):

TEMPLATE = app
CONFIG += console
CONFIG += c++14
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += main.cpp


include(deployment.pri)
qtcAddDeployment()

INCLUDEPATH += /opt/boost_1_57_0

unix:!macx: LIBS += -L/opt/boost_1_57_0/stage/lib -lboost_program_options

Running the program without any options results in:

./untitled4
overwrite is: false

However running with '-o' option/flag gives:

./untitled4 -o
overwrite is: true
寄意 2024-12-08 01:49:23

使用 zero_tokens 修饰符。看来您还需要使用implicit_value,但选项名称后面提供的任何内容都不会被选项解析器使用。相反,当在命令行上注意到该选项时,隐式值将被分配给该选项,从而触发该选项的通知程序(因此请确保提供通知程序功能)。显然,选项的值类型为字符串也很重要。我不清楚为什么。

void got_foo(std::string const&);

desc.add_options()
  ("foo", 
   po::value<std::string>()
     ->implicit_value("")
     ->zero_tokens()
     ->notifier(&got_foo),
   "foo description")
;

Use the zero_tokens modifier. It seems you also need to use implicit_value, but anything provided after the option name won't be consumed by the option parser. Instead, when the option is noticed on the command line, the implicit value will be assigned to the option, triggering the option's notifier (so make sure to provide a notifier function). Apparently, it's also important for the option's value type to be string. I'm not clear on why.

void got_foo(std::string const&);

desc.add_options()
  ("foo", 
   po::value<std::string>()
     ->implicit_value("")
     ->zero_tokens()
     ->notifier(&got_foo),
   "foo description")
;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文