如何使用升压program_options读取一个整数数组? [英] How to use boost program_options to read an integer array?

查看:128
本文介绍了如何使用升压program_options读取一个整数数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Ubuntu和提高V1.50。

I am using Ubuntu and boost v1.50.

previously我用升压program_options一组选项送入一个程序,像这样:

Previously I used boost program_options to feed a set of options into a program like so:

#!/bin/bash

./prog --arg1 1 --arg2 "2" --arg3 {1,2,3} --arg4 {1,2} --arg5 5

所以,我处理单整数,字符串和整型数组的混合。
这工作得很好。

So I am dealing with a mix of single integers, strings and integer arrays. This worked fine.

然而,在bash引入局部变量精益求精的code后,我有:

However, after "improving" the code by introducing local variables in bash, I have:

#!/bin/bash
a1=1
a2="2"
a3={1,2,3}
a4={1,2}
a5=5

./prog --arg1 $a1 --arg2 $a2 --arg3 $a3 --arg4 $a4 --arg5 $a5

执行此导致一个错误:

Executing this results in an error:

error: the argument ('{1,2,3}') for option '--arg3' is invalid

我已经设置了升压program_options是这样的:

I have set up the boost program_options like this:

namespace po = boost::program_options;
using namespace std;
try{
    po::options_description desc("Allowed options");
    desc.add_options()
        ("help", "produce help message")
        ("arg1", po::value<int>(&arg1)->required(), "doc1")
        ("arg2", po::value<string>(&arg2)->default_value("test"), "doc2")
        ("arg3", po::value<vector<int> >(&arg3)->multitoken(), "doc3")
        ("arg4", po::value<vector<int> >(&arg4)->multitoken(), "doc4")
        ("arg5", po::value<int>(&arg5)->default_value(1), "doc5")
        ;

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

    if(vm.count("help")) cout << desc << "\n";
}
catch(exception& e){
    cerr << "error: " << e.what() << "\n";
    errorflag=1;
}
catch(...){
    cerr << "Exception of unknown type!\n";
    errorflag=1;
}

在哪里我会错呢?是多令牌在这种情况下并不合适?我可以用呢?它是无法读取整型数组?

Where did I go wrong? Is multitoken not appropriate in this context? What can I use instead? Is it not possible to read integer arrays?

我试着忽略多令牌,但随后也将失败。
利用围绕在bash脚本中的局部变量引号没有帮助的。

I tried omitting multitoken but then it fails also. Using quotation marks around the local variable in the bash script does not help either.

如果我改变从数组输入{A,B,C}为A B C,这是确定。不过,我已经有一大批其它格式的条目,我想其他的方案依赖于它太继续使用它。

If I change the array input from {a,b,c} to "a b c", it is ok. However, I already have a large number of entries in the other format and I am would like to continue using it as other programs depend on it too.

我觉得应该是可行的,因为它的工作没有局部变量。是否有人知道怎么样?

I think it should be doable, since it worked without the local variables. Does someone know how?

编辑:我已经错了。 A B C不作为输入的工作:(

I have been mistaken. "a b c" does NOT work as input :(

编辑2:我想出了一个小的解决方法:
我将{A,B,C} - >使用A B C

EDIT 2: I came up with a little workaround: I convert {a,b,c} -> a b c using

argnew=`echo ${arg:1:-1} | tr ',' ' '`

和它喂到程序工作正常。那是最好的解决办法?

and the feeding it to the program works fine. Is that the best solution?

推荐答案

更改原始脚本中添加 -x 的bash调试选项,如:

Changing your original script to add the -x bash debugging option, like this:

#!/bin/bash -x

./prog --arg1 1 --arg2 "2" --arg3 {1,2,3} --arg4 {1,2} --arg5 5

,然后运行它显示输出:

and then running it shows this output:

+ ./prog --arg1 1 --arg2 2 --arg3 1 2 3 --arg4 1 2 --arg5 5

所以,你的花括号分组工作不这样,你认为他们正在努力,因为bash命令行处理 ./ PROG 之前调用扩大它们。

您可以得到它的工作,如果在你的第二个脚本,如果你改变了分配的 A3 A4 来是这样的:

You can get it working if in your second script, if you change the assignments for a3 and a4 to be like this:

a3='1 2 3'
a4='1 2'

和则双引号所有,当你调用 ./ PROG 变量:

and then double-quote all your variables when you call ./prog:

./prog --arg1 "$a1" --arg2 "$a2" --arg3 "$a3" --arg4 "$a4" --arg5 "$a5"

这篇关于如何使用升压program_options读取一个整数数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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