为程序选项提升多个值

ham*_*els 6 c++ boost command-line-interface boost-program-options

当我a.out -i file0 file1在命令行输入时,我希望选项-i同时接收file0file1但是,-i只接收file0但不接收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;
}
Run Code Online (Sandbox Code Playgroud)

Pee*_*pee 5

从肖恩·克莱恩(Sean Cline):

将您的值标记为多令牌应使其表现出预期的效果。

("input-file,i", po::value<vector<std::string>>()->multitoken(), "input file")
Run Code Online (Sandbox Code Playgroud)