gpt4 book ai didi

c++ - boost program options 一个选项的多个值

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:28:11 24 4
gpt4 key购买 nike

当我在命令行键入 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;
}

最佳答案

来自肖恩·克莱恩:

将您的值标记为多 token 应该会使其按照您的预期运行。

("input-file,i", po::value<vector<std::string>>()->multitoken(), "input file")

关于c++ - boost program options 一个选项的多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42428671/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com