提高program_options多个值的问题 [英] boost program_options multiple values problem

查看:142
本文介绍了提高program_options多个值的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我工作过的用于升压program_options库中的一个例子,我想尝试的多值/矢量值中的一个设定的默认值,但它似乎并没有工作。因为我认为这是<一个href=\"http://old.nabble.com/-boost---program-options--Recommended-way-to-specify-a-list-of-strings-td26881411.html\">suggested在这里工作的。

我已经修改是约40行:

  PO :: options_description配置(配置);
    config.add_options()
        (优化,PO :​​:值&LT; INT&GT;(安培; OPT) - &GT; DEFAULT_VALUE(10)
              优化级别)
        (包括路径,I,邻::值&LT;矢量&lt;串&GT;&GT;() - GT; DEFAULT_VALUE(矢量&lt;串&GT;(),东西) - GT;构成(),包括路径)
        ;

当我编译这个小小的改变,我希望在没有-I选项传递的东西被添加到包括路径参数列表。

没有人有任何想法,为什么这是不是这样?

下面是完整的源$ C ​​$ C:

 的#include&LT;升压/ program_options.hpp&GT;
命名空间PO =的boost :: program_options;
#包括LT&;&iostream的GT;
#包括LT&;&的fstream GT;
#包括LT&;&迭代器GT;
使用命名空间std;//一个辅助函数来简化主要部分。
模板&LT;类T&GT;
ostream的&安培;运营商的LT;≤(ostream的&安培; OS,常量矢量&lt; T&GT;&安培; 5)
{
    副本(v.begin(),v.end(),ostream_iterator&LT; T&GT;(COUT,));
    返回操作系统;
}
INT主(INT交流,字符* AV [])
{
    尝试{
        INT选择;        //声明一组,这将是选项
        //只允许在命令行
        PO :: options_description通用(通用选项);
        generic.add_options()
            (版本,V,印刷版字符串)
            (帮助,生产帮助信息)
            ;        //声明一组,这将是选项
        //允许无论在命令行和
        //配置文件
        PO :: options_description配置(配置);
        config.add_options()
            (优化,PO :​​:值&LT; INT&GT;(安培; OPT) - &GT; DEFAULT_VALUE(10)
                  优化级别)
            (包括路径,我
                 PO ::值&LT;矢量&lt;字符串&GT; &GT;() - GT; DEFAULT_VALUE(矢量&lt;串GT;(),东西) - GT;构成()
                 包括路径)
            ;        //隐藏选项,将在命令行中都允许和
        //在配置文件中,但将不显示给用户。
        PO :: options_description隐藏(隐藏选项);
        hidden.add_options()
            (输入文件,PO :​​:值&LT;矢量&lt;串GT;&GT;(),输入文件)
            ;
        PO :: options_description cmdline_options;
        cmdline_options.add(通用)。新增(配置)。新增(隐藏);        PO :: options_description config_file_options;
        config_file_options.add(配置)。新增(隐藏);        PO :: options_description可见(允许的选项);
        visible.add(通用)。新增(配置);        PO :: positional_options_description磷;
        p.add(输入文件,-1);        PO :: variables_map VM;
        存储(PO :: command_line_parser(AC,AV)。
              选项​​(cmdline_options).positional(P).RUN(),VM);        ifstream的IFS(multiple_sources.cfg);
        存储(parse_config_file(IFS,config_file_options),VM);
        通知(VM);        如果(vm.count(帮助)){
            COUT&LT;&LT;可见&LT;&LT; \\ n;
            返回0;
        }        如果(vm.count(版本)){
            COUT&LT;&LT; 多源为例,1.0版\\ n;
            返回0;
        }        如果(vm.count(包括路径))
        {
            COUT&LT;&LT; 包括路径是:
                 &LT;&LT; VM [包括路径]为&lt;矢量&lt;字符串&GT; &GT;()&LT;&LT; \\ n;
        }        如果(vm.count(输入文件))
        {
            COUT&LT;&LT; 输入文件是:
                 &LT;&LT; VM [输入文件]为&lt;矢量&lt;字符串&GT; &GT;()&LT;&LT; \\ n;
        }        COUT&LT;&LT; 优化级别为&LT;&LT;选择&LT;&LT; \\ n;
    }
    赶上(例外急症)
    {
        COUT&LT;&LT; e.what()&所述;&下; \\ n;
        返回1;
    }
    返回0;
}


解决方案

对于DEFAULT_VALUE的方法,第一个参数是你希望你的选择是真正的价值,第二个值仅为文本重新presentation(在--help显示)时,升压不能推断它。

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

  PO ::值&LT;矢量&lt;字符串&GT; &GT;() -  GT; DEFAULT_VALUE(
      矢量&lt;串GT;(1,东西),东西) - GT;构成()

这样,你说的,默认值是单个元素东西的载体,以及要显示的帮助东西,如:

 配置:
  --optimization ARG(= 10)优化级
  -I [--include路径] ARG(=东西)
                                        包含路径

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;
}

解决方案

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

这篇关于提高program_options多个值的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆