Reputation: 39
i want to parse two different command line argument such that :
file -f something -o something
i found some code on internet and i changed it but i cannot parse both of them.
int
main (int argc, char **argv)
{
char *fvalue = NULL;
char *ovalue = NULL;
int index;
int c;
opterr = 0;
while ((c = getopt (argc, argv, "fo:")) != -1)
switch (c)
{
case 'f':
fvalue = optarg;
break;
case 'o':
ovalue = optarg;
break;
default:
abort ();
}
printf ("fvalue = %s, ovalue = %s\n",
fvalue, ovalue);
for (index = optind; index < argc; index++)
printf ("Non-option argument %s\n", argv[index]);
return 0;
}
Upvotes: 1
Views: 63
Reputation: 53006
Your optstring is wrong, it must be
while ((c = getopt (argc, argv, "f:o:")) != -1)
the colon means that the option requires an argument.
Upvotes: 2