Getopt-The optional "description" of the parse command getopt is just a simple parse command option that can only be parsed in a simple format, with the following format: 1, form: cmd [-a][-b]//parsing of short options, 2, Shape: cmd [-A a_argument ][-b B_argument]//parameter resolution for short options and short options, 3, form: cmd [-a[a_argument]]//Option A parameter is also optional case resolution
Prototypes:
extern Char *optarg; extern int Optind, Opterr, optopt; int getopt (intchar,constconstchar *optstring);
Description:1, getopt function parse command line arguments,
argc、
argvIs the parameter passed in when the main function is called. The incoming '-' start character is parsed as an option, getopt once execution resolves an option, if the loop executes, you can resolve all the options in the argv, 2, in getopt execution, each entry will update the Optind variable, The variable points to the next argv parameter, 3, if getopt returns 1, means all the options in argv[] are resolved, Optind points to the first non-option argument element; During getopt execution, a separate argument is exchanged to the back of the argv array, and the option options are advanced, such as Cmd-a file1-b file2, if A/b is an option without parameters, this eventually argv array becomes: cmd-a-B file1 File2;4,
optstringSpecify options that are valid, one character represents an option, and a ': ' After the character indicates that the option takes an argument with two ': ' to indicate that the option has an optional parameter (parameter is optional), if there is a parameter, Optarg points to the parameter, otherwise optarg is 0;5, As I said before, getopt will adjust the argv order, but it can also be changed by setting the optstring, there are two kinds: 1) If the first parameter of optstring is ' + ' or
Posixly_correctis set, the getopt is returned with the first non-option in the order of the original argv (1; 2) If the first parameter of the optstring is '-', all non-option-selected items are processed, and 1 is returned with the character code 1, 6, If Getopt does not recognize an option character, it prints an error message to stderr and stores the character in Optopt, returning '? '. ; The calling program can set the opterr=0 setting without printing error messages;
Note: To enable printing of an error message, the first character of the optstring (or after the first character is +/-) cannot be ': ', otherwise the error will not be printed;7. If option is required in optstring, but there is no argument on the command line, then getopt will return '? ' if the first character in the optstring (or after the first character is +/-) is ': ', then ': ';
return Value:1, the return type is int, this in the programming time to note, because the return value type range to contain-1, it is easy to return the value of the receive is defined as char, but in some systems char is unsigned, will cause a program error, 2, when the incoming argv options are all parsed, getopt () Returns-1, which is also the getopt for option parsing, 3, if the option character described in optstring is resolved in argv, the character is returned if the option specifies a parameter, and the global variable Optarg points to the parameter; 4. If Getopt encounters a non-optstring-specified option character, which means that the option is unrecognized, returns '? ', and stores the option in the global variable optopt; 5, if the optstring specifies the option must take the parameter, but the corresponding option passed in the missing parameter , the return value depends on the first character of Optstring, if the first character is ': ', return ': ', otherwise return '? ' Because illegal option return is also '? ', so the first character of optstring is often specified as ': ', and the option is stored in the global variable optopt;
Test Routines:
1#include <stdio.h>2#include <stdlib.h>3#include <unistd.h>4#include <string.h>5#include <sys/types.h>6 7 intMainintargcChar*argv[])8 { 9 extern Char*Optarg;Ten extern intOptind, Opterr, optopt; One inti; A intret; - - for(i=0; i<argc; i++) the { -printf ("argv[%d]%s\n", I, Argv[i]); - } -printf ("\ n"); + - while(ret = getopt (argc, argv,": A:b::c")) != -1) + { A Switch(ret) { at Case 'a': -printf ("option:%c argv:%s\n", ret, optarg); - Break; - Case 'b': - if(Optarg) -printf ("option:%c argv:%s\n", ret, optarg); in Else -printf ("option:%c no argument\n", ret); to Break; + Case '?': -printf ("encountered a unrecognized option:%c, argv:%s\n", Optopt, Argv[optind-1]); the Break; * Case ':': $printf ("option:%c missing argument\n", optopt);Panax Notoginseng Break; - default: theprintf ("option:%c\n", ret); + Break; A } the } + -printf ("\noptind:%d\n\n", optind); $ for(I=optind; i>0&& i<argc; i++) $printf ("argv[%d]%s\n", I, Argv[i]); - -printf ("\ n"); the for(i=0; i<argc; i++) -printf ("argv[%d]%s\n", I, Argv[i]);Wuyi the return 0; -}
Then we run the test routines and do some analysis ourselves according to the previous description: [email protected]:./parse_cmdline-axxx-byyyargv[0]./parse_cmdlineargv[1]-axxxargv[2] -byyy option:a argv:xxxoption:b argv:yyy optind:3 argv[0]./parse_cmdlineargv[1]-axxxargv[2]-byyy [email protected ]:./parse_cmdline-a xxx-b yyyargv[0]./parse_cmdlineargv[1]-aargv[2] xxxargv[3]-bargv[4] yyy option:a argv:xxxoption : b no argument optind:4 argv[4] yyy argv[0]./parse_cmdlineargv[1]-aargv[2] xxxargv[3]-bargv[4] yyy from this can be seen,
the option parameter with parameters can be followed, or there may be spaces in the middle;[Email Protected]:./parse_cmdline-axxx-b yyy-cargv[0]./parse_cmdlineargv[1]-axxxargv[2]-bargv[3] yyyargv[4]-c Opti On:a argv:xxxoption:b no argumentoption:c optind:4 argv[4] yyy argv[0]./parse_cmdlineargv[1]-axxxargv[2]-bargv[3] -CARGV[4] yyy [email protected]:./parse_cmdline-axxx-byyy-cargv[0]./parse_cmdlineargv[1]-axxxargv[2]-byyyargv[3]- C option:a argv:xxxoption:b argv:yyyoption:c optind:4 argv[0]./parse_cmdlineargv[1]-axxxargv[2]-byyyargv[3]-C from here It can be seen that for-B is
Parameters can not be followed with parameters , parameters must be followed, the middle cannot have spaces。 At the same time can be seen: in the argument in the argv, the final appearance of argv[] element position change,
The option moves forward, and the parameter moves back; [email protected]:./parse_cmdline-xargv[0]./parse_cmdlineargv[1]-X encountered a unrecognized option:x, argv:-x opt Ind:2 argv[0]./parse_cmdlineargv[1]-X [email protected]:./parse_cmdline-aargv[0]./parse_cmdlineargv[1]-a option:a Missing argument optind:2 argv[0]./parse_cmdlineargv[1]-A here you can see the unrecognized option, and the option condition of the missing parameter;//Modify Code while ((ret = getopt ( ARGC, argv, "A:b::c"))! =-1) [email protected]:./parse_cmdline-x argv[0]./parse_cmdlineargv[1]-X
./parse_cmdline:invalid option--XEncountered a unrecognized option:x, argv:-X optind:2 argv[0]./parse_cmdlineargv[1]-X//Modify code while (ret = getopt (ar GC, argv, "+a:b::c"))! =-1) [email protected]:./parse_cmdline-x xxxx-ayyyy-bzzz kkkkargv[0]./parse_cmdlineargv[1]-xar GV[2] xxxxargv[3]-ayyyyargv[4]-bzzzargv[5] kkkk./parse_cmdline:invalid option--xencountered a unrecognized option:x , argv:-xoption:a argv:yyyyoption:b argv:zzz optind:4 argv[4] xxxxargv[5] kkkk argv[0]./parse_cmdlineargv[1]-xargv [2]-ayyyyargv[3]-bzzzargv[4] xxxxargv[5] KKKK This piece of code illustrates the function of the first character of Optstring, the rest of the other functions of the reader's own analysis;
The getopt of Linux programming