[Reprint] The getopt () function and its parameters optind and getoptoptind
Recently, the getopt () function has been used to understand it. This blog post is still very clear. It is worth learning. Recently, an open-source project has been improved, hoping that you can analyze the Code with ease.
Bytes ---------------------------------------------------------------------------------------------------
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 position where optind is stored.
Extern int opterr, // when opterr = 0, getopt does not output an error message 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 example, gcc-g-o 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 that must be followed by a space. 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.
The execution program is: 0 1 2 3 4 5 6 7 8 9 $. during the/test file1-a-B-c code-d file2-e file3 scan, optind is the index of the next option, the unoption parameter is skipped, and optind is 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-a-B-c-d-e file1 code file2 file3 at the same time, optind points to the first parameter of a non-option, as shown above, optind points to file1
The Code is as follows:
1 #include <unistd.h> 2 #include <stdio.h> 3 int main(int argc, char * argv[]) 4 { 5 int aflag=0, bflag=0, cflag=0; 6 int ch; 7 printf("optind:%d,opterr:%d\n",optind,opterr); 8 printf("--------------------------\n"); 9 while ((ch = getopt(argc, argv, "ab:c:de::")) != -1)10 {11 printf("optind: %d,argc:%d,argv[%d]:%s\n", optind,argc,optind,argv[optind]);12 switch (ch) {13 case 'a':14 printf("HAVE option: -a\n\n");15 16 break;17 case 'b':18 printf("HAVE option: -b\n");19 20 printf("The argument of -b is %s\n\n", optarg);21 break;22 case 'c':23 printf("HAVE option: -c\n");24 printf("The argument of -c is %s\n\n", optarg);25 26 break;27 case 'd':28 printf("HAVE option: -d\n");29 break;30 case 'e':31 printf("HAVE option: -e\n");32 printf("The argument of -e is %s\n\n", optarg);33 break;34 35 case '?':36 printf("Unknown option: %c\n",(char)optopt);37 break;38 }39 }40 printf("----------------------------\n");41 printf("optind=%d,argv[%d]=%s\n",optind,optind,argv[optind]);42 }
Execution result:
Shiqi @ wjl-desktop :~ /Code $ vim getopt. c
Shiqi @ wjl-desktop :~ /Code $ gcc getopt. c-o g
Shiqi @ wjl-desktop :~ /Code $./g file1-a-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