부 program_options:분석하는 여러 인수 목록
-
23-09-2019 - |
문제
고 싶을 통과하는 여러 인수와 함께 긍정적 또는 부정적인 값입니다.그것이 가능해 분석?
현재 저는 다음과 같은 초기:
vector<int> IDlist;
namespace po = boost::program_options;
po::options_description commands("Allowed options");
commands.add_options()
("IDlist",po::value< vector<int> >(&IDlist)->multitoken(), "Which IDs to trace: ex. --IDlist=0 1 200 -2")
("help","print help")
;
고 싶 전화:
./test_ids.x --IDlist=0 1 200 -2
unknown option -2
그래서,program_options 에서는 내가 전달 -2 으로 또 다른 옵션입니다.
를 구성할 수 있습니 program_options 는 방식으로 그것을 받아들일 수 있는 부정적인 정수 값?
감사 아르 멘.
편집: BTW I was 분석은 그것에 의해 간단한 파서
store(command_line_parser(argc, argv).options(commands).run(), vm);
, 하지만, 솔루션 었을 사용하여 확장된 중 하나:
parse_command_line
해결책
Have you tried "-2"?
Edit: Quoting doesn't seem to do the trick, however, changing the command line style works:
char* v[] = {"name","--IDlist=0","1","200","-2"};
int c = 5;
std::vector<int> IDlist;
namespace po = boost::program_options;
po::options_description commands("Allowed options");
commands.add_options()
("IDlist",po::value< std::vector<int> >(&IDlist)->multitoken(), "Which IDs to trace: ex. --IDlist=0 1 200 -2")
("help","print help")
;
po::variables_map vm;
po::store(parse_command_line(c, v, commands, po::command_line_style::unix_style ^ po::command_line_style::allow_short), vm);
po::notify(vm);
BOOST_FOREACH(int id, IDlist)
std::cout << id << std::endl;
다른 팁
NOTE: this is a remark to the accepted solution.
Disabling short options is the key. The solution above proposed by kloffy works great, but if you happen to use positional_option_description
(e.g. to parse parameters without using an option like ls file.txt instead of ls --file=file.txt
) you might have a hard time converting your code to do that using parse_command_line
.
However you can also disable short options and keep using the basic_command_line_parser
like this:
Replace
store(command_line_parser(argc, argv).options(commands).run(), vm);
with
store(command_line_parser(argc, argv).options(commands).style(
po::command_line_style::unix_style ^ po::command_line_style::allow_short
).run(), vm);
도--IDlist"0,1,200,-2"또는-IDlist="0, 1, 200, -2"