代码中我们使用getopt_long来实现长选项的解析,其中我们使用了一个结构体struct options的数组,struct options longopt[].
struct options的定义如下:
struct option{
const char *name;
int has_arg;
int *flag;
int val;
};
对结构中的各元素解释如下:
const char *name
这是选项名,前面没有短横线。譬如"help"、"verbose"之类。
int has_arg
描述了选项是否有选项参数。如果有,是哪种类型的参数,此时,它的值一定是下表中的一个。
符号常量 数值 含义
no_argument 0 选项没有参数
required_argument 1 选项需要参数
optional_argument 2 选项参数可选
int *flag
如果这个指针为NULL,那么
getopt_long()返回该结构val字段中的数值。如果该指针不为NULL,getopt_long()会使得它所指向的变量中填入val字段中
的数值,并且getopt_long()返回0。如果flag不是NULL,但未发现长选项,那么它所指向的变量的数值不变。
int val
这个值是发现了长选项时的返回值,或者flag不是NULL时载入*flag中的值。典型情况下,若flag不是NULL,那么val是个真/假值,譬如
1或0;另一方面,如果flag是NULL,那么
val通常是字符常量,若长选项与短选项一致,那么该字符常量应该与optstring中出现的这个选项的参数相同。
(上部分来源于cudev http://blog.chinaunix.net/space.php?uid=20357359&do=blog&cuid=429841)
67 /* Data type for reentrant functions. */ 此用来返回函数值,这是在main函数中的一个数据结构。
68 struct _getopt_data
69 {
70 /* These have exactly the same meaning as the corresponding global
71 variables, except that they are used for the reentrant
72 versions of getopt. */
73 int optind; /*选项的索引 */
74 int opterr; /*是否打印出错误*/
75 int optopt; /*getopt返回
76 char *optarg; /*如果返回值有参数那么指向参数*/
77
78 /* Internal members. */
79
80 /* True if the internal members have been initialized. */
81 int __initialized;
82
83 /* The next char to be scanned in the option-element
84 in which the last option character we returned was found.
85 This allows us to pick up the scan where we left off.
86
87 If this is zero, or a null string, it means resume the scan
88 by advancing to the next ARGV-element. */
89 char *__nextchar; /*如果此值是0或者为空字符,那么意味着恢复查看到下个argv-字符*/
90
91 /* See __ord above. */
92 enum __ord __ordering; /*
93
94 /* If the POSIXLY_CORRECT environment variable is set
95 or getopt was called. */
96 int __posixly_correct; /*如果posixly_correct环境变量被设置为1,或者getopt被调用*/
97
98
99 /* Handle permutation of arguments. */
100
101 /* Describe the part of ARGV that contains non-options that have
102 been skipped. `first_nonopt' is the index in ARGV of the first
103 of them; `last_nonopt' is the index after the last of them. */
104
105 int __first_nonopt; /*描述部分还有空选项的被跳过的argv,first_nonopt是它们第一个argv的索引值,last_nonopt在它们最后面的那个索引值之后。
106 int __last_nonopt;
107
108 #if defined _LIBC && defined USE_NONOPTION_FLAGS
109 int __nonoption_flags_max_len;
110 int __nonoption_flags_len;
111 #endif
112 };
翻译:
/* Describe how to deal with options that follow non-option ARGV-elements.
34 /*描述怎么处理跟在空选项后面的选项*/
35 If the caller did not specify anything,
36 the default is REQUIRE_ORDER if the environment variable
37 POSIXLY_CORRECT is defined, PERMUTE otherwise.
38 /*如果调用者没有说明,那么默认的就是(REQUIRE_ORDER),当环境变量POSIXLY_CORRECT被定义,否则为PERMUTE*/
39 REQUIRE_ORDER means don't recognize them as options;
40 stop option processing when the first non-option is seen.
41 This is what Unix does.
42 This mode of operation is selected by either setting the environment
43 variable POSIXLY_CORRECT, or using `+' as the first character
44 of the list of option characters, or by calling getopt.
45 /*REQUIRE_ORDER 意味着不把它们当作选项,停止选项处理,当第一个空选项被发现,这是unix所作的。 这种处理方式被选择,当环境变量POSIX_CORRECT或者用‘+’字符当作选项字符表的第一个,或者调用getop.
46 PERMUTE is the default. We permute the contents of ARGV as we
47 scan, so that eventually all the non-options are at the end.
48 This allows options to be given in any order, even with programs
49 that were not written to expect this.
50 /*PERMUTE是默认的。我们从新排序整个被扫描的argv,直到所有最后空选项都在末尾出现。这允许选项以任何顺序出现,甚至程序都没有想到过的方式。
51 RETURN_IN_ORDER is an option available to programs that were
52 written to expect options and other ARGV-elements in any order
53 and that care about the ordering of the two. We describe each
54 non-option ARGV-element as if it were the argument of an option
55 with character code 1. Using `-' as the first character of the
56 list of option characters selects this mode of operation.
57 /*RETURN_IN_ORDER是一个可用的选项来使程序期望选项和其他的argv元素的任何顺序,并且关心它们两个的顺序。我们描述每一个空选项argv并且把它当作含有字符1的一个选项的参数。用'-'作为选项清单的第一个选项来选择这种操作方式。
58 The special argument `--' forces an end of option-scanning regardless
59 of the value of `ordering'. In the case of RETURN_IN_ORDER, only
60 `--' can cause `getopt' to return -1 with `optind' != ARGC. */
61
62 enum __ord
63 {
64 REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
65 };
阅读(668) | 评论(0) | 转发(0) |