Detailed analysis of Linux getopt Function

Source: Internet
Author: User

Standard library function: getopt

Header file # include <unistd. h>

Int getopt (INT argc, char * const argv [], const char * optstring );

Explanation: Take the argc and argv as passed to main function (the argc and argv parameters are the same as int main (INT argc, char * argv)

And an options specifier string that tells

The getopt function what options are defined for the program and whether they have associated values.

The optstring is simply a list of characters, each representing a single character option.

If a character is followed by a colon (colon), It indicatess that the option has an associated value that

Will be taken as the next argument.

Example: getopt (argc, argv, "If: LR ");

It allows for simple options-I,-l,-R and-F (followed by a filename parameter)

Assume that the execution program is named test.

./Test-LRF: I

Then argc = 2 argv [0] = "./test" argv [1] = "-LRF: I" (verified)

 

# Include <stdio. h>
# Include <unistd. h>
# Include <sys/STAT. h>
# Include <stdlib. h>
# Include <fcntl. h>
Int main (INT argc, char * argv [])

{
Int OPT;
While (OPT = getopt (argc, argv, "If: LR "))! =-1)
{

Switch (OPT)
{
Case 'I ':
Printf ("I: % d \ n", optind );
Printf ("option: % C \ n", OPT );
Break;
Case 'l ':
Printf ("L: % d \ n", optind );
Printf ("option: % C \ n", OPT );
Break;
Case 'r ':
Printf ("R: % d \ n", optind );
Printf ("option: % C \ n", OPT );
Break;
Case 'F ':
Printf ("F: % d \ n", optind );
Printf ("filename: % s \ n", optarg );
Break;

}

}

Printf ("final optind: % d", optind );

For (; optind <argc; ++ optind)
Printf ("argument: % s \ n", argv [optind]);

Exit (0 );

}

Assume that the execution program is named aaa

./AAA-I-LR 'Hello world'-F Fred. c
The output is as follows:

I: 2
Option: I
L: 2
Option: l
R: 3
Option: R
F: 6
Filename: Fred. c

5

Argument: Hello World
Analysis:

MAN 3 getopt for help

The variable optind is the index of the next element to be processed in
Argv. The system initializes this value to 1. The caller can reset it
To 1 to restart scanning of the same argv, or when scanning a new argu ‐
Ment vector.

If getopt () finds another option character, it returns that character,
Updating the external variable optind and a static variable nextchar so
That the next call to getopt () can resume the scan with the following
Option character or argv-element.

If there are no more option characters, getopt () returns-1. Then
Optind is the index in argv of the first argv-element that is not
Option.

The variable optind (Initial Value: 1) is an argv index, representing the next element (to be processed ).

If getopt finds the option character, getopt returns this character and updates it (Note: Not necessarily add)

Optind.

If no option characters are available, getopt returns-1.

Optind is the index of the first argv element (not optional ).

Modified ).

Important: by default, getopt () permutes (Arrangement) the contents of argv as it scans, so that
Eventually all the nonoptions are at the end.

Analyze the example above:

Argv points to {"./AAA", "-I", "-LR", "Hello World", "-F", "Fred. c "}

I = 2 indicates that the index of the next element to be processed in argv is 2.

L = 2 means the same as I, because l's next element to be processed is R, and R's index is 2

R = 3 that is, the index of the element after r in argv is 3.

F = 6 because the index of Fred. c followed by F is 5, and Fred. c is the parameter of F, F = 6

Because no option is available after F, getopt returns-1, while optind returns the index of Hello world in argv (note:

The non-selectable option has been adjusted to the end of the option, so its index is 5 (./AAA-I-LR-F Fred. c 'Hello World ')

 

 

 

 

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.