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)如果该选项具有可选参数
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值