Merge branch 'jc/parseopt-command-modes'
[git/raj.git] / parse-options.h
blobf37fc88143f0cc0277b33d55ec92cc43034774cf
1 #ifndef PARSE_OPTIONS_H
2 #define PARSE_OPTIONS_H
4 enum parse_opt_type {
5 /* special types */
6 OPTION_END,
7 OPTION_ARGUMENT,
8 OPTION_GROUP,
9 OPTION_NUMBER,
10 /* options with no arguments */
11 OPTION_BIT,
12 OPTION_NEGBIT,
13 OPTION_COUNTUP,
14 OPTION_SET_INT,
15 OPTION_SET_PTR,
16 OPTION_CMDMODE,
17 /* options with arguments (usually) */
18 OPTION_STRING,
19 OPTION_INTEGER,
20 OPTION_CALLBACK,
21 OPTION_LOWLEVEL_CALLBACK,
22 OPTION_FILENAME
25 /* Deprecated synonym */
26 #define OPTION_BOOLEAN OPTION_COUNTUP
28 enum parse_opt_flags {
29 PARSE_OPT_KEEP_DASHDASH = 1,
30 PARSE_OPT_STOP_AT_NON_OPTION = 2,
31 PARSE_OPT_KEEP_ARGV0 = 4,
32 PARSE_OPT_KEEP_UNKNOWN = 8,
33 PARSE_OPT_NO_INTERNAL_HELP = 16
36 enum parse_opt_option_flags {
37 PARSE_OPT_OPTARG = 1,
38 PARSE_OPT_NOARG = 2,
39 PARSE_OPT_NONEG = 4,
40 PARSE_OPT_HIDDEN = 8,
41 PARSE_OPT_LASTARG_DEFAULT = 16,
42 PARSE_OPT_NODASH = 32,
43 PARSE_OPT_LITERAL_ARGHELP = 64,
44 PARSE_OPT_SHELL_EVAL = 256
47 struct option;
48 typedef int parse_opt_cb(const struct option *, const char *arg, int unset);
50 struct parse_opt_ctx_t;
51 typedef int parse_opt_ll_cb(struct parse_opt_ctx_t *ctx,
52 const struct option *opt, int unset);
55 * `type`::
56 * holds the type of the option, you must have an OPTION_END last in your
57 * array.
59 * `short_name`::
60 * the character to use as a short option name, '\0' if none.
62 * `long_name`::
63 * the long option name, without the leading dashes, NULL if none.
65 * `value`::
66 * stores pointers to the values to be filled.
68 * `argh`::
69 * token to explain the kind of argument this option wants. Keep it
70 * homogeneous across the repository. Should be wrapped by N_() for
71 * translation.
73 * `help`::
74 * the short help associated to what the option does.
75 * Must never be NULL (except for OPTION_END).
76 * OPTION_GROUP uses this pointer to store the group header.
77 * Should be wrapped by N_() for translation.
79 * `flags`::
80 * mask of parse_opt_option_flags.
81 * PARSE_OPT_OPTARG: says that the argument is optional (not for BOOLEANs)
82 * PARSE_OPT_NOARG: says that this option does not take an argument
83 * PARSE_OPT_NONEG: says that this option cannot be negated
84 * PARSE_OPT_HIDDEN: this option is skipped in the default usage, and
85 * shown only in the full usage.
86 * PARSE_OPT_LASTARG_DEFAULT: says that this option will take the default
87 * value if no argument is given when the option
88 * is last on the command line. If the option is
89 * not last it will require an argument.
90 * Should not be used with PARSE_OPT_OPTARG.
91 * PARSE_OPT_NODASH: this option doesn't start with a dash.
92 * PARSE_OPT_LITERAL_ARGHELP: says that argh shouldn't be enclosed in brackets
93 * (i.e. '<argh>') in the help message.
94 * Useful for options with multiple parameters.
96 * `callback`::
97 * pointer to the callback to use for OPTION_CALLBACK or
98 * OPTION_LOWLEVEL_CALLBACK.
100 * `defval`::
101 * default value to fill (*->value) with for PARSE_OPT_OPTARG.
102 * OPTION_{BIT,SET_INT,SET_PTR} store the {mask,integer,pointer} to put in
103 * the value when met.
104 * CALLBACKS can use it like they want.
106 struct option {
107 enum parse_opt_type type;
108 int short_name;
109 const char *long_name;
110 void *value;
111 const char *argh;
112 const char *help;
114 int flags;
115 parse_opt_cb *callback;
116 intptr_t defval;
119 #define OPT_END() { OPTION_END }
120 #define OPT_ARGUMENT(l, h) { OPTION_ARGUMENT, 0, (l), NULL, NULL, \
121 (h), PARSE_OPT_NOARG}
122 #define OPT_GROUP(h) { OPTION_GROUP, 0, NULL, NULL, NULL, (h) }
123 #define OPT_BIT(s, l, v, h, b) { OPTION_BIT, (s), (l), (v), NULL, (h), \
124 PARSE_OPT_NOARG, NULL, (b) }
125 #define OPT_NEGBIT(s, l, v, h, b) { OPTION_NEGBIT, (s), (l), (v), NULL, \
126 (h), PARSE_OPT_NOARG, NULL, (b) }
127 #define OPT_COUNTUP(s, l, v, h) { OPTION_COUNTUP, (s), (l), (v), NULL, \
128 (h), PARSE_OPT_NOARG }
129 #define OPT_SET_INT(s, l, v, h, i) { OPTION_SET_INT, (s), (l), (v), NULL, \
130 (h), PARSE_OPT_NOARG, NULL, (i) }
131 #define OPT_BOOL(s, l, v, h) OPT_SET_INT(s, l, v, h, 1)
132 #define OPT_SET_PTR(s, l, v, h, p) { OPTION_SET_PTR, (s), (l), (v), NULL, \
133 (h), PARSE_OPT_NOARG, NULL, (p) }
134 #define OPT_CMDMODE(s, l, v, h, i) { OPTION_CMDMODE, (s), (l), (v), NULL, \
135 (h), PARSE_OPT_NOARG|PARSE_OPT_NONEG, NULL, (i) }
136 #define OPT_INTEGER(s, l, v, h) { OPTION_INTEGER, (s), (l), (v), N_("n"), (h) }
137 #define OPT_STRING(s, l, v, a, h) { OPTION_STRING, (s), (l), (v), (a), (h) }
138 #define OPT_STRING_LIST(s, l, v, a, h) \
139 { OPTION_CALLBACK, (s), (l), (v), (a), \
140 (h), 0, &parse_opt_string_list }
141 #define OPT_UYN(s, l, v, h) { OPTION_CALLBACK, (s), (l), (v), NULL, \
142 (h), PARSE_OPT_NOARG, &parse_opt_tertiary }
143 #define OPT_DATE(s, l, v, h) \
144 { OPTION_CALLBACK, (s), (l), (v), N_("time"),(h), 0, \
145 parse_opt_approxidate_cb }
146 #define OPT_EXPIRY_DATE(s, l, v, h) \
147 { OPTION_CALLBACK, (s), (l), (v), N_("expiry date"),(h), 0, \
148 parse_opt_expiry_date_cb }
149 #define OPT_CALLBACK(s, l, v, a, h, f) \
150 { OPTION_CALLBACK, (s), (l), (v), (a), (h), 0, (f) }
151 #define OPT_NUMBER_CALLBACK(v, h, f) \
152 { OPTION_NUMBER, 0, NULL, (v), NULL, (h), \
153 PARSE_OPT_NOARG | PARSE_OPT_NONEG, (f) }
154 #define OPT_FILENAME(s, l, v, h) { OPTION_FILENAME, (s), (l), (v), \
155 N_("file"), (h) }
156 #define OPT_COLOR_FLAG(s, l, v, h) \
157 { OPTION_CALLBACK, (s), (l), (v), N_("when"), (h), PARSE_OPT_OPTARG, \
158 parse_opt_color_flag_cb, (intptr_t)"always" }
160 #define OPT_NOOP_NOARG(s, l) \
161 { OPTION_CALLBACK, (s), (l), NULL, NULL, \
162 N_("no-op (backward compatibility)"), \
163 PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, parse_opt_noop_cb }
165 /* Deprecated synonym */
166 #define OPT_BOOLEAN OPT_COUNTUP
168 /* parse_options() will filter out the processed options and leave the
169 * non-option arguments in argv[]. usagestr strings should be marked
170 * for translation with N_().
171 * Returns the number of arguments left in argv[].
173 extern int parse_options(int argc, const char **argv, const char *prefix,
174 const struct option *options,
175 const char * const usagestr[], int flags);
177 extern NORETURN void usage_with_options(const char * const *usagestr,
178 const struct option *options);
180 extern NORETURN void usage_msg_opt(const char *msg,
181 const char * const *usagestr,
182 const struct option *options);
184 extern int optbug(const struct option *opt, const char *reason);
185 extern int opterror(const struct option *opt, const char *reason, int flags);
186 #if defined(__GNUC__) && ! defined(__clang__)
187 #define opterror(o,r,f) (opterror((o),(r),(f)), -1)
188 #endif
190 /*----- incremental advanced APIs -----*/
192 enum {
193 PARSE_OPT_HELP = -1,
194 PARSE_OPT_DONE,
195 PARSE_OPT_NON_OPTION,
196 PARSE_OPT_UNKNOWN
200 * It's okay for the caller to consume argv/argc in the usual way.
201 * Other fields of that structure are private to parse-options and should not
202 * be modified in any way.
204 struct parse_opt_ctx_t {
205 const char **argv;
206 const char **out;
207 int argc, cpidx;
208 const char *opt;
209 int flags;
210 const char *prefix;
213 extern void parse_options_start(struct parse_opt_ctx_t *ctx,
214 int argc, const char **argv, const char *prefix,
215 const struct option *options, int flags);
217 extern int parse_options_step(struct parse_opt_ctx_t *ctx,
218 const struct option *options,
219 const char * const usagestr[]);
221 extern int parse_options_end(struct parse_opt_ctx_t *ctx);
223 extern int parse_options_concat(struct option *dst, size_t, struct option *src);
225 /*----- some often used options -----*/
226 extern int parse_opt_abbrev_cb(const struct option *, const char *, int);
227 extern int parse_opt_approxidate_cb(const struct option *, const char *, int);
228 extern int parse_opt_expiry_date_cb(const struct option *, const char *, int);
229 extern int parse_opt_color_flag_cb(const struct option *, const char *, int);
230 extern int parse_opt_verbosity_cb(const struct option *, const char *, int);
231 extern int parse_opt_with_commit(const struct option *, const char *, int);
232 extern int parse_opt_tertiary(const struct option *, const char *, int);
233 extern int parse_opt_string_list(const struct option *, const char *, int);
234 extern int parse_opt_noop_cb(const struct option *, const char *, int);
236 #define OPT__VERBOSE(var, h) OPT_BOOLEAN('v', "verbose", (var), (h))
237 #define OPT__QUIET(var, h) OPT_BOOLEAN('q', "quiet", (var), (h))
238 #define OPT__VERBOSITY(var) \
239 { OPTION_CALLBACK, 'v', "verbose", (var), NULL, N_("be more verbose"), \
240 PARSE_OPT_NOARG, &parse_opt_verbosity_cb, 0 }, \
241 { OPTION_CALLBACK, 'q', "quiet", (var), NULL, N_("be more quiet"), \
242 PARSE_OPT_NOARG, &parse_opt_verbosity_cb, 0 }
243 #define OPT__DRY_RUN(var, h) OPT_BOOLEAN('n', "dry-run", (var), (h))
244 #define OPT__FORCE(var, h) OPT_BOOLEAN('f', "force", (var), (h))
245 #define OPT__ABBREV(var) \
246 { OPTION_CALLBACK, 0, "abbrev", (var), N_("n"), \
247 N_("use <n> digits to display SHA-1s"), \
248 PARSE_OPT_OPTARG, &parse_opt_abbrev_cb, 0 }
249 #define OPT__COLOR(var, h) \
250 OPT_COLOR_FLAG(0, "color", (var), (h))
251 #define OPT_COLUMN(s, l, v, h) \
252 { OPTION_CALLBACK, (s), (l), (v), N_("style"), (h), PARSE_OPT_OPTARG, parseopt_column_callback }
254 #endif