Header file:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main (int argc, char **argv) {
int i, ch;
opterr = 0;
while ((ch = getopt(argc, argv, "ab::c:")) != -1) {
switch (ch) {
case 'a':
printf("Option 'a\'\n");
break;
case 'b':
printf("Option 'b\'");
if (optarg)
printf(" & its Optional arg-%s", optarg);
printf("\n");
break;
case 'c':
printf("Option 'c\'");
printf(" & its required arg - %s\n", optarg);
break;
case '?':
if (optopt == 'c')
printf("For option 'c\', argument is mandatory\n");
else if (isprint (optopt))
printf("U have given an unknown "
"option - %c\n", optopt);
else
printf("Unknown Error-0x%08x\n", optopt);
break;
default:
exit(0);
}
}
for (i = optind; i < argc; i++)
printf("Redundant argument - %s\n", argv[i]);
return 0;
}
Notes:
Why optional argument is not parsed by getopt?
For optional argument, the argument should immediately follow the option character. There should not be any space between option and its argument.
Eg:
./a.out -bhello
Here, option b will take the optional argument hello since we don't have space between the option and argument
./a.out -b hello
Here, option b won't take the optional argument hello since we have space between the option and argument.
unistd.h
Synopsis:
int getopt (int argc, char **argv, const char *options)
Description:
getopt is used to parse command line options. We need to know the below information to get clear idea on getopt.
int opterr
If opterr is non-zero, then getopt will print error message for unknown options or options with missing required argument. If opterr is zero, then getopt will return '?' for unknown options or options with missing required argument.
int optopt
If getopt finds an unknown option character or an option with a missing required argument, then it stores that option character in optint.
If getopt finds an unknown option character or an option with a missing required argument, then it stores that option character in optint.
int optind
Holds the index of the next element in argv and it will be set by getopt.
char *optarg
Pointer to the value of the required option argument or optional argument and it will be set by getopt.
Argument options indicates option characters. If the option character followed by single colon(:), it indicates that the option takes required argument. If the option character followed by double colon(::), it indicates that the option takes optional argument. And hypen '-' is used to recognize options.
getopt function C example
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main (int argc, char **argv) {
int i, ch;
opterr = 0;
while ((ch = getopt(argc, argv, "ab::c:")) != -1) {
switch (ch) {
case 'a':
printf("Option 'a\'\n");
break;
case 'b':
printf("Option 'b\'");
if (optarg)
printf(" & its Optional arg-%s", optarg);
printf("\n");
break;
case 'c':
printf("Option 'c\'");
printf(" & its required arg - %s\n", optarg);
break;
case '?':
if (optopt == 'c')
printf("For option 'c\', argument is mandatory\n");
else if (isprint (optopt))
printf("U have given an unknown "
"option - %c\n", optopt);
else
printf("Unknown Error-0x%08x\n", optopt);
break;
default:
exit(0);
}
}
for (i = optind; i < argc; i++)
printf("Redundant argument - %s\n", argv[i]);
return 0;
}
Output:
jp@jp-VirtualBox:$ ./a.out -boptionB -c optionC
Option 'b' & its Optional arg-optionB
Option 'c' & its required arg - optionC
jp@jp-VirtualBox:$ ./a.out -a -c optionC optionD
Option 'a'
Option 'c' & its required arg - optionC
Redundant argument - optionD
jp@jp-VirtualBox:$ ./a.out -a -e
Option 'a'
U have given an unknown option - e
Option 'b' & its Optional arg-optionB
Option 'c' & its required arg - optionC
jp@jp-VirtualBox:$ ./a.out -a -c optionC optionD
Option 'a'
Option 'c' & its required arg - optionC
Redundant argument - optionD
jp@jp-VirtualBox:$ ./a.out -a -e
Option 'a'
U have given an unknown option - e
Notes:
Why optional argument is not parsed by getopt?
For optional argument, the argument should immediately follow the option character. There should not be any space between option and its argument.
Eg:
./a.out -bhello
Here, option b will take the optional argument hello since we don't have space between the option and argument
./a.out -b hello
Here, option b won't take the optional argument hello since we have space between the option and argument.
Do you need Finance? Are you looking for Finance? Are you looking for finance to enlarge your business? We help individuals and companies to obtain finance for business expanding and to setup a new business ranging any amount. Get finance at affordable interest rate of 3%, Do you need this finance for business and to clear your bills? Then send us an email now for more information contact us now via (financialserviceoffer876@gmail.com) whats-App +918929509036 Dr James Eric Finance Pvt Ltd Thanks
ReplyDelete