boost program_options 多值问题

发布于 2024-09-07 16:10:18 字数 3949 浏览 6 评论 0原文

因此,我正在研究 Boost program_options 库的示例之一,我想尝试为多值/向量值之一设置默认值,但它似乎不起作用。我认为是 建议在这里工作

我修改的内容大约在第 40 行:

    po::options_description config("Configuration");
    config.add_options()
        ("optimization", po::value<int>(&opt)->default_value(10), 
              "optimization level")
        ("include-path,I", o::value< vector<string> >()->default_value(vector<string>(),"SOMETHING")->composing(), "include path")
        ;

当我编译这个小更改时,我希望当没有传递 -I 选项时,“SOMETHING”将添加到包含路径参数列表中。

有谁知道为什么情况并非如此?

这是完整的源代码:

#include <boost/program_options.hpp>
namespace po = boost::program_options;


#include <iostream>
#include <fstream>
#include <iterator>
using namespace std;

// A helper function to simplify the main part.
template<class T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    copy(v.begin(), v.end(), ostream_iterator<T>(cout, " ")); 
    return os;
}


int main(int ac, char* av[])
{
    try {
        int opt;

        // Declare a group of options that will be 
        // allowed only on command line
        po::options_description generic("Generic options");
        generic.add_options()
            ("version,v", "print version string")
            ("help", "produce help message")    
            ;

        // Declare a group of options that will be 
        // allowed both on command line and in
        // config file
        po::options_description config("Configuration");
        config.add_options()
            ("optimization", po::value<int>(&opt)->default_value(10), 
                  "optimization level")
            ("include-path,I", 
                 po::value< vector<string> >()->default_value(vector<string>(),"SOMETHING")->composing(), 
                 "include path")
            ;

        // Hidden options, will be allowed both on command line and
        // in config file, but will not be shown to the user.
        po::options_description hidden("Hidden options");
        hidden.add_options()
            ("input-file", po::value< vector<string> >(), "input file")
            ;


        po::options_description cmdline_options;
        cmdline_options.add(generic).add(config).add(hidden);

        po::options_description config_file_options;
        config_file_options.add(config).add(hidden);

        po::options_description visible("Allowed options");
        visible.add(generic).add(config);

        po::positional_options_description p;
        p.add("input-file", -1);

        po::variables_map vm;
        store(po::command_line_parser(ac, av).
              options(cmdline_options).positional(p).run(), vm);

        ifstream ifs("multiple_sources.cfg");
        store(parse_config_file(ifs, config_file_options), vm);
        notify(vm);

        if (vm.count("help")) {
            cout << visible << "\n";
            return 0;
        }

        if (vm.count("version")) {
            cout << "Multiple sources example, version 1.0\n";
            return 0;
        }

        if (vm.count("include-path"))
        {
            cout << "Include paths are: " 
                 << vm["include-path"].as< vector<string> >() << "\n";
        }

        if (vm.count("input-file"))
        {
            cout << "Input files are: " 
                 << vm["input-file"].as< vector<string> >() << "\n";
        }

        cout << "Optimization level is " << opt << "\n";                
    }
    catch(exception& e)
    {
        cout << e.what() << "\n";
        return 1;
    }    
    return 0;
}

So I'm working off one of the examples for Boost program_options library, and I wanted to try setting a default value for one of the multiple-values/ vector-values, but it doesn't seem to work. As I think is suggested here to work.

What I've modified is on about line 40:

    po::options_description config("Configuration");
    config.add_options()
        ("optimization", po::value<int>(&opt)->default_value(10), 
              "optimization level")
        ("include-path,I", o::value< vector<string> >()->default_value(vector<string>(),"SOMETHING")->composing(), "include path")
        ;

When I compile this small change, I expect that when no -I option is passed that the "SOMETHING" is added to the include-path argument list.

Does anyone have any idea why this is not the case?

Here is the complete source code:

#include <boost/program_options.hpp>
namespace po = boost::program_options;


#include <iostream>
#include <fstream>
#include <iterator>
using namespace std;

// A helper function to simplify the main part.
template<class T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    copy(v.begin(), v.end(), ostream_iterator<T>(cout, " ")); 
    return os;
}


int main(int ac, char* av[])
{
    try {
        int opt;

        // Declare a group of options that will be 
        // allowed only on command line
        po::options_description generic("Generic options");
        generic.add_options()
            ("version,v", "print version string")
            ("help", "produce help message")    
            ;

        // Declare a group of options that will be 
        // allowed both on command line and in
        // config file
        po::options_description config("Configuration");
        config.add_options()
            ("optimization", po::value<int>(&opt)->default_value(10), 
                  "optimization level")
            ("include-path,I", 
                 po::value< vector<string> >()->default_value(vector<string>(),"SOMETHING")->composing(), 
                 "include path")
            ;

        // Hidden options, will be allowed both on command line and
        // in config file, but will not be shown to the user.
        po::options_description hidden("Hidden options");
        hidden.add_options()
            ("input-file", po::value< vector<string> >(), "input file")
            ;


        po::options_description cmdline_options;
        cmdline_options.add(generic).add(config).add(hidden);

        po::options_description config_file_options;
        config_file_options.add(config).add(hidden);

        po::options_description visible("Allowed options");
        visible.add(generic).add(config);

        po::positional_options_description p;
        p.add("input-file", -1);

        po::variables_map vm;
        store(po::command_line_parser(ac, av).
              options(cmdline_options).positional(p).run(), vm);

        ifstream ifs("multiple_sources.cfg");
        store(parse_config_file(ifs, config_file_options), vm);
        notify(vm);

        if (vm.count("help")) {
            cout << visible << "\n";
            return 0;
        }

        if (vm.count("version")) {
            cout << "Multiple sources example, version 1.0\n";
            return 0;
        }

        if (vm.count("include-path"))
        {
            cout << "Include paths are: " 
                 << vm["include-path"].as< vector<string> >() << "\n";
        }

        if (vm.count("input-file"))
        {
            cout << "Input files are: " 
                 << vm["input-file"].as< vector<string> >() << "\n";
        }

        cout << "Optimization level is " << opt << "\n";                
    }
    catch(exception& e)
    {
        cout << e.what() << "\n";
        return 1;
    }    
    return 0;
}

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

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

发布评论

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

评论(1

毅然前行 2024-09-14 16:10:18

对于“default_value”方法,第一个参数是您希望选项的实际值,第二个值只是当 boost 无法推断它时的文本表示(用于在 --help 中显示)。

所以,解决你的问题的方法是这样写:

po::value< vector<string> >()->default_value(
      vector<string>(1, "SOMETHING"), "SOMETHING")->composing(),

这样,你就说默认值是一个带有单个元素“SOMETHING”的向量,并且你想在帮助中显示“SOMETHING”,例如:

Configuration:
  --optimization arg (=10)              optimization level
  -I [ --include-path ] arg (=SOMETHING)
                                        include path

For the "default_value" method, the first parameter is the real value that you wish your option to be, the second value being only the textual representation (for display in --help) when boost cannot infer it.

So, the solution to your problem is to write:

po::value< vector<string> >()->default_value(
      vector<string>(1, "SOMETHING"), "SOMETHING")->composing(),

This way, you are saying that the default value is a vector with a single element "SOMETHING", and that you want to display "SOMETHING" in the help, such as:

Configuration:
  --optimization arg (=10)              optimization level
  -I [ --include-path ] arg (=SOMETHING)
                                        include path
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文