Parsing command line options related knowledge

When met recently read source command line parsing related knowledge, rather ambiguous. Extirpated be a record

1. The basic function of the head main

main(int argc,char *argv[ ])

If only the input program name XXX , the argc =. 1 , the argv [0] points to enter the name and path of the program ./xxx; if the input XXX para_1 (program name plus a parameter), argc = 2 , the argv [0]  pointing input the program path and name , said, argv [0] points to the parameter para_1 string . And so on, that is, char * argv []: argv is an array of pointers, which is the number of elements is argc , is stored in a pointer pointing to each parameter .

2.getopt_long () function

Example:

while ((c = getopt_long (argc, argv, "i:o:c:m:t:h", longOpts, &longIndex)) != -1) {
      switch (c) {
      case 'i':
        inputfilename = optarg;
        break;
      case 'o':
        outputfilename = optarg;
        break;
}

getopt_long () for parsing command line options parameter, header files are #include <getopt.h>.

Code prototype:

getopt_long(int argc, char * const argv[], const char *optstring,const struct option *longopts, int*longindex)

Next, we analyze the meaning of each parameter represented:

& argc argv: ditto 
optstring: option string, indicate which options and parameters which need treatment options. For example where, as ab: c :: indicates a no parameters, b must have a parameter, the parameter C may have not have. In the command line input -b 20 -c -a (10) 
longopts: long names and attributes parameters 
longindex: If longindex non-empty, the variable pointed to the record of the current parameter fit the description longopts found in the first few elements, that is, the standard value longopts

 So for this example optstring "i: o: c: m: t: h" indicates no addition -h argument, else there are certain parameters.

Function's return value:

For the short option, if successfully found, the option to return the letter; if all command-line options are parsed completely, Returns - 1 ; If you have not optstring option character, returns the character ' ? ' ; If you encounter missing parameters, the return value depends in optstring first character, if the first character is ' : ' returns ' : ' , otherwise ' ? ' and prompts an error message. For a long option, if the flag is NULL, the return val, 0 otherwise 
so example while (c = -! 1) , because parsing = -1 has been completed. So optarg is Shane? (Returned when parsing the contents of each option)
optarg: points to the current option argument (if any) of the pointer. If the input -i "test.txt" then the time optarg = "test.txt"; then the time if the input -h = OPTARG "null"
OPTIND: argv next pointer index to 0 as the start
optopt: storing error option (such as lack of parameters), or do not know the Option
OPTERR: control whether to print an error to STDERR. If not, then as long as the global variable opterr can be set to 0.

 

3. command line - the difference - and

Transfer: https: //www.zhihu.com/question/267095526/answer/319387178

In simple terms, "-" and "-" are two different styles of command-line options. The former is the traditional Unix-style, which is the GNU style.

Specific point of view, the design initially Unix command-line programs, you need a command "Options" (Options) and "parameters" (Arguments) to distinguish, on the introduction of "-."

Where to "-" at the beginning, is the option. Options with a single letter as a symbol, usually an option abbreviated English.

For example, "- a" means all (all), "- c" represents the command (command), '- f' represents a file (file), "- V" indicates the version (version).

Multiple options can write continuously, such as "ls -A -l" can be written as "ls -Al."

However, the number of individual letters is limited, it will be enough, but to express the meaning is not clear enough.

So there after the GNU style to complement the above-mentioned shortcomings, use "-" as a prefix, optionally followed by a string of words, such as "--version", "- all."

Option own parameters which can be placed back separated by a space, you can use the "=", such as "--file foobar.out" is equivalent to "--file = foobar.out."

This option is often called the GNU-style "long options" (Long Options), while Unix-style as "short option" (Short Options).

Generally short option has a corresponding long option, such as "-a, - all" "- V, - version."

Guess you like

Origin www.cnblogs.com/lyeeer/p/11334138.html