Getopt (analyze command line parameters)
Related Function header file# Include <unistd. h>
Define functionsInt getopt (INT argc, char * const argv [], const char * optstring );
Function DescriptionGetopt () is used to analyze command line parameters. The argc and argv parameters are the number and content of parameters passed by main. The optstring parameter indicates the option string to be processed. This function returns the option letter in argv, which corresponds to the letter in the optstring parameter. If the letter in the option string is followed by the colon ":", it indicates that there are related parameters. The global variable optarg points to this additional parameter. If getopt () fails to find the correct parameter, an error message is printed and the global variable optopt is set to "?". If you do not want getopt () to print the error message, set the global variable opterr to 0.
Short Parameter Definition
Getopt () uses the string referred to by optstring as the short parameter list, such as "1ac: D:", which is a short parameter list. A short parameter is defined as a '-' followed by a letter or number, such as-a, and-B is a short parameter. Each number or letter defines a parameter.
The short parameters are divided into three types in the getopt definition:
1. A parameter without a value is defined as the parameter itself.
2. A parameter must contain a value. Its definition is to add a colon after the parameter itself.
3. Optional value parameter, which is defined by adding two colons after the parameter itself.
Here, we will take the "1ac: D:" as an example to describe. Among them, 1, A is a parameter without a value, and C is a parameter with a value, D is an optional parameter.
In actual calls, '-1-a-c cvalue-d','-1-a-c cvalue-ddvalue ', '-1A-ddvalue-C cvalue' is valid.
Note the following three points:1. parameters without values can be written consecutively. For example, parameters with values 1 and A can be written separately by-1-A, or by-1A or-A1. 2. parameters are in different order. The resolution results of '-1A-C cvalue-ddvalue' and '-D-C cvalue-a1' are the same. 3. Note that there must be no space between the values of the optional values and the parameters. The values must be written in a format such as-ddvalue. If the values are written in a format such as-D dvalue, an error will be parsed.
Return ValueEach call to getopt () will return the parameters passed in from the command line. Getopt () returns-1 for the last call without parameters. If a parameter not included in optstring is parsed, or a required parameter without a value '? '. When optstring starts with ':', ':' instead of '? Is returned if a parameter is missing '? '. C. c file
1 #include <stdio.h> 2 #include <unistd.h> 3 4 int main(int argc, char **argv) 5 { 6 int ch; 7 opterr = 0; 8 while ((ch = getopt(argc,argv,"a:bcde::f"))!=-1) 9 {10 switch(ch)11 {12 case ‘a‘:13 printf("option a:‘%s‘\n",optarg);14 break;15 case ‘b‘:16 printf("option b :b\n");17 break;18 case ‘e‘:19 printf("option e:‘%s‘\n",optarg);20 break;21 default:22 printf("other option :%c\n",ch);23 }24 }25 printf("optopt +%c\n",optopt);26 27 return 1;28 }
Running result:
1 ~ Home$ ./c -a 2 other option :? 3 optopt +a 4 ~ Home$ ./c -a 123 5 option a:‘123‘ 6 optopt +a 7 ~ Home$ ./c -a123 8 option a:‘123‘ 9 optopt +a10 ~ Home$ ./c -b11 option b :b12 optopt +b13 ~ Home$ ./c -cde14 other option :c15 other option :d16 other option :?17 optopt +e18 ~ Home$ ./c -e19 other option :?20 optopt +e21 ~ Home$ ./c -e12322 option e:‘123‘23 optopt +e24 ~ Home$ ./c -e 45625 option e:‘456‘26 optopt +e27 ~ Home$ ./c -f28 other option :f29 optopt +f30 ~ Home$ ./c -a 123 -bcdf -e 45631 option a:‘123‘32 option b :b33 other option :c34 other option :d35 other option :f36 option e:‘456‘37 optopt +e
Reference:
Http://vopit.blog.51cto.com/2400931/440453