1. 程式人生 > >struct option結構體的定義

struct option結構體的定義

struct optio結構體的定義

標頭檔案

extern char *optarg;
extern int optind, opterr, optopt;

#include <getopt.h>

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

結構體

struct option {
    const char *name;
    int
has_arg; int *flag; int val; };

描述

 longopts is a pointer to the  first  element  of  an  array  of
       struct option declared in <getopt.h> as

           struct option {
               const char *name;
               int         has_arg;
               int        *flag;
               int         val;
           };

       The meanings of
the different fields are: name is the name of the long option. has_arg is: no_argument (or 0) if the option does not take an argument; required_argument (or 1) if the option requires an argument; or optional_argument (or 2) if the
option takes an optional argument. flag specifies how results are returned for a long option. If flag is NULL, then getopt_long() returns val. (For example, the calling program may set val to the equiva- lent short option character.) Otherwise, getopt_long() returns 0, and flag points to a variable which is set to val if the option is found, but left unchanged if the option is not found. val is the value to return, or to load into the variable pointed to by flag. The last element of the array has to be filled with zeros. If longindex is not NULL, it points to a variable which is set to the index of the long option relative to longopts.

has_arg

取值 描述
no_argument(或0) 如果該選項沒有引數
required_argument(或1) 如果該選項需要引數
optional_argument(或2) 如果該選項具有可選引數