1 #ifndef PARSE_OPTIONS_H
2 #define PARSE_OPTIONS_H
13 enum parse_opt_flags {
14 PARSE_OPT_KEEP_DASHDASH = 1,
17 enum parse_opt_option_flags {
23 typedef int parse_opt_cb(const struct option *, const char *arg, int unset);
27 * holds the type of the option, you must have an OPTION_END last in your
31 * the character to use as a short option name, '\0' if none.
34 * the long option name, without the leading dashes, NULL if none.
37 * stores pointers to the values to be filled.
40 * token to explain the kind of argument this option wants. Keep it
41 * homogenous across the repository.
44 * the short help associated to what the option does.
45 * Must never be NULL (except for OPTION_END).
46 * OPTION_GROUP uses this pointer to store the group header.
49 * mask of parse_opt_option_flags.
50 * PARSE_OPT_OPTARG: says that the argument is optionnal (not for BOOLEANs)
51 * PARSE_OPT_NOARG: says that this option takes no argument, for CALLBACKs
54 * pointer to the callback to use for OPTION_CALLBACK.
57 * default value to fill (*->value) with for PARSE_OPT_OPTARG.
58 * CALLBACKS can use it like they want.
61 enum parse_opt_type type;
63 const char *long_name;
69 parse_opt_cb *callback;
73 #define OPT_END() { OPTION_END }
74 #define OPT_GROUP(h) { OPTION_GROUP, 0, NULL, NULL, NULL, (h) }
75 #define OPT_BOOLEAN(s, l, v, h) { OPTION_BOOLEAN, (s), (l), (v), NULL, (h) }
76 #define OPT_INTEGER(s, l, v, h) { OPTION_INTEGER, (s), (l), (v), NULL, (h) }
77 #define OPT_STRING(s, l, v, a, h) { OPTION_STRING, (s), (l), (v), (a), (h) }
78 #define OPT_CALLBACK(s, l, v, a, h, f) \
79 { OPTION_CALLBACK, (s), (l), (v), (a), (h), 0, (f) }
81 /* parse_options() will filter out the processed options and leave the
82 * non-option argments in argv[].
83 * Returns the number of arguments left in argv[].
85 extern int parse_options(int argc, const char **argv,
86 const struct option *options,
87 const char * const usagestr[], int flags);
89 extern NORETURN void usage_with_options(const char * const *usagestr,
90 const struct option *options);
92 /*----- some often used options -----*/
93 extern int parse_opt_abbrev_cb(const struct option *, const char *, int);
95 #define OPT__VERBOSE(var) OPT_BOOLEAN('v', "verbose", (var), "be verbose")
96 #define OPT__QUIET(var) OPT_BOOLEAN('q', "quiet", (var), "be quiet")
97 #define OPT__DRY_RUN(var) OPT_BOOLEAN('n', "dry-run", (var), "dry run")
98 #define OPT__ABBREV(var) \
99 { OPTION_CALLBACK, 0, "abbrev", (var), "n", \
100 "use <n> digits to display SHA-1s", \
101 PARSE_OPT_OPTARG, &parse_opt_abbrev_cb, 0 }