Linux parsing command line options Getopt_long usage
In the process of inevitable need to use command-line options, you can choose to parse the command-line options, but there are ready-made, why rebuild the wheel.
The following is a description of using Getopt_long to resolve command-line options.
The main use of the program:
Whether the short option long option requires parameter comments
-V--version no query version number
-N--name is (user name) specifies the consumer
-D--debug Whether it has been tested
1. Function source
Copy Code
The code is as follows:
[CPP]
#include //getopt_long () header file location
int Getopt_long (int ___argc, char *const *___argv,
const Char *__shortopts,
const struct Option *__longopts, int *__longind);
int getopt_long_only (int ___argc, char *const *___argv,
const Char *__shortopts,
const struct Option *__longopts, int *__longind);
2. Parameter Introduction
ARGC argv: Passed directly from main function
Shortopts: Short option string. As "n:v", it should be pointed out that the short option string does not need '-', but the option needs to pass the parameter, after the short option followed by ":".
Longopts:struct option Array, for holding long option arguments.
Longind: Used to return the index value of a long option in an array of longopts structures for debugging. Normal to NULL
Below is a description of struct option
Copy Code
The code is as follows:
[CPP]
struct option
{
const char *name;//long option name
int has_arg;//Whether parameters are required
int *flag;
int Val;
};
Name: Long option names
Has_arg: Parameters are required. The value has three different cases
Copy Code
The code is as follows:
[CPP]
# define No_argument 0//No parameters required
# define Required_argument 1//must specify parameters
# define Optional_argument 2//parameter optional
Flag and Val
Flag and Val are interdependent, mainly in two ways:
(1), flag is the Null,val value used to determine the long option, so you need to specify a unique Val value for the long option. A bridge is also built for long options and short options.
(2), flag is not NULL, the Val value is stored in the storage space that the flag points to, identifying the long option that appears.
3, return value
Use short options in your program to return a short option character (such as ' n '), and then save the argument to Optarg before returning it when the parameter is required.
The long option is used in the program, and the return value is determined by flag and Val. Returns the Val value when flag is null. So different processing is done according to the Val value, which also shows that Val must be unique. When the Val value is equal to the short option value, the short option resolves the function resolution long option, and when flag is not NULL, the Val value is saved to the storage point flag, Getopt_long returns 0
There are undefined long options or short options, Getopt_long returns?
Parsing completed, Getopt_long return-1
4. Example
Theory should be combined with reality
Copy Code
The code is as follows:
[CPP]
#include
#include
#include //getopt_long () header file location
int main (int argc, char** argv)
{
const char *optstring= "N:V";
int c,deb,index;
struct option opts[]={{"username", Required_argument,null, ' n '},
{"Version", No_argument,null, ' V '},
{"Debug", no_argument,&deb,1},
{0,0,0,0}};
while ((C=getopt_long (Argc,argv,optstring,opts,&index))!=-1)
{
Switch (c)
{
Case ' n '://-n or--username Specify user name
printf ("username is%sn", optarg);
Break
Case ' V '://-v or--version, output version number
printf ("version is 0.0.1 n");
Break
Case 0://flag NOT NULL
printf ("Debug is%dn", Deb);
Break
Case '? ':/ /Option Not defined
printf ("? n");
Break
Default
printf ("C is%dn", c);
Break
}
}
return 0;
}
Run Screenshots:
Description: Getopt_long_only, this function is the same as the Getopt_long, only can use '-' followed by the long option name, such as./main-username Jackie