If you need to specify a series of parameters when starting the program command line, getopt () and getopt_long () are your best choice.
As a child paper that was still hand-written, I found it with tears ..
1. Int getopt (INT argc, char * const argv [], const char * optstring)
If the option is in optstring, the option character is returned; otherwise,-1 is returned. parameters corresponding to the option are saved in the optarg variable.
The parameters contained in unistd. H, argc, and argv correspond to those of main (INT argc, char * argv,
Optstring is the character set of the option, represented as the first character after '-' in the startup command line, for example, LS-l-, 'A' and 'l' are in optstring.
The optstring format is as follows:
1) A single character is a parameter Option
2) The option character is followed by ':', indicating that the option must have parameters. The parameters and options are separated by spaces, for example, start-F flile.
3) The option character is followed by ':', indicating that the option must have a parameter. The parameter is followed by the option. The two are connected together, which is an extension of GNU, for example, start-ffile.
2. Int getopt_long (INT argc, char * const argv [], const char * optstring, const struct option * longopts, int * longindex );
If the option is in optstring, the option character is returned; otherwise,-1 is returned. parameters corresponding to the option are saved in the optarg variable.
Included in getopt. in H, getopt_long () can be considered as getopt () that supports long options. The parameters of argc and argv correspond to those of main (INT argc, char * argv,
Long Options start with "--". For example, "-h" and "-- help"
In 1990s, Unix applications began to support long options. Linux is a UNIX-like system, so it is compatible with long options.
The first three parameters of getopt_long () are the same as those of getopt (). The first 4th parameters point to the array of the Option structure, which is called the "long option table ". If the longindex parameter is not set to null, it points to a variable, which is assigned the index value of the long option in longopts, which can be used for error diagnosis.
The Declaration of the Option structure in getopt. H is as follows:
Struct option {
Const char * Name;
Int has_args;
Int * flag;
Int val;
};
Name: Option string
Has_args: three parameter types. no_argument indicates no parameter (0), required_argument indicates required parameter (1), and optional_argument indicates optional parameter (2)
Flag: If it is null, getopt_long () returns the value in the Val field of the structure. If it is not null, getopt_long () will fill in the value in the Val field in the variable to which it points, and getopt_long () returns 0. Generally, flag is set to null, and Val is set to the short option corresponding to this long option.
VAL: the return value when the long option is found, or the value in * flag is loaded when the flag is not null. In typical cases, if the flag is not null, Val is a real/false value, such as 1 or 0. If the flag is null, Val is usually a character constant, if the length option is the same as the short option, the character constant should be the same as the parameter of this option in optstring.
The following is an example:
1 #include <unistd.h>
2 #include <getopt.h>
3
4 struct option opts[] = {
5 {"config", required_argument, NULL, 'f'},
6 {"help", no_argument, NULL, 'h'},
7 {"version", no_argument, NULL, 'v'}
8 };
9
10 int32_t main(int32_t argc, char **argv) {
11 char *configfile = NULL;
12 int32_t opt = 0;
13 //while ((opt = getopt(argc, argv, "f:hv")) != -1) {
14 while ((opt = getopt_long(argc, argv, "f:hv", opts, NULL)) != -1) {
15 switch (opt) {
16 case 'f':
17 configfile = strdup(optarg);
18 break;
19 case 'h':
20 LOG1("Usage: ./chatserver -hv | [-f configure_file]");
21 LOG1(" -f --config configure file");
22 LOG1(" -h --help help information");
23 LOG1(" -v --version help information");
24 return 0;
25 case 'v':
26 LOG1("version 1.0.0");
27 return 0;
28 default:
29 LOG1("Usage: ./chatserver -hv | [-f configure_file]");
30 LOG1(" -f --config configure file");
31 LOG1(" -h --help help information");
32 LOG1(" -v --version help information");
33 return -1;
34 }
35 }
36 return 0;
37 }