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;
}
上記のコードでは、引数をテストして解析を切り替えようとしています。次のテストが行われました。
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
正しい使い方を教えてください。私は何を間違っていますか?
お時間をいただきありがとうございます