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 equivalent 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.
一般情况下,flag为0,val是一个能代表这个option的字符,用于返回值。
这里还有几个extern的全局变量,其中最有用处的就是
extern char *optarg
当我们的option后面需要跟一个值得时候(例如: --file="/dev/NULL"),optarg就是这项这个字符串的指针(也就是:optarg就是指向"/dev/NULL的指针")。
NOTE:
使用getopt_long()的时候,第三个参数也是需要的。
3. 例子:
- #include <stdio.h>
-
#include <getopt.h>
-
#include <unistd.h>
-
/**
-
* @val short_opts
-
* This is short option string, and the : after d
-
* means option d should have value like d=xxx.
- * 这里,如果option后面需要跟一个参数,即使这个参数是optional的,
- * 在那个代表option的字母后面跟一个冒号':'
-
*/
-
static const char short_opts[] = "d:hmru";
-
-
/**
-
* @val lng_opts
-
* This is for long options, and the last one
-
* in the array must be all ZERO.
-
*/
-
static const struct option lng_opts[] = {
-
{"device", required_argument, NULL, 'd'},
-
{"help", no_argument, NULL, 'h'},
-
{"mmap", no_argument, NULL, 'm'},
-
{"read", no_argument, NULL, 'r'},
-
{"usrptr", no_argument, NULL, 'u'},
-
{NULL, 0, NULL, 0}
-
};
-
extern char *optarg;
-
int get_usr_opt(int argc, char *argv[]);
-
-
int main(int argc, char *argv[]) {
-
int ret = get_usr_opt(argc, argv);
-
-
switch (ret) {
-
case 0:
-
printf("set as user option.\n");
-
break;
-
case -1:
-
printf("argv is NULL.\n");
-
break;
-
case -2:
-
printf("no user options at all.\n");
-
break;
-
case -3:
-
printf("unknown user option.\n");
-
break;
-
default:
-
perror("UnKnown error!\n");
-
}
-
-
return 0;
-
}
-
-
/**
-
* @func int get_usr_opt(int argc, char *argv[])
-
* get user setted options from command line.
-
* @param argc number of user options, the same as main.
-
* @param argv user input string, the sameas main.
-
* @return
-
* 0 set as user option.
-
* -1 argv is NULL.
-
* -2 no user options at all.
-
* -3 unknown user option.
-
*/
-
int get_usr_opt(int argc, char *argv[])
-
{
-
if (NULL == argv)
-
return -1;
-
-
if (argc < 2) {
-
return -2; /**< no user option, give default*/
-
}
-
-
int idx = 0, opt = 0;
-
while (1) {
-
opt = getopt_long(argc, argv, short_opts, lng_opts, &idx);
-
/**< get the last*/
-
if (-1 == opt)
-
break;
-
-
switch (opt) {
-
case '?':
-
perror("Unknown Option!\n");
-
return -3;
-
case 'd':
-
printf("d is chosen, option value is %s\n", optarg);
-
break;
-
case 'h':
-
printf("h is chosen\n");
-
break;
-
case 'm':
-
printf("m is chosen\n");
-
break;
-
case 'r':
-
printf("r is chosen\n");
-
break;
-
case 'u':
-
printf("u is chosen\n");
-
break;
-
default:
-
perror("Unknown Option!\n");
-
return -3;
-
}
-
}
-
-
return 0;
-
}