问题
int next_option;
int keep_content =0;
int option_index = 0;
const string short_options = "c::";
const struct option long_options[] =
{
{"config", optional_argument, NULL, 'c'},
{"keep", no_argument, &keep_content, 1},
{ NULL,0, NULL, 0}
};
while((next_option = getopt_long(argc,argv,short_options.c_str(),long_options,&option_index))!= -1)
{
cout << "name: " << long_options[option_index].name << " " << "value: " << optarg << endl;
cout << "keep_content: " << keep_content << endl;
}
I have the above code where iam trying to test argument and switch parsing. The following test were made:
a.out -chey --> name: config value: hey //which is correct
a.out -c hey --> name: value: //what's wrong?
a.out --confighey --> name: value: //what's wrong?
a.out --config hey --> name: value: //what's wrong?
a.out -chey --keep --> name: config value: hey keep_content: 0 // what's wrong? keep_content should be 1
can you please help me understand the correct usage? what am i doing wrong?
Thank you for your time
回答1:
You are expecting an optional argument and according to man 3 getopt
:
optstring is a string containing the legitimate option characters. If such a character is followed by a colon, the option requires an argument, so getopt() places a pointer to the following text in the same argv-element, or the text of the following argv-element, in optarg. Two colons mean an option takes an optional arg; if there is text in the current argv-element (i.e., in the same word as the option name itself, for example, "-oarg"), then it is returned in optarg, otherwise optarg is set to zero. This is a GNU extension. If optstring contains W followed by a semicolon, then -W foo is treated as the long option --foo. (The -W option is reserved by POSIX.2 for implementation extensions.)
来源:https://stackoverflow.com/questions/10797731/getopt-long-doesnt-handle-my-arguments-right