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
You are expecting an optional argument and according to
man 3 getopt
: