blob: 9b94596e4aa113518d502502a0ca1bf804a60cbc [file] [log] [blame]
Pierre Habouzit4a59fd12007-10-15 01:35:37 +02001#ifndef PARSE_OPTIONS_H
2#define PARSE_OPTIONS_H
3
4enum parse_opt_type {
Pierre Habouzitdb7244b2007-11-07 11:20:27 +01005 /* special types */
Pierre Habouzit4a59fd12007-10-15 01:35:37 +02006 OPTION_END,
Pierre Habouzit580d5bf2008-03-02 11:35:56 +01007 OPTION_ARGUMENT,
Pierre Habouzitd7a38c52007-10-15 01:38:30 +02008 OPTION_GROUP,
René Scharfee0319ff2009-05-07 21:45:08 +02009 OPTION_NUMBER,
Pierre Habouzitdb7244b2007-11-07 11:20:27 +010010 /* options with no arguments */
11 OPTION_BIT,
René Scharfe2f4b97f2009-05-07 21:44:17 +020012 OPTION_NEGBIT,
Junio C Hamanob04ba2b2011-09-27 16:56:49 -070013 OPTION_COUNTUP,
Pierre Habouzitdb7244b2007-11-07 11:20:27 +010014 OPTION_SET_INT,
15 OPTION_SET_PTR,
16 /* options with arguments (usually) */
Pierre Habouzit4a59fd12007-10-15 01:35:37 +020017 OPTION_STRING,
18 OPTION_INTEGER,
Pierre Habouzitffe659f2007-10-15 01:45:45 +020019 OPTION_CALLBACK,
Jonathan Niederb0b3a8b2010-12-01 17:32:16 -060020 OPTION_LOWLEVEL_CALLBACK,
Stephen Boyddf217ed2009-05-23 11:53:13 -070021 OPTION_FILENAME
Pierre Habouzit4a59fd12007-10-15 01:35:37 +020022};
23
Junio C Hamanob04ba2b2011-09-27 16:56:49 -070024/* Deprecated synonym */
25#define OPTION_BOOLEAN OPTION_COUNTUP
26
Pierre Habouzit4a59fd12007-10-15 01:35:37 +020027enum parse_opt_flags {
28 PARSE_OPT_KEEP_DASHDASH = 1,
Johannes Schindelina0ec9d22008-02-29 01:45:09 +000029 PARSE_OPT_STOP_AT_NON_OPTION = 2,
Pierre Habouzita32a4ea2008-06-24 00:31:31 +020030 PARSE_OPT_KEEP_ARGV0 = 4,
René Scharfeb5ce3a52009-03-08 19:12:47 +010031 PARSE_OPT_KEEP_UNKNOWN = 8,
Gary V. Vaughan4b055482010-05-14 09:31:35 +000032 PARSE_OPT_NO_INTERNAL_HELP = 16
Pierre Habouzit4a59fd12007-10-15 01:35:37 +020033};
34
Pierre Habouzitffe659f2007-10-15 01:45:45 +020035enum parse_opt_option_flags {
36 PARSE_OPT_OPTARG = 1,
Pierre Habouzitf481e222007-10-16 00:32:38 +020037 PARSE_OPT_NOARG = 2,
Pierre Habouzitdb7244b2007-11-07 11:20:27 +010038 PARSE_OPT_NONEG = 4,
Pierre Habouzitdd3bf0f2007-11-19 10:21:44 +010039 PARSE_OPT_HIDDEN = 8,
Pierre Habouzit1cc69852008-07-08 12:34:08 +020040 PARSE_OPT_LASTARG_DEFAULT = 16,
René Scharfe51a99492009-05-07 21:45:42 +020041 PARSE_OPT_NODASH = 32,
Stephen Boyd29f25d42009-05-21 00:33:17 -070042 PARSE_OPT_LITERAL_ARGHELP = 64,
Thomas Rast47e9cd22010-06-12 14:57:39 +020043 PARSE_OPT_SHELL_EVAL = 256
Pierre Habouzitffe659f2007-10-15 01:45:45 +020044};
45
46struct option;
47typedef int parse_opt_cb(const struct option *, const char *arg, int unset);
48
Jonathan Niederb0b3a8b2010-12-01 17:32:16 -060049struct parse_opt_ctx_t;
50typedef int parse_opt_ll_cb(struct parse_opt_ctx_t *ctx,
51 const struct option *opt, int unset);
52
Pierre Habouzit9b3beb52007-11-05 13:03:22 +010053/*
54 * `type`::
55 * holds the type of the option, you must have an OPTION_END last in your
56 * array.
57 *
58 * `short_name`::
59 * the character to use as a short option name, '\0' if none.
60 *
61 * `long_name`::
62 * the long option name, without the leading dashes, NULL if none.
63 *
64 * `value`::
65 * stores pointers to the values to be filled.
66 *
67 * `argh`::
68 * token to explain the kind of argument this option wants. Keep it
Nguyễn Thái Ngọc Duy54e6dc72012-05-06 21:23:51 +070069 * homogeneous across the repository. Should be wrapped by N_() for
70 * translation.
Pierre Habouzit9b3beb52007-11-05 13:03:22 +010071 *
72 * `help`::
73 * the short help associated to what the option does.
74 * Must never be NULL (except for OPTION_END).
75 * OPTION_GROUP uses this pointer to store the group header.
Nguyễn Thái Ngọc Duy54e6dc72012-05-06 21:23:51 +070076 * Should be wrapped by N_() for translation.
Pierre Habouzit9b3beb52007-11-05 13:03:22 +010077 *
78 * `flags`::
79 * mask of parse_opt_option_flags.
Mike Ralphson3ea3c212009-04-17 19:13:30 +010080 * PARSE_OPT_OPTARG: says that the argument is optional (not for BOOLEANs)
Jonathan Niederef45e4d2010-08-22 21:56:38 +053081 * PARSE_OPT_NOARG: says that this option does not take an argument
Pierre Habouzitdb7244b2007-11-07 11:20:27 +010082 * PARSE_OPT_NONEG: says that this option cannot be negated
René Scharfe51a99492009-05-07 21:45:42 +020083 * PARSE_OPT_HIDDEN: this option is skipped in the default usage, and
84 * shown only in the full usage.
Stephen Boyde169b972009-06-07 16:39:15 -070085 * PARSE_OPT_LASTARG_DEFAULT: says that this option will take the default
86 * value if no argument is given when the option
87 * is last on the command line. If the option is
88 * not last it will require an argument.
89 * Should not be used with PARSE_OPT_OPTARG.
René Scharfe51a99492009-05-07 21:45:42 +020090 * PARSE_OPT_NODASH: this option doesn't start with a dash.
Stephen Boyd29f25d42009-05-21 00:33:17 -070091 * PARSE_OPT_LITERAL_ARGHELP: says that argh shouldn't be enclosed in brackets
92 * (i.e. '<argh>') in the help message.
93 * Useful for options with multiple parameters.
Pierre Habouzit9b3beb52007-11-05 13:03:22 +010094 *
95 * `callback`::
Jonathan Niederb0b3a8b2010-12-01 17:32:16 -060096 * pointer to the callback to use for OPTION_CALLBACK or
97 * OPTION_LOWLEVEL_CALLBACK.
Pierre Habouzit9b3beb52007-11-05 13:03:22 +010098 *
99 * `defval`::
100 * default value to fill (*->value) with for PARSE_OPT_OPTARG.
Pierre Habouzitdb7244b2007-11-07 11:20:27 +0100101 * OPTION_{BIT,SET_INT,SET_PTR} store the {mask,integer,pointer} to put in
102 * the value when met.
Pierre Habouzit9b3beb52007-11-05 13:03:22 +0100103 * CALLBACKS can use it like they want.
104 */
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200105struct option {
106 enum parse_opt_type type;
107 int short_name;
108 const char *long_name;
109 void *value;
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200110 const char *argh;
111 const char *help;
Pierre Habouzitffe659f2007-10-15 01:45:45 +0200112
113 int flags;
114 parse_opt_cb *callback;
Pierre Habouzitffe659f2007-10-15 01:45:45 +0200115 intptr_t defval;
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200116};
117
118#define OPT_END() { OPTION_END }
Stephen Boyd34aec9f2009-06-04 16:43:57 -0700119#define OPT_ARGUMENT(l, h) { OPTION_ARGUMENT, 0, (l), NULL, NULL, \
120 (h), PARSE_OPT_NOARG}
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200121#define OPT_GROUP(h) { OPTION_GROUP, 0, NULL, NULL, NULL, (h) }
Stephen Boyd34aec9f2009-06-04 16:43:57 -0700122#define OPT_BIT(s, l, v, h, b) { OPTION_BIT, (s), (l), (v), NULL, (h), \
123 PARSE_OPT_NOARG, NULL, (b) }
124#define OPT_NEGBIT(s, l, v, h, b) { OPTION_NEGBIT, (s), (l), (v), NULL, \
125 (h), PARSE_OPT_NOARG, NULL, (b) }
Junio C Hamanob04ba2b2011-09-27 16:56:49 -0700126#define OPT_COUNTUP(s, l, v, h) { OPTION_COUNTUP, (s), (l), (v), NULL, \
Stephen Boyd34aec9f2009-06-04 16:43:57 -0700127 (h), PARSE_OPT_NOARG }
128#define OPT_SET_INT(s, l, v, h, i) { OPTION_SET_INT, (s), (l), (v), NULL, \
129 (h), PARSE_OPT_NOARG, NULL, (i) }
Junio C Hamanob04ba2b2011-09-27 16:56:49 -0700130#define OPT_BOOL(s, l, v, h) OPT_SET_INT(s, l, v, h, 1)
Stephen Boyd34aec9f2009-06-04 16:43:57 -0700131#define OPT_SET_PTR(s, l, v, h, p) { OPTION_SET_PTR, (s), (l), (v), NULL, \
132 (h), PARSE_OPT_NOARG, NULL, (p) }
Nguyễn Thái Ngọc Duy54e6dc72012-05-06 21:23:51 +0700133#define OPT_INTEGER(s, l, v, h) { OPTION_INTEGER, (s), (l), (v), N_("n"), (h) }
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200134#define OPT_STRING(s, l, v, a, h) { OPTION_STRING, (s), (l), (v), (a), (h) }
Jeff Kingc8ba1632011-06-09 11:55:23 -0400135#define OPT_STRING_LIST(s, l, v, a, h) \
136 { OPTION_CALLBACK, (s), (l), (v), (a), \
137 (h), 0, &parse_opt_string_list }
Junio C Hamanocb6020b2009-12-04 00:20:48 -0800138#define OPT_UYN(s, l, v, h) { OPTION_CALLBACK, (s), (l), (v), NULL, \
139 (h), PARSE_OPT_NOARG, &parse_opt_tertiary }
Michele Ballabio1f4a7112008-03-24 15:02:21 +0100140#define OPT_DATE(s, l, v, h) \
Nguyễn Thái Ngọc Duy54e6dc72012-05-06 21:23:51 +0700141 { OPTION_CALLBACK, (s), (l), (v), N_("time"),(h), 0, \
Michele Ballabio1f4a7112008-03-24 15:02:21 +0100142 parse_opt_approxidate_cb }
Junio C Hamano27ec3942013-04-25 11:13:49 -0700143#define OPT_EXPIRY_DATE(s, l, v, h) \
144 { OPTION_CALLBACK, (s), (l), (v), N_("expiry date"),(h), 0, \
145 parse_opt_expiry_date_cb }
Pierre Habouzitffe659f2007-10-15 01:45:45 +0200146#define OPT_CALLBACK(s, l, v, a, h, f) \
147 { OPTION_CALLBACK, (s), (l), (v), (a), (h), 0, (f) }
René Scharfee0319ff2009-05-07 21:45:08 +0200148#define OPT_NUMBER_CALLBACK(v, h, f) \
149 { OPTION_NUMBER, 0, NULL, (v), NULL, (h), \
150 PARSE_OPT_NOARG | PARSE_OPT_NONEG, (f) }
Stephen Boyddf217ed2009-05-23 11:53:13 -0700151#define OPT_FILENAME(s, l, v, h) { OPTION_FILENAME, (s), (l), (v), \
Nguyễn Thái Ngọc Duy54e6dc72012-05-06 21:23:51 +0700152 N_("file"), (h) }
Mark Lodato73e9da02010-02-16 23:55:58 -0500153#define OPT_COLOR_FLAG(s, l, v, h) \
Nguyễn Thái Ngọc Duy54e6dc72012-05-06 21:23:51 +0700154 { OPTION_CALLBACK, (s), (l), (v), N_("when"), (h), PARSE_OPT_OPTARG, \
Mark Lodato73e9da02010-02-16 23:55:58 -0500155 parse_opt_color_flag_cb, (intptr_t)"always" }
156
René Scharfe6acec032011-09-28 19:44:30 +0200157#define OPT_NOOP_NOARG(s, l) \
158 { OPTION_CALLBACK, (s), (l), NULL, NULL, \
Nguyễn Thái Ngọc Duy54e6dc72012-05-06 21:23:51 +0700159 N_("no-op (backward compatibility)"), \
René Scharfe6acec032011-09-28 19:44:30 +0200160 PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, parse_opt_noop_cb }
161
Junio C Hamanob04ba2b2011-09-27 16:56:49 -0700162/* Deprecated synonym */
163#define OPT_BOOLEAN OPT_COUNTUP
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200164
165/* parse_options() will filter out the processed options and leave the
Nguyễn Thái Ngọc Duy54e6dc72012-05-06 21:23:51 +0700166 * non-option arguments in argv[]. usagestr strings should be marked
167 * for translation with N_().
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200168 * Returns the number of arguments left in argv[].
169 */
Stephen Boyd37782922009-05-23 11:53:12 -0700170extern int parse_options(int argc, const char **argv, const char *prefix,
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200171 const struct option *options,
Pierre Habouzitd7a38c52007-10-15 01:38:30 +0200172 const char * const usagestr[], int flags);
173
174extern NORETURN void usage_with_options(const char * const *usagestr,
175 const struct option *options);
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200176
Christian Couder451bb212009-02-02 06:12:58 +0100177extern NORETURN void usage_msg_opt(const char *msg,
178 const char * const *usagestr,
179 const struct option *options);
180
Dmitry Ivankov1f275b72011-08-11 15:15:37 +0600181extern int optbug(const struct option *opt, const char *reason);
182extern int opterror(const struct option *opt, const char *reason, int flags);
Eric Sunshinea3bc3d02013-08-09 05:06:17 -0400183#if defined(__GNUC__) && ! defined(__clang__)
Jeff Kinga469a102012-12-15 12:42:10 -0500184#define opterror(o,r,f) (opterror((o),(r),(f)), -1)
185#endif
186
Mike Ralphson3ea3c212009-04-17 19:13:30 +0100187/*----- incremental advanced APIs -----*/
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200188
Pierre Habouzitee68b872008-06-23 22:28:04 +0200189enum {
190 PARSE_OPT_HELP = -1,
191 PARSE_OPT_DONE,
Jonathan Nieder979240f2010-12-01 17:32:55 -0600192 PARSE_OPT_NON_OPTION,
Gary V. Vaughan4b055482010-05-14 09:31:35 +0000193 PARSE_OPT_UNKNOWN
Pierre Habouzitee68b872008-06-23 22:28:04 +0200194};
195
Pierre Habouzit26141b52008-06-23 22:55:11 +0200196/*
197 * It's okay for the caller to consume argv/argc in the usual way.
198 * Other fields of that structure are private to parse-options and should not
199 * be modified in any way.
200 */
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200201struct parse_opt_ctx_t {
202 const char **argv;
203 const char **out;
204 int argc, cpidx;
205 const char *opt;
206 int flags;
Stephen Boyd37782922009-05-23 11:53:12 -0700207 const char *prefix;
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200208};
209
210extern void parse_options_start(struct parse_opt_ctx_t *ctx,
Stephen Boyd37782922009-05-23 11:53:12 -0700211 int argc, const char **argv, const char *prefix,
Stephen Boyd9ca11692010-12-05 23:57:42 -0800212 const struct option *options, int flags);
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200213
Pierre Habouzitff43ec32008-06-23 22:38:58 +0200214extern int parse_options_step(struct parse_opt_ctx_t *ctx,
215 const struct option *options,
216 const char * const usagestr[]);
217
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200218extern int parse_options_end(struct parse_opt_ctx_t *ctx);
219
Junio C Hamano8b74d752010-03-06 21:34:40 +0100220extern int parse_options_concat(struct option *dst, size_t, struct option *src);
Pierre Habouzit7e7bbcb2008-06-23 21:59:37 +0200221
Pierre Habouzit0ce865b2007-10-14 11:05:12 +0200222/*----- some often used options -----*/
223extern int parse_opt_abbrev_cb(const struct option *, const char *, int);
Michele Ballabio1f4a7112008-03-24 15:02:21 +0100224extern int parse_opt_approxidate_cb(const struct option *, const char *, int);
Junio C Hamano27ec3942013-04-25 11:13:49 -0700225extern int parse_opt_expiry_date_cb(const struct option *, const char *, int);
Mark Lodato73e9da02010-02-16 23:55:58 -0500226extern int parse_opt_color_flag_cb(const struct option *, const char *, int);
Tuncer Ayaz7f87aff2008-11-15 01:14:24 +0100227extern int parse_opt_verbosity_cb(const struct option *, const char *, int);
Jake Goulding269defd2009-01-26 09:13:23 -0500228extern int parse_opt_with_commit(const struct option *, const char *, int);
Junio C Hamanocb6020b2009-12-04 00:20:48 -0800229extern int parse_opt_tertiary(const struct option *, const char *, int);
Jeff Kingc8ba1632011-06-09 11:55:23 -0400230extern int parse_opt_string_list(const struct option *, const char *, int);
René Scharfe6acec032011-09-28 19:44:30 +0200231extern int parse_opt_noop_cb(const struct option *, const char *, int);
Pierre Habouzit0ce865b2007-10-14 11:05:12 +0200232
René Scharfefd038812010-11-08 18:56:39 +0100233#define OPT__VERBOSE(var, h) OPT_BOOLEAN('v', "verbose", (var), (h))
René Scharfed52ee6e2010-11-08 19:06:54 +0100234#define OPT__QUIET(var, h) OPT_BOOLEAN('q', "quiet", (var), (h))
Tuncer Ayaz7f87aff2008-11-15 01:14:24 +0100235#define OPT__VERBOSITY(var) \
Nguyễn Thái Ngọc Duy54e6dc72012-05-06 21:23:51 +0700236 { OPTION_CALLBACK, 'v', "verbose", (var), NULL, N_("be more verbose"), \
Tuncer Ayaz7f87aff2008-11-15 01:14:24 +0100237 PARSE_OPT_NOARG, &parse_opt_verbosity_cb, 0 }, \
Nguyễn Thái Ngọc Duy54e6dc72012-05-06 21:23:51 +0700238 { OPTION_CALLBACK, 'q', "quiet", (var), NULL, N_("be more quiet"), \
Tuncer Ayaz7f87aff2008-11-15 01:14:24 +0100239 PARSE_OPT_NOARG, &parse_opt_verbosity_cb, 0 }
René Scharfee21adb82010-11-08 18:58:51 +0100240#define OPT__DRY_RUN(var, h) OPT_BOOLEAN('n', "dry-run", (var), (h))
René Scharfe76946b72010-11-08 19:01:54 +0100241#define OPT__FORCE(var, h) OPT_BOOLEAN('f', "force", (var), (h))
Pierre Habouzit0ce865b2007-10-14 11:05:12 +0200242#define OPT__ABBREV(var) \
Nguyễn Thái Ngọc Duy54e6dc72012-05-06 21:23:51 +0700243 { OPTION_CALLBACK, 0, "abbrev", (var), N_("n"), \
244 N_("use <n> digits to display SHA-1s"), \
Pierre Habouzit0ce865b2007-10-14 11:05:12 +0200245 PARSE_OPT_OPTARG, &parse_opt_abbrev_cb, 0 }
Mark Lodato73e9da02010-02-16 23:55:58 -0500246#define OPT__COLOR(var, h) \
247 OPT_COLOR_FLAG(0, "color", (var), (h))
Nguyễn Thái Ngọc Duy7e29b822012-04-21 11:44:32 +0700248#define OPT_COLUMN(s, l, v, h) \
Nguyễn Thái Ngọc Duya054e042012-08-20 19:31:50 +0700249 { OPTION_CALLBACK, (s), (l), (v), N_("style"), (h), PARSE_OPT_OPTARG, parseopt_column_callback }
Pierre Habouzit0ce865b2007-10-14 11:05:12 +0200250
Pierre Habouzit4a59fd12007-10-15 01:35:37 +0200251#endif