The getopt_long of function parses the command line in detail

  • 2020-04-02 01:33:25
  • OfStack

The header file
# include < The getopt. H >

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

Function description
Getopt is used to parse command-line option arguments.
Getopt_long supports command-line resolution of long options, and the arguments argc and argv in the function are usually passed directly from the main() arguments. An optstring is a string of options.

The string optstring can include the following elements:
1. Single character, indicating options,

2. A colon after a single character: indicates that the option must be followed by an argument. Arguments are followed by options or separated by Spaces. The pointer to this parameter is assigned to optarg.

3. A single character is followed by two colons, indicating that the option can be followed by an argument or no argument. If there are arguments, they must be immediately followed by an option and not separated by a space. The pointer to this parameter is assigned to optarg. This feature is an extension of GNU.
An optstring is a string that represents an acceptable parameter. For example, "a:b: CD "means that the acceptable parameters are a,b,c,d, where the a and b parameters are followed by more parameter values. (for example: -a host --b name).

The parameter longopts is actually an instance of a structure:


struct option { 
const char *name; //Name represents the long parameter name
int has_arg ;  //Has_arg has three values. No_argument (or 0) means that the argument is not followed by the value of the argument
//Required_argument (or 1), which means that the argument must be followed by a parameter value
//Optional_argument (or 2) means that the argument can be followed by or not followed by the argument value
int *flag; 
//To determine what the return value of getopt_long() is. If the flag is null, the function returns the val value matching the option
int val; //And flag jointly determine the return value
} 

Here's an example:

struct option long_options[] = { 
{"a123", required_argument, 0, 'a'}, 
{"c123", no_argument, 0, 'c'}, 
}

Now, if the command line argument is -a 123, the call to getopt_long() returns the character 'a' and returns the string 123 from optarg (note! String 123 brought back by optarg! Optarg does not need to be defined, it is already defined in getopt.h), so if the command line argument is -c, then the call to getopt_long() will return the character 'c', while optarg is null. Finally, when getopt_long() has parsed all the arguments on the command line, -1 is returned.

The parameter longopts is actually an instance of a structure:
The same code at the page code block index 0
Here's an example:


struct option long_options[] = { 
{"a123", required_argument, 0, 'a'}, 
{"c123", no_argument, 0, 'c'}, 
} 

Now, if the command line argument is -a 123, the call to getopt_long() returns the character 'a' and returns the string 123 from optarg (note! String 123 brought back by optarg! Optarg does not need to be defined, it is already defined in getopt.h), so if the command line argument is -c, then the call to getopt_long() will return the character 'c', while optarg is null. Finally, when getopt_long() has parsed all the arguments on the command line, -1 is returned.
sample

#include <stdio.h> 
#include <getopt.h> 
char *l_opt_arg; 
char* const short_options = "nbl:"; 
struct option long_options[] = { 
{ "name", 0, NULL, 'n' }, 
{ "bf_name", 0, NULL, 'b' }, 
{ "love", 1, NULL, 'l' }, 
{ 0, 0, 0, 0}, 
}; 
int main(int argc, char *argv[]) 
{ 
int c; 
while((c = getopt_long (argc, argv, short_options, long_options, NULL)) != -1) 
{ 
switch (c) 
{ 
case 'n': 
printf("My name is XL./n"); 
break; 
case 'b': 
printf("His name is ST./n"); 
break; 
case 'l': 
l_opt_arg = optarg; 
printf("Our love is %s!/n", l_opt_arg); 
break; 
} 
} 
return 0; 
} 
[root@localhost wyp]# gcc -o getopt getopt.c 
[root@localhost wyp]# ./getopt -n -b -l forever 
My name is XL. 
His name is ST. 
Our love is forever! 
[root@localhost liuxltest]# 
[root@localhost liuxltest]# ./getopt -nb -l forever 
My name is XL. 
His name is ST. 
Our love is forever! 
[root@localhost liuxltest]# ./getopt -nbl forever 
My name is XL. 
His name is ST. 
Our love is forever!


Related articles: