Code

Merge branch 'rr/cvsexportcommit-w'
[git.git] / parse-options.h
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,
11 };
13 enum parse_opt_flags {
14         PARSE_OPT_KEEP_DASHDASH = 1,
15 };
17 enum parse_opt_option_flags {
18         PARSE_OPT_OPTARG  = 1,
19         PARSE_OPT_NOARG   = 2,
20 };
22 struct option;
23 typedef int parse_opt_cb(const struct option *, const char *arg, int unset);
25 /*
26  * `type`::
27  *   holds the type of the option, you must have an OPTION_END last in your
28  *   array.
29  *
30  * `short_name`::
31  *   the character to use as a short option name, '\0' if none.
32  *
33  * `long_name`::
34  *   the long option name, without the leading dashes, NULL if none.
35  *
36  * `value`::
37  *   stores pointers to the values to be filled.
38  *
39  * `argh`::
40  *   token to explain the kind of argument this option wants. Keep it
41  *   homogenous across the repository.
42  *
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.
47  *
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
52  *
53  * `callback`::
54  *   pointer to the callback to use for OPTION_CALLBACK.
55  *
56  * `defval`::
57  *   default value to fill (*->value) with for PARSE_OPT_OPTARG.
58  *   CALLBACKS can use it like they want.
59  */
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;
71 };
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[].
84  */
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