您可以尝试使用multitoken
andzero_tokens
选项的技巧:
using namespace std;
namespace po = boost::program_options;
vector<string> replay;
po::options_description desc("Allowed options");
desc.add_options()
("replay,r", po::value< vector<string> >(&replay)->multitoken()->zero_tokens(), "bla bla bla");
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if (vm.count("replay"))
{
size_t s = vm["replay"].as< vector<string> >().size();
if (s == 0)
cout << "replay without args" << endl;
else if (s == 1)
cout << "replay with one arg" << endl;
else
cout << "replay with multiple args" << endl;
}
else
cout << "replay not specified" << endl;
replay
然后只需计算向量中的元素数。如果将多个参数传递给replay选项,您将需要抛出错误。