C++ 如何使用boost program_选项读取整数数组?

C++ 如何使用boost program_选项读取整数数组?,c++,linux,bash,boost-program-options,C++,Linux,Bash,Boost Program Options,我正在使用Ubuntu和BoostV1.50 之前,我使用boost program_options将一组选项输入到程序中,如下所示: #!/bin/bash ./prog --arg1 1 --arg2 "2" --arg3 {1,2,3} --arg4 {1,2} --arg5 5 所以我要处理的是单整数、字符串和整数数组的混合。 这很有效 但是,在通过在bash中引入局部变量改进代码之后,我有: #!/bin/bash a1=1 a2="2" a3={1,2,3} a4={1,2} a

我正在使用Ubuntu和BoostV1.50

之前,我使用boost program_options将一组选项输入到程序中,如下所示:

#!/bin/bash

./prog --arg1 1 --arg2 "2" --arg3 {1,2,3} --arg4 {1,2} --arg5 5
所以我要处理的是单整数、字符串和整数数组的混合。 这很有效

但是,在通过在bash中引入局部变量改进代码之后,我有:

#!/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
执行此操作将导致错误:

error: the argument ('{1,2,3}') for option '--arg3' is invalid
我设置了如下boost program_选项:

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;
}
#!/bin/bash -x

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

把它输入程序运行良好。这是最好的解决方案吗?

更改原始脚本以添加-x bash调试选项,如下所示:

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;
}
#!/bin/bash -x

./prog --arg1 1 --arg2 "2" --arg3 {1,2,3} --arg4 {1,2} --arg5 5
a3='1 2 3'
a4='1 2'
然后运行它会显示以下输出:

+ ./prog --arg1 1 --arg2 2 --arg3 1 2 3 --arg4 1 2 --arg5 5
因此,大括号分组的工作方式与您认为的不同,因为bash命令行处理在调用。/prog之前对它们进行了扩展

如果在第二个脚本中,将a3和a4的赋值更改为如下所示,则可以使其工作:

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;
}
#!/bin/bash -x

./prog --arg1 1 --arg2 "2" --arg3 {1,2,3} --arg4 {1,2} --arg5 5
a3='1 2 3'
a4='1 2'
然后在调用时双引号引用所有变量。/prog:


使用我的小转换技巧EDIT 2,我不需要重写数据输入,也不需要引用所有输入变量。但是如果没有你的帮助,我不会找到根本原因,所以我接受它作为解决办法。