为程序选项提升多个值

时间:2017-02-24 00:22:58

标签: c++ boost command-line-interface boost-program-options

当我在命令行输入a.out -i file0 file1时,我希望选项-i同时接收file0file1但是-i只接收file0 1}}但不是file1

但是,我发现我必须输入a.out -i file0 -i file1才能-i选项同时接收file0file1

boost::program_options可以这样做吗?

代码改编自http://www.boost.org/doc/libs/1_62_0/libs/program_options/example/options_description.cpp

#include <boost/program_options.hpp>

using namespace boost;
namespace po = boost::program_options;

#include <iostream>
#include <algorithm>
#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>(os, " "));
    return os;
}

int main(int ac, char* av[])
{
    try {
        int opt;
        int portnum;
        po::options_description desc("Allowed options");
        desc.add_options()
                ("help", "produce help message")
                ("input-file,i", po::value< vector<std::string> >(), "input "
                        "file")
                ;

        po::variables_map vm;
        po::store(po::command_line_parser(ac, av).
                options(desc).run(), vm);
        po::notify(vm);

        if (vm.count("help")) {
            cout << "Usage: options_description [options]\n";
            cout << desc;
            return 0;
        }


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

    }
    catch(std::exception& e)
    {
        cout << e.what() << "\n";
        return 1;
    }
    return 0;
}

1 个答案:

答案 0 :(得分:5)

来自Sean Cline:

将您的值标记为多重语音应该使其按预期运行。

get<T>()