struct-option configuration language c

C language knowledge Supplements, struct option structure.

struct optionIndicates a long argument, often used in some console applications, you need to specify different parameters to run the program. As open source software webbench run ./webbench -hthere will be a number of options, the different parameters are specified, the program will make a different accordingly.

1
2
3
4
5
6
struct  {
const char *name;
int has_arg;
int *flag;
int val;
}

Parameters:
name indicates the name of the parameter length;
has_arg indicating whether needs to follow after the argument name parameter, no_argument (0) need not, required_argument (1) must be used with parameters, optional_argument (2) may or may not;
In Flag determined getopt_longthe return value is NULL, this field returns the value val; if not NULL, it will point to the contents becomes a value in val, and return 0; if not found long option, then the same point;
val designated the default values.

Parsing long command parameters

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

Parameters:
argcand argvis the command line parameters;
optstringa string consisting of option parameters, if a letter followed by a colon, such as the back t:, then this option represents a required parameter;
longoptsis a custom option structure needs;
longindexspecify an index pointer.

Examples :

Original: Big Box  struct-option structure c language


Guess you like

Origin www.cnblogs.com/wangziqiang123/p/11618347.html