I have the following code
#include <stdio.h>
#include <getopt.h>
int main(int argc, char* argv[]){
const struct option longopts[]={
{"one", required_argument, 0, '1'},
{"two", required_argument, 0, '2'},
{"three", required_argument, 0, '3'},
{"four", required_argument, 0, '4'},
{"five", required_argument, 0, '5'},
{0,0,0,0}
};
const char* shortopts="1:2:3:4:5:";
int c;
c = -1;
for(;;){
int optind = 0;
c = getopt_long(argc, argv, shortopts, longopts, &optind);
if(c<0)
break;
switch(c){
case 0:
case '1':
case '2':
case '3':
case '4':
case '5':
fprintf(stdout, "----------------------------------------\n");
fprintf(stdout, "c = %c\n", c);
fprintf(stdout, "optindd = %d\n", optind);
fprintf(stdout, "val = %c, \n", longopts[optind].val);
fprintf(stdout, "name = %s\n", longopts[optind].name);
fprintf(stdout, "optarg = %s\n", optarg);
break;
}
}
}
Input:
./a.out --one 1 -2 two --three 3 -4 four --five 5
Expected output:
I want to print members of the struct option (name and val) when its corresponding shortopt/longopt is encountered.
The above code prints the following with some unexpected outputs :
----------------------------------------
c = 1
optindd = 0
val = 1,
name = one
optarg = 1
----------------------------------------
c = 2
optindd = 0 // expected 1
val = 1, // expected 2
name = one // expected two
optarg = two
----------------------------------------
c = 3
optindd = 2
val = 3,
name = three
optarg = 3
----------------------------------------
c = 4
optindd = 0 // expected 3
val = 1, // expected 4
name = one // expected four
val = four
----------------------------------------
c = 5
optindd = 4
val = 5,
name = five
val = 5
I am using Ubuntu 14.04.