1 #ifndef __PARSE_OPTIONS_H__
2 #define __PARSE_OPTIONS_H__
11 /* options with no arguments */
17 /* options with arguments (usually) */
26 enum parse_opt_flags
{
27 PARSE_OPT_KEEP_DASHDASH
= 1,
28 PARSE_OPT_STOP_AT_NON_OPTION
= 2,
29 PARSE_OPT_KEEP_ARGV0
= 4,
30 PARSE_OPT_KEEP_UNKNOWN
= 8,
31 PARSE_OPT_NO_INTERNAL_HELP
= 16,
34 enum parse_opt_option_flags
{
39 PARSE_OPT_LASTARG_DEFAULT
= 16,
43 typedef int parse_opt_cb(const struct option
*, const char *arg
, int unset
);
46 * holds the type of the option, you must have an OPTION_END last in your
50 * the character to use as a short option name, '\0' if none.
53 * the long option name, without the leading dashes, NULL if none.
56 * stores pointers to the values to be filled.
59 * token to explain the kind of argument this option wants. Keep it
60 * homogenous across the repository.
63 * the short help associated to what the option does.
64 * Must never be NULL (except for OPTION_END).
65 * OPTION_GROUP uses this pointer to store the group header.
68 * mask of parse_opt_option_flags.
69 * PARSE_OPT_OPTARG: says that the argument is optionnal (not for BOOLEANs)
70 * PARSE_OPT_NOARG: says that this option takes no argument, for CALLBACKs
71 * PARSE_OPT_NONEG: says that this option cannot be negated
72 * PARSE_OPT_HIDDEN this option is skipped in the default usage, showed in
76 * pointer to the callback to use for OPTION_CALLBACK.
79 * default value to fill (*->value) with for PARSE_OPT_OPTARG.
80 * OPTION_{BIT,SET_UINT,SET_PTR} store the {mask,integer,pointer} to put in
82 * CALLBACKS can use it like they want.
85 enum parse_opt_type type
;
87 const char *long_name
;
93 parse_opt_cb
*callback
;
97 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
98 #define check_vtype(v, type) \
99 (BUILD_BUG_ON_ZERO(!__builtin_types_compatible_p(typeof(v), type)) + v)
101 #define OPT_INTEGER(s, l, v, h) \
103 .type = OPTION_INTEGER, \
106 .value = check_vtype(v, int *), \
110 #define OPT_U64(s, l, v, h) \
112 .type = OPTION_U64, \
115 .value = check_vtype(v, u64 *), \
119 #define OPT_STRING(s, l, v, a, h) \
121 .type = OPTION_STRING, \
124 .value = check_vtype(v, const char **), (a), \
128 #define OPT_BOOLEAN(s, l, v, h) \
130 .type = OPTION_BOOLEAN, \
133 .value = check_vtype(v, bool *), \
137 #define OPT_INCR(s, l, v, h) \
139 .type = OPTION_INCR, \
142 .value = check_vtype(v, int *), \
146 #define OPT_GROUP(h) \
148 .type = OPTION_GROUP, \
152 #define OPT_CALLBACK(s, l, v, a, h, f) \
154 .type = OPTION_CALLBACK, \
163 #define OPT_CALLBACK_NOOPT(s, l, v, a, h, f) \
165 .type = OPTION_CALLBACK, \
172 .flags = PARSE_OPT_NOARG \
175 #define OPT_CALLBACK_DEFAULT(s, l, v, a, h, f, d) \
177 .type = OPTION_CALLBACK, \
183 .defval = (intptr_t)d, \
184 .flags = PARSE_OPT_LASTARG_DEFAULT \
187 #define OPT_END() { .type = OPTION_END }
196 * It's okay for the caller to consume argv/argc in the usual way.
197 * Other fields of that structure are private to parse-options and should not
198 * be modified in any way.
200 struct parse_opt_ctx_t
{
208 /* global functions */
209 void usage_with_options(const char * const *usagestr
,
210 const struct option
*opts
);
211 int parse_options(int argc
, const char **argv
, const struct option
*options
,
212 const char * const usagestr
[], int flags
);