Getopt is used to parse command line option parameters.
# Include <unistd. h>
Extern char * optarg; // Parameter pointer of the option
Extern int optind, // When getopt is called next time, check the option again from the optind storage location.
Extern int opterr, // When opterr is set to 0, getopt does not output error messages to stderr.
Extern int optopt; // When the command line option character is not included in optstring or the option lacks necessary parameters, this option is stored in optopt, and getopt returns '? ',
Int getopt (INT argc, char * const argv [], const char * optstring );
Call once and return an option. If the option parameter in the command line does not check any more options contained in optstring,-1 is returned, and optind stores the first command line parameter that does not contain the option.
First, let's talk about what is an option and what is a parameter.
1. A single character, indicating options,
2. A single character followed by a colon: indicates that this option must be followed by a parameter. Parameters are immediately followed by options or separated by spaces. The pointer of this parameter is assigned to optarg.
3. A single character is followed by two colons, indicating that this option must be followed by a parameter. Parameters must be followed by no space. The pointer of this parameter is assigned to optarg. (This feature is the expansion of GNU ).
For exampleGcc-g-o Test test. C, where G and O represent options, and test is the parameter of option O.
The above is the basic meaning of the getopt () function. After you understand this, we will give an example to further understand it.
For example, we call getopt (argc, argv, "AB: C: de ::");
We can see from the above that option A and D have no parameters. Option B and C have a parameter. Option E has a parameter andIt must be followed immediately after the option and cannot be separated by spaces.Getopt first scans argv [1] to argv [argc-1], and places options and parameters to the leftmost side of the argv array, and non-option parameters to the last side of argv.
RunProgramIs:
0
1
2
3
4
5
6
7
8
9
$./Test file1-
-B-C code-D file2-e file3
During the scanning process, optind is the index of the next option. If it is not an option, it will be skipped and optind will be increased by 1. The initial optind value is 1. When argv [1] is scanned, it is a non-option parameter, skipped, optind = 2; when the-A option is scanned, the next option to be scanned is-B, and optind is changed to 3; when the-B option is scanned, there are parameters (the-C option is considered to be the option B parameter), optind = 5, And the optind = 6 is skipped when the Code option is not scanned; the-D option is scanned, and there is no parameter at the end. optind = 7; the option of scanning to file2 skips optind = 8; the parameter is supposed to exist after scanning to-e, optind = 9 but there is a space, so the E parameter is null.
After scanning, getopt modifies the argv array to the following format:
0
1
2
3
4
5
6
7
8
9
$./Test
-
-B
-C
-D
-E
File1
Code
File2
File3
At the same time, optind points to the first parameter of the non-option. As shown above, optind points to file1
CodeAs follows:
# Include <unistd. h> # include <stdio. h> int main (INT argc, char * argv []) {int aflag = 0, bflag = 0, cflag = 0; int ch; printf ("optind: % d, opterr: % d \ n ", optind, opterr); printf (" -------------------------- \ n "); While (CH = getopt (argc, argv," AB: C: de: :"))! =-1) {printf ("optind: % d, argc: % d, argv [% d]: % s \ n", optind, argc, optind, argv [optind]); Switch (CH) {Case 'A': printf ("have option:-A \ n"); break; Case 'B ': printf ("have option:-B \ n"); printf ("the argument of-B is % s \ n", optarg); break; Case 'C ': printf ("have option:-C \ n"); printf ("the argument of-C is % s \ n", optarg); break; Case 'D ': printf ("have option:-d \ n"); break; Case 'E': printf ("Ha Ve option:-e \ n "); printf (" the argument of-E is % s \ n ", optarg); break; Case '? ': Printf ("unknown option: % C \ n", (char) optopt); break;} printf ("-------------------------- \ n "); printf ("optind = % d, argv [% d] = % s \ n", optind, optind, argv [optind]);}
Execution result:
Shiqi @ wjl-desktop :~ /Code $ Vim getopt. c
Shiqi @ wjl-desktop :~ /Code $ GCC getopt. C-o G
Shiqi @ wjl-desktop :~ /Code $./g file1--B-C code-D file2-e file3
Optind: 1, opterr: 1
--------------------------
Optind: 3, argc: 10, argv [3]:-B
Have option:-
Optind: 5, argc: 10, argv [5]: Code
Have option:-B
The argument of-B is-C
Optind: 7, argc: 10, argv [7]: file2
Have option:-d
Optind: 9, argc: 10, argv [9]: file3
Have option:-e
The argument of-E is (null)
----------------------------
Optind = 6, argv [6] = file1 // After the while loop is executed, optind = 6