0
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

sparky
  • 375
  • 6
  • 22
  • For long options you should use the format `--long-option=value`. Also, for the last you should get the printout twice with `keep_content` correct in the second line. – Some programmer dude May 29 '12 at 10:58
  • I also recommend that you always check the return value, it may be `'?'` if the command line is ambiguous or have extra unrecognized parameters. – Some programmer dude May 29 '12 at 11:05
  • hey thanks for replying. i didn't understand what's wrong with the keep option though, could you please clarify why it's not working? maybe an example? – sparky May 29 '12 at 16:45

1 Answers1

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.)

peje
  • 350
  • 1
  • 3
  • 13
  • When you process the --keep switch, optarg is a null pointer and cout fails and terminates the program before printing keep_content. – peje May 30 '12 at 07:18
  • Breaking the first cout line: `cout << "name: " << long_options[option_index].name << " " << "value: "; if(optarg) cout << optarg; cout << endl;` – peje May 30 '12 at 07:56
  • I would personally use a `switch` statement inside the `while`-loop as shown int the example in `man 3 getopt` – peje May 30 '12 at 08:17
  • yes of course, the above code was just a snippet to simplify but recreate my problem. In any case thanks for pointing out ;) – sparky May 31 '12 at 17:50