Some better parse-options documentation.
[git/git.git] / parse-options.h
CommitLineData
4a59fd13
PH
1#ifndef PARSE_OPTIONS_H
2#define PARSE_OPTIONS_H
3
4enum parse_opt_type {
5 OPTION_END,
d7a38c54 6 OPTION_GROUP,
4a59fd13
PH
7 OPTION_BOOLEAN,
8 OPTION_STRING,
9 OPTION_INTEGER,
ffe659f9 10 OPTION_CALLBACK,
4a59fd13
PH
11};
12
13enum parse_opt_flags {
14 PARSE_OPT_KEEP_DASHDASH = 1,
15};
16
ffe659f9
PH
17enum parse_opt_option_flags {
18 PARSE_OPT_OPTARG = 1,
f481e22a 19 PARSE_OPT_NOARG = 2,
ffe659f9
PH
20};
21
22struct option;
23typedef int parse_opt_cb(const struct option *, const char *arg, int unset);
24
9b3beb58
PH
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 */
4a59fd13
PH
60struct option {
61 enum parse_opt_type type;
62 int short_name;
63 const char *long_name;
64 void *value;
d7a38c54
PH
65 const char *argh;
66 const char *help;
ffe659f9
PH
67
68 int flags;
69 parse_opt_cb *callback;
ffe659f9 70 intptr_t defval;
4a59fd13
PH
71};
72
73#define OPT_END() { OPTION_END }
d7a38c54
PH
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) }
ffe659f9
PH
78#define OPT_CALLBACK(s, l, v, a, h, f) \
79 { OPTION_CALLBACK, (s), (l), (v), (a), (h), 0, (f) }
4a59fd13
PH
80
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 */
85extern int parse_options(int argc, const char **argv,
86 const struct option *options,
d7a38c54
PH
87 const char * const usagestr[], int flags);
88
89extern NORETURN void usage_with_options(const char * const *usagestr,
90 const struct option *options);
4a59fd13 91
0ce865b1
PH
92/*----- some often used options -----*/
93extern int parse_opt_abbrev_cb(const struct option *, const char *, int);
94
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 }
102
4a59fd13 103#endif