function Declaration
int getopt (int argc,char * CONST argv[],const char * optstring);
function Description
Getopt () is used to parse command line arguments. Parameter argc and argv are the number and contents of parameters passed by main ().
The parameter optstring represents the option string to be processed. This function returns the next option letter in argv, which corresponds to the letter in the parameter optstring. If the letter in the option string followed by a colon ":" indicates that there are also related parameters, the global variable Optarg points to this extra parameter. If getopt () cannot find the parameters that are met, the error message is printed and the global variable optarg is set to? Character, if you do not want getopt () to print an error message, you can set the global variable Opterr to 0. return value
Getopt () Each call returns the arguments passed in at the command line at a time.
Getopt () returns 1 when there is no last call to the argument.
When parsing to a parameter that is not inside the optstring, or if a required value parameter does not have a value, return '? '.
When the optstring is started with ': ', it returns ': ' Instead of '? ' when the value parameter is missing. Global Variables
Char *optarg--the current option argument string (if any).
The current index value of the int optind--argv. When Getopt () is used in the while loop, the remainder of the string is considered an operand, which is found in Argv[optind] through argv[argc-1.
int opterr--This variable is not zero, the getopt () function is "invalid option" and "Missing parameter option, and output its error message."
int optopt--when an invalid option character is found, the getopt () function or return '? ' character, or return ': ' character, and Optopt contains the invalid option characters found. definition of parameter list
Getopt () uses the optstring reference string as a short argument list, like ab:c::d is a short argument list. The definition of a short parameter is one-followed by a letter or number, like-a,-b is a short argument. Each digit or letter defines a parameter.
The short parameters are divided into three kinds in the definition of getopt: Without the value of the parameter, its definition is the parameter itself. Must have a value parameter, which is defined to add a colon after the parameter itself. The parameter of an optional value, which is defined to add a two colon after the argument itself. Note that there can be no spaces between the values of the parameters and the arguments, otherwise parsing is an error. Parameters with no value can be ligatures or separately written. Parameters are in no order. instance
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> extern char* Opta Rg /* point to the current getopt () return option (if any) (if any)/extern int optopt; /* The selected item does not appear in optstring, or when the option lacks the necessary parameters, the option is stored in optopt, GETOPT returns '. ' */extern int opterr; /* Used to control whether getopt () print error messages/extern int optind;
/* Current getopt () returns the index of the next option (argv array)/int main (int argc, char* argv[)) {int opt = 0; Opterr = 0; /* Do not print error messages/while (opt = getopt (argc, argv, "AB:C::d"))!=-1) {switch (opt) {case ' a '://This option does not
Need to have parameters printf ("option: A, next option:%s\n", Argv[optind]);
Break
Case ' B '://This option must be with parameters printf ("option: B, with parameters:%s\n", Optarg);
Break
Case ' C '://Optional printf ("option: C, with parameters:%s\n", optarg) for this option band;
Break
Case ' d '://This option does not require printf with parameters ("option: d\n");
Break Case '? '://Not in the list of options, and saved in optopt printf ("Unknown option:%c\ n ", optopt);
Break
Default:break;
} return 0; }
Run Results
root@qt:/home/user/share#./A.OUT-ACD
Options: A, next option:-ACD
option: C, with parameters: D
root@qt:/home/user/share#
root@qt:/home/user/share#./a.out-a-babc-c
option: A, next option:-BABC
option: B, with parameters: ABC
option: C, with parameters: (NULL)
root@qt:/home/user/share#
root@qt:/home/user/share#./a.out-c123456-k
Options: C, with parameters: 123456
unknown option: K
root@qt:/home/user/share#./a.out-a kill-c123456-k
option: A, next option: Kill
option: C, with parameters: 123456
Do not know the option: K