首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >boost program_options多值问题

boost program_options多值问题
EN

Stack Overflow用户
提问于 2010-07-01 01:22:02
回答 1查看 8.7K关注 0票数 8

因此,我正在编写Boost program_options库的一个示例,我想尝试为其中一个多值/向量值设置默认值,但似乎不起作用。就像我认为的suggested here to work一样。

我所修改的大约是第40行:

代码语言:javascript
复制
    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选项时,会将“-I”添加到include-path参数列表中。

有人知道为什么不是这样吗?

以下是完整的源代码:

代码语言:javascript
复制
#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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-07-01 03:39:49

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

所以,你的问题的解决方案是写下:

代码语言:javascript
复制
po::value< vector<string> >()->default_value(
      vector<string>(1, "SOMETHING"), "SOMETHING")->composing(),

这样,您就表示默认值是一个具有单个元素"SOMETHING“的向量,并且您希望在帮助中显示"SOMETHING",例如:

代码语言:javascript
复制
Configuration:
  --optimization arg (=10)              optimization level
  -I [ --include-path ] arg (=SOMETHING)
                                        include path
票数 15
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3151729

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档