[Convert] The getopt () function and its optind Parameter

Source: Internet
Author: User

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 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:
# 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: % DN", optind, opterr );
Printf ("-------------------------- N ");
While (CH = getopt (argc, argv, "AB: C: de ::"))! =-1)
{
Printf ("optind: % d, argc: % d, argv [% d]: % Sn", optind, argc, optind, argv [optind]);
Switch (CH ){
Case 'A ':
Printf ("have option:-Ann ");

Break;
Case 'B ':
Printf ("have option:-BN ");

Printf ("the argument of-B is % SNN", optarg );
Break;
Case 'C ':
Printf ("have option:-CN ");
Printf ("the argument of-C is % SNN", optarg );

Break;
Case 'D ':
Printf ("have option:-DN ");
Break;
Case 'E ':
Printf ("have option:-en ");
Printf ("the argument of-E is % SNN", optarg );
Break;

Case '? ':
Printf ("unknown option: % CN", (char) optopt );
Break;
}
}
Printf ("---------------------------- N ");
Printf ("optind = % d, argv [% d] = % Sn", optind, optind, argv [optind]);
}
Execution result:
[Email protected]: ~ /Code $ Vim getopt. c
[Email protected]: ~ /Code $ GCC getopt. C-o G
[Email protected]: ~ /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

[Convert] The getopt () function and its optind Parameter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.