Application of Linux head file <getopt.h> and its function

Source: Internet
Author: User

This article excerpt from: https://www.cnblogs.com/qingergege/p/5914218.html

Function One:

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

Parameters 1, 2 are the input parameters of the main function, parameter 3 is the option string, and the function return value is the option character.

Parameters inside the <getopt.h file:

1, extern char* Optarg; For saving options.

2, extern int optind, used to record the next search location.

3, extern int Opterr, whether the wrong information output to stderr, 0 is not output.

4, extern int optopt, represents an option that is not in the option string optstring.

First look at a directive: gcc helloworld.c-o helloworld.out; The-O in this directive is the command-line option, and the following helloworld.out is the parameter that the option-o option carries. Some options are without parameters, so the option without parameters can be written together, for example, there are two options-C and-D, both of which have no parameters, then they can be written together, namely:-CD.

About the option string: such as "a:b:cd::e", this is an option string. corresponding to the command line is-A,-B,-C,-D,-E. A colon indicates a parameter, and a colon indicates that the option must be followed by an argument. This parameter can be connected with the options, or separated by a space, for example:-a123 and-a 123, the table appears to be 123 as-a parameter. A two colon means that the parameter of this option is optional, that is, you can have parameters or no parameters, but be aware that there are no spaces between the arguments and the options when there are parameters.

An example:

/*============================================================================ name:test.c Author:lgh = =========================================================================== */#include<unistd.h>#include<stdio.h>intMainintargcChar*argv[]) {    intch; printf ("\ n"); printf ("optind:%d, opterr:%d\n", Optind,opterr); printf ("-----------------------------------------------------------\ n");  while(ch = getopt (argc, argv,"Ab:c:de::")) != -1)    {        Switch(CH) { Case 'a': printf ("a\n\n"); printf ("The argument of-a is%s\n\n", Optarg);  Break;  Case 'b': printf ("b\n\n"); printf ("The argument of-b is%s\n\n", Optarg);  Break;  Case 'C': printf ("c\n\n"); printf ("The argument of-c is%s\n\n", Optarg);  Break;  Case 'D': printf ("d\n\n");  Break;  Case 'e': printf ("e\n\n"); printf ("The argument of-e is%s\n\n", Optarg);  Break;  Case '?': printf ("unknown option:%c\n",(Char) optopt);  Break; } printf ("Next Start search location: Optind =%d\n", Optind); printf ("-----------------------------------------------------------\ n"); printf ("\ n"); }}

The post-compilation command executes./test-b "Hello World"

The output is:

Optind:1, Opterr:1-----------------------------------------------------------is   3-----------------------------------------------------------

You can see that the initial values for both Optind and Opterr are 1, and opterr nonzero indicates that errors will be output to stderr if an error occurs.

int main (int argc, char* argv[]); ARGC represents the number of arguments, argv[] represents each parameter string. For the./test-b "Hello World" command, ARGC for 3,argv[] respectively./test-b "Hello World", actually the real parameters start from-B, which is argv[1], so the initial value of Optind is 1.

When the getopt () function is executed, each command-line argument is scanned sequentially (starting at subscript 1), the first-B is an option, and this option is available in the option string optstring, followed by a colon after B to indicate that B must be followed by a parameter, "Hello World" is its argument. So this command line is in line with the requirements. As to why Optind is 3 after execution, this is because Optind is the next getopt () function to start the search from argv[3]. Of course, this example argv[3] is gone, at this point the getopt () function returns-1, ending the while loop.

Enter the command again:./test-b "Hello World"-c1234

The output is:

Optind:1, Opterr:1-----------------------------------------------------------is  3-----------------------------------------------------------is   12344-----------------------------------------------------------

For this process, the first optind=1, find the option-B and his argument "Hello World", then the search starting position jumps to argv[3] to find the-C and his parameter 1234 (the options and parameters are linked together), because-c1234 is written together, So they both Occupy Argv[3], then the search starting position jumps to argv[4], and argv[4] is empty, so that the getopt () function returns 1, the loop ends.

Enter the command again:./test-z 123

The output is:

Optind:1, Opterr:1-----------------------------------------------------------. ' Z '  2-----------------------------------------------------------

Where./test:invalid option-' Z ' is the error output to stderr, and if the Opterr is set to 0 then there will be no output.

Enter the command again:./test-zheng

The output is:

Optind:1, Opterr:1-----------------------------------------------------------./test:invalid Option--'Z'Unknown option:z Next start search location: Optind=1-----------------------------------------------------------./test:invalid Option--'h'Unknown option:h Next start search location: Optind=1-----------------------------------------------------------EThe argument of-E isng Next Start search location: Optind=2-----------------------------------------------------------

Since the options without parameters can be written together, when the getopt () function finds the-Z and finds no in the optstring, he thinks H is also an option, that is,-H and-Z are written together, and so on, until you find-e, and then find the optstring.

Application of Linux head file <getopt.h> and its function

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.