add strbuf_adddup()
[git/dkf.git] / parse-options.h
blob65bce6eafd19bfedd7d3b0b31ad81625842a7c0b
1 #ifndef PARSE_OPTIONS_H
2 #define PARSE_OPTIONS_H
4 enum parse_opt_type {
5 OPTION_END,
6 OPTION_GROUP,
7 OPTION_BOOLEAN,
8 OPTION_STRING,
9 OPTION_INTEGER,
10 OPTION_CALLBACK,
13 enum parse_opt_flags {
14 PARSE_OPT_KEEP_DASHDASH = 1,
17 enum parse_opt_option_flags {
18 PARSE_OPT_OPTARG = 1,
19 PARSE_OPT_NOARG = 2,
22 struct option;
23 typedef int parse_opt_cb(const struct option *, const char *arg, int unset);
26 * `type`::
27 * holds the type of the option, you must have an OPTION_END last in your
28 * array.
30 * `short_name`::
31 * the character to use as a short option name, '\0' if none.
33 * `long_name`::
34 * the long option name, without the leading dashes, NULL if none.
36 * `value`::
37 * stores pointers to the values to be filled.
39 * `argh`::
40 * token to explain the kind of argument this option wants. Keep it
41 * homogenous across the repository.
43 * `help`::
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.
48 * `flags`::
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
53 * `callback`::
54 * pointer to the callback to use for OPTION_CALLBACK.
56 * `defval`::
57 * default value to fill (*->value) with for PARSE_OPT_OPTARG.
58 * CALLBACKS can use it like they want.
60 struct option {
61 enum parse_opt_type type;
62 int short_name;
63 const char *long_name;
64 void *value;
65 const char *argh;
66 const char *help;
68 int flags;
69 parse_opt_cb *callback;
70 intptr_t defval;
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 }
103 #endif