getopt function usage in Linux

Source: Internet
Author: User
Tags function prototype

Recently done cache lab used the getopt function, with man 3 getopt view the following usage, make a summary.

Description: The Getopt function is used to parse command-line arguments, with an option element of '-' or '-' that starts with a '-' or '-'.

As an option character. If the getopt function is called repeatedly, it returns the option characters in each option element in turn.

Use the Getopt function to include a bottom file:

#include

#include

There are several global variables related to the GETOPT function resolution parameter:

The Optind:int type, indicating the next parameter position to resolve, initially 1.

Optarg:char *, you must connect the parameters of the parameter's option element, such as the-nxzz above, Optarg to the "Xzz" string.

The Opterr:int type, set to 0, will not print the error message.

The function prototype is: int getopt (int argc, char * const argv[], const char *optstring);

Parameter description: The first two parameters and main function parameters are the same, argc save the number of parameters, argv[] Save the parameter array, the third parameter optstring is you define

A string of option characters, such as "ABC", that indicates that the command has three option elements-A,-B,-C, followed by an option character if a colon says

Specifies that the option element must have a parameter that is stored in optarg, such as "N:t",

After the option element n is to be followed by the argument, option element t

No parameters, such as-n xzz-t or-nxzz T, have two colons that indicate that the option can be optional, but optional parameters are not in the Optarg.

Return value: If the current processing parameter is an option element, and the option character is in the optstring string, the option defined for you, returns the

option character, if the option character is not what you define, return the character '? ' and update the global variable Optind, pointing to the next in the ARGC array

A parameter. If the currently processed parameter is not an OPTION element, Optind offsets the next argument until the first option element is found, and then

Then, if the option element is not found and the parsing ends, then 1 is returned by the action described earlier.

Here's a look at the example:

#include

#include

int main (int argc, char * const argv[])

{

int opt;

while (opt = getopt (argc, argv, "Nb:o::t"))!=-1) {

printf ("opt =%c, Optarg =%s, Optind =%d, argv[%d] =%SN",

Opt, Optarg, Optind, Optind, Argv[optind]);

}

return 0;

}

#./test-x-y-z <------Invalid parameter, the error message is printed and the character '? ' is returned.

Output:

./getopt:invalid option-' x '

opt =?, Optarg = (null), Optind = 2, argv[2] =-y

./getopt:invalid option--' Y '

opt =?, Optarg = (null), Optind = 3, argv[3] =-Z

./getopt:invalid option-' Z '

opt =?, Optarg = (null), Optind = 4, argv[4] = (NULL)

#./test-n-B xzz-t

opt = N, Optarg = (null), Optind = 2, argv[2] =-B

opt = b, optarg = xzz, Optind = 4, argv[4] =-T <-----------optarg The argument to the option element, and Optind skips the argument, pointing directly to the-t argument

opt = T, Optarg = (null), Optind = 5, argv[5] = (NULL)

#./test-n-bxzz-t <-------------You can also write option parameters together with their parameters

opt = N, Optarg = (null), Optind = 2, argv[2] =-bxzz

opt = b, optarg = Xzz, Optind = 3, argv[3] =-T <-----------Optind will also point to the next argument-t

opt = T, Optarg = (null), Optind = 4, argv[4] = (NULL)

#./test-b-T

opt = b, optarg = t, Optind = 3, argv[3] = (NULL) <------------T is treated as a parameter of option element-B, then no longer resolves-t, Optind is 3

#./TEST-T-B <-----B missing parameters

opt = T, Optarg = (null), Optind = 2, argv[2] =-B

./getopt:option requires an argument--' B '

opt =?, Optarg = (null), Optind = 3, argv[3] = (NULL)

#./test-t A-b Xzz <-------the command line has a useless parameter a, which is ignored when parsing, because-T requires no connection parameters

opt = T, Optarg = (null), Optind = 2, argv[2] = a

opt = b, optarg = xzz, Optind = 5, argv[5] = (NULL)

#./TEST-NTB Xzz <---------can also be linked to write the parameters

opt = N, Optarg = (null), Optind = 1, argv[1] =-NTB

opt = T, Optarg = (null), Optind = 1, argv[1] =-NTB

opt = b, optarg = Xzz, Optind = 3, argv[3] = (NULL)

#./test-t-o-b Xzz <------o option does not pick up parameters

opt = T, Optarg = (null), Optind = 2, argv[2] =-O

opt = O, Optarg = (null), Optind = 3, argv[3] =-B

opt = b, optarg = xzz, Optind = 5, argv[5] = (NULL)

#./TEST-T-O 10-b xzz <-------o option Connection parameter 10

opt = T, Optarg = (null), Optind = 2, argv[2] =-O

opt = O, Optarg = (null), Optind = 3, argv[3] = <------------------can see that 10 is not saved in Optarg

opt = b, optarg = xzz, Optind = 6, argv[6] = (NULL) <-----------------and-B parameters are saved in Optarg

Common usage:

The Getopt function is used to parse the parameters using the while loop with the switch statement, as follows:

/*

* Get parameter information

*/

int getinfo (int argc, char * const argv[], int *ps, int *pe, int *pb, char *trace_name, int *vflag)

{

int opt;

int count_arg = 0;

Opterr = 0;

while (opt = getopt (argc, argv, "Vs:e:b:t:")!=-1) {

Switch (opt) {

Case ' V ':

*vflag = 1;

Break

Case ' s ':

++count_arg;

*ps = Atoi (Optarg);

Break

Case ' E ':

++count_arg;

*pe = Atoi (Optarg);

Break

Case ' B ':

++count_arg;

*PB = Atoi (Optarg);

Break

Case ' t ':

++count_arg;

strcpy (Trace_name, Optarg);

Break

Default:/* '? ' * *

Usage ();

Exit (-1);

Break

}

}

if (Count_arg < 4) {

Usage ();

Exit (-1);

}

return 0;

}

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.