getopt function usage in Linux:
1. Function functions:
The Getop function is used to parse command-line arguments with the '-' or '--' parameter option, removing '-' or '-' option elements
If called repeatedly, it returns the selected character of each option element in turn.
2. Function prototype:
#include <unistd.h> int getopt (int argc, char * const argv[], const char *optstring); extern char *optarg; extern int Optind, opterr, optopt;
The meanings of several of these global variables:
extern char *optarg: The parameter that holds the selection element that must be followed by the parameter,
extern int Optind: Indicates the next parameter location to resolve, with an initial value of 1,
extern int Opterr: set to 0 will not print error messages.
extern int optopt: When an invalid option character is found, the getopt () function or return '? ' character, or return ': ' character, and Optopt contains the found invalid option character.
3, function parameter analysis:
a, int argc, char * const argv[], each holding the arguments of the main function,
b, char *optstring parameters to parse
1. A single character, which represents options, there are altogether T, D, C, H, Q five options
2. A single character followed by a colon ":" indicates that the option must be followed by a parameter, immediately after the option or separated by a space
3. A single character followed by two colons, indicating that the option can be followed by a parameter, or not followed, if followed by a parameter, the parameter must be immediately after the option cannot be separated by a space
4, if the first character of the optstring parameter is a colon, then the getopt () function remains silent and returns different characters according to the error condition, as follows:
"Invalid option"--getopt () returns '? ', and Optopt contains invalid option characters (this is normal behavior).
"missing option parameter"--getopt () returns ': ' If the first character of optstring is not a colon, Then getopt () returns '? ', which makes this case not to be separated from the case of invalid options.
4. Return value:
the option character is returned if the parameter currently being processed is an option parameter, and the option string is optstring in the string, which is the option defined.
If the parameter currently being processed is not an option parameter, Optind offsets the next argument until the first option argument is found, and then the previous description, if no option argument is found, indicating that the parsing is complete, returns 1
5. Test Case:
1, the most basic usage:
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include < Errno.h>int main (int argc,char *argv[]) { int oc; //option Character char *b_opt_arg; //option Parameter String while ((Oc = getopt (ARGC,ARGV, "NGL:") != -1) { switch (OC) { case ' n ': printf ("my name is fengkai\n"); break; case ' G ': printf ("Her name is zhanghaun\n "); break; case ' l ': b_opt_ arg = optarg; printf ("our love is %s\n", B_opt_arg); break; } } return 0;}
2, plus ":" followed by parameters:
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include < Errno.h>int main (int argc,char *argv[]) { int oc; //option Character char *b_opt_arg; //option Parameter String while ((Oc = getopt (ARGC,ARGV, "NGL:") != -1) { switch (OC) { case ' n ': printf ("my name is fengkai\n"); break; case ' G ': printf ("Her name is zhanghaun\n "); break; case ' l ': b_opt_ arg = optarg; printf ("our love is %s\n", B_opt_arg); break; case '? ': printf ("arguement error\n"); break; } } return 0;}
3, the argument starts with ":" is returned according to an error:
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include < Errno.h>int main (int argc,char *argv[]) { int oc; //option Character char ec; //Invalid option character char *b_opt_arg; //option Parameter String while ((Oc = getopt (ARGC,ARGV, ": NGL:")) != -1) { switch (OC) { case ' n ': printf ("my name is fengkai\n"); break; case ' G ': printf ("Her name is zhanghaun\n "); break; case ' l ': b_opt_arg = optarg; printf (" Our love is %s\n ", B_opt_arg); break; case '? ': printf ("arguement error\n"); break; case ': ': printf ("Missing option parameter!\n"); break; } } return 0;}
On getopt function of Unix/linux program design