parse-options.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. #ifndef PARSE_OPTIONS_H
  2. #define PARSE_OPTIONS_H
  3. /**
  4. * Refer to Documentation/technical/api-parse-options.txt for the API doc.
  5. */
  6. enum parse_opt_type {
  7. /* special types */
  8. OPTION_END,
  9. OPTION_ARGUMENT,
  10. OPTION_GROUP,
  11. OPTION_NUMBER,
  12. OPTION_ALIAS,
  13. /* options with no arguments */
  14. OPTION_BIT,
  15. OPTION_NEGBIT,
  16. OPTION_BITOP,
  17. OPTION_COUNTUP,
  18. OPTION_SET_INT,
  19. /* options with arguments (usually) */
  20. OPTION_STRING,
  21. OPTION_INTEGER,
  22. OPTION_MAGNITUDE,
  23. OPTION_CALLBACK,
  24. OPTION_LOWLEVEL_CALLBACK,
  25. OPTION_FILENAME
  26. };
  27. enum parse_opt_flags {
  28. PARSE_OPT_KEEP_DASHDASH = 1,
  29. PARSE_OPT_STOP_AT_NON_OPTION = 2,
  30. PARSE_OPT_KEEP_ARGV0 = 4,
  31. PARSE_OPT_KEEP_UNKNOWN = 8,
  32. PARSE_OPT_NO_INTERNAL_HELP = 16,
  33. PARSE_OPT_ONE_SHOT = 32
  34. };
  35. enum parse_opt_option_flags {
  36. PARSE_OPT_OPTARG = 1,
  37. PARSE_OPT_NOARG = 2,
  38. PARSE_OPT_NONEG = 4,
  39. PARSE_OPT_HIDDEN = 8,
  40. PARSE_OPT_LASTARG_DEFAULT = 16,
  41. PARSE_OPT_NODASH = 32,
  42. PARSE_OPT_LITERAL_ARGHELP = 64,
  43. PARSE_OPT_SHELL_EVAL = 256,
  44. PARSE_OPT_NOCOMPLETE = 512,
  45. PARSE_OPT_COMP_ARG = 1024,
  46. PARSE_OPT_CMDMODE = 2048
  47. };
  48. enum parse_opt_result {
  49. PARSE_OPT_COMPLETE = -3,
  50. PARSE_OPT_HELP = -2,
  51. PARSE_OPT_ERROR = -1, /* must be the same as error() */
  52. PARSE_OPT_DONE = 0, /* fixed so that "return 0" works */
  53. PARSE_OPT_NON_OPTION,
  54. PARSE_OPT_UNKNOWN
  55. };
  56. struct option;
  57. typedef int parse_opt_cb(const struct option *, const char *arg, int unset);
  58. struct parse_opt_ctx_t;
  59. typedef enum parse_opt_result parse_opt_ll_cb(struct parse_opt_ctx_t *ctx,
  60. const struct option *opt,
  61. const char *arg, int unset);
  62. /*
  63. * `type`::
  64. * holds the type of the option, you must have an OPTION_END last in your
  65. * array.
  66. *
  67. * `short_name`::
  68. * the character to use as a short option name, '\0' if none.
  69. *
  70. * `long_name`::
  71. * the long option name, without the leading dashes, NULL if none.
  72. *
  73. * `value`::
  74. * stores pointers to the values to be filled.
  75. *
  76. * `argh`::
  77. * token to explain the kind of argument this option wants. Keep it
  78. * homogeneous across the repository. Should be wrapped by N_() for
  79. * translation.
  80. *
  81. * `help`::
  82. * the short help associated to what the option does.
  83. * Must never be NULL (except for OPTION_END).
  84. * OPTION_GROUP uses this pointer to store the group header.
  85. * Should be wrapped by N_() for translation.
  86. *
  87. * `flags`::
  88. * mask of parse_opt_option_flags.
  89. * PARSE_OPT_OPTARG: says that the argument is optional (not for BOOLEANs)
  90. * PARSE_OPT_NOARG: says that this option does not take an argument
  91. * PARSE_OPT_NONEG: says that this option cannot be negated
  92. * PARSE_OPT_HIDDEN: this option is skipped in the default usage, and
  93. * shown only in the full usage.
  94. * PARSE_OPT_LASTARG_DEFAULT: says that this option will take the default
  95. * value if no argument is given when the option
  96. * is last on the command line. If the option is
  97. * not last it will require an argument.
  98. * Should not be used with PARSE_OPT_OPTARG.
  99. * PARSE_OPT_NODASH: this option doesn't start with a dash.
  100. * PARSE_OPT_LITERAL_ARGHELP: says that argh shouldn't be enclosed in brackets
  101. * (i.e. '<argh>') in the help message.
  102. * Useful for options with multiple parameters.
  103. * PARSE_OPT_NOCOMPLETE: by default all visible options are completable
  104. * by git-completion.bash. This option suppresses that.
  105. * PARSE_OPT_COMP_ARG: this option forces to git-completion.bash to
  106. * complete an option as --name= not --name even if
  107. * the option takes optional argument.
  108. *
  109. * `callback`::
  110. * pointer to the callback to use for OPTION_CALLBACK
  111. *
  112. * `defval`::
  113. * default value to fill (*->value) with for PARSE_OPT_OPTARG.
  114. * OPTION_{BIT,SET_INT} store the {mask,integer} to put in the value when met.
  115. * CALLBACKS can use it like they want.
  116. *
  117. * `ll_callback`::
  118. * pointer to the callback to use for OPTION_LOWLEVEL_CALLBACK
  119. *
  120. */
  121. struct option {
  122. enum parse_opt_type type;
  123. int short_name;
  124. const char *long_name;
  125. void *value;
  126. const char *argh;
  127. const char *help;
  128. int flags;
  129. parse_opt_cb *callback;
  130. intptr_t defval;
  131. parse_opt_ll_cb *ll_callback;
  132. intptr_t extra;
  133. };
  134. #define OPT_BIT_F(s, l, v, h, b, f) { OPTION_BIT, (s), (l), (v), NULL, (h), \
  135. PARSE_OPT_NOARG|(f), NULL, (b) }
  136. #define OPT_COUNTUP_F(s, l, v, h, f) { OPTION_COUNTUP, (s), (l), (v), NULL, \
  137. (h), PARSE_OPT_NOARG|(f) }
  138. #define OPT_SET_INT_F(s, l, v, h, i, f) { OPTION_SET_INT, (s), (l), (v), NULL, \
  139. (h), PARSE_OPT_NOARG | (f), NULL, (i) }
  140. #define OPT_BOOL_F(s, l, v, h, f) OPT_SET_INT_F(s, l, v, h, 1, f)
  141. #define OPT_CALLBACK_F(s, l, v, a, h, f, cb) \
  142. { OPTION_CALLBACK, (s), (l), (v), (a), (h), (f), (cb) }
  143. #define OPT_STRING_F(s, l, v, a, h, f) { OPTION_STRING, (s), (l), (v), (a), (h), (f) }
  144. #define OPT_INTEGER_F(s, l, v, h, f) { OPTION_INTEGER, (s), (l), (v), N_("n"), (h), (f) }
  145. #define OPT_END() { OPTION_END }
  146. #define OPT_ARGUMENT(l, v, h) { OPTION_ARGUMENT, 0, (l), (v), NULL, \
  147. (h), PARSE_OPT_NOARG, NULL, 1 }
  148. #define OPT_GROUP(h) { OPTION_GROUP, 0, NULL, NULL, NULL, (h) }
  149. #define OPT_BIT(s, l, v, h, b) OPT_BIT_F(s, l, v, h, b, 0)
  150. #define OPT_BITOP(s, l, v, h, set, clear) { OPTION_BITOP, (s), (l), (v), NULL, (h), \
  151. PARSE_OPT_NOARG|PARSE_OPT_NONEG, NULL, \
  152. (set), NULL, (clear) }
  153. #define OPT_NEGBIT(s, l, v, h, b) { OPTION_NEGBIT, (s), (l), (v), NULL, \
  154. (h), PARSE_OPT_NOARG, NULL, (b) }
  155. #define OPT_COUNTUP(s, l, v, h) OPT_COUNTUP_F(s, l, v, h, 0)
  156. #define OPT_SET_INT(s, l, v, h, i) OPT_SET_INT_F(s, l, v, h, i, 0)
  157. #define OPT_BOOL(s, l, v, h) OPT_BOOL_F(s, l, v, h, 0)
  158. #define OPT_HIDDEN_BOOL(s, l, v, h) { OPTION_SET_INT, (s), (l), (v), NULL, \
  159. (h), PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, NULL, 1}
  160. #define OPT_CMDMODE(s, l, v, h, i) { OPTION_SET_INT, (s), (l), (v), NULL, \
  161. (h), PARSE_OPT_CMDMODE|PARSE_OPT_NOARG|PARSE_OPT_NONEG, NULL, (i) }
  162. #define OPT_INTEGER(s, l, v, h) OPT_INTEGER_F(s, l, v, h, 0)
  163. #define OPT_MAGNITUDE(s, l, v, h) { OPTION_MAGNITUDE, (s), (l), (v), \
  164. N_("n"), (h), PARSE_OPT_NONEG }
  165. #define OPT_STRING(s, l, v, a, h) OPT_STRING_F(s, l, v, a, h, 0)
  166. #define OPT_STRING_LIST(s, l, v, a, h) \
  167. { OPTION_CALLBACK, (s), (l), (v), (a), \
  168. (h), 0, &parse_opt_string_list }
  169. #define OPT_UYN(s, l, v, h) { OPTION_CALLBACK, (s), (l), (v), NULL, \
  170. (h), PARSE_OPT_NOARG, &parse_opt_tertiary }
  171. #define OPT_EXPIRY_DATE(s, l, v, h) \
  172. { OPTION_CALLBACK, (s), (l), (v), N_("expiry-date"),(h), 0, \
  173. parse_opt_expiry_date_cb }
  174. #define OPT_CALLBACK(s, l, v, a, h, f) OPT_CALLBACK_F(s, l, v, a, h, 0, f)
  175. #define OPT_NUMBER_CALLBACK(v, h, f) \
  176. { OPTION_NUMBER, 0, NULL, (v), NULL, (h), \
  177. PARSE_OPT_NOARG | PARSE_OPT_NONEG, (f) }
  178. #define OPT_FILENAME(s, l, v, h) { OPTION_FILENAME, (s), (l), (v), \
  179. N_("file"), (h) }
  180. #define OPT_COLOR_FLAG(s, l, v, h) \
  181. { OPTION_CALLBACK, (s), (l), (v), N_("when"), (h), PARSE_OPT_OPTARG, \
  182. parse_opt_color_flag_cb, (intptr_t)"always" }
  183. #define OPT_NOOP_NOARG(s, l) \
  184. { OPTION_CALLBACK, (s), (l), NULL, NULL, \
  185. N_("no-op (backward compatibility)"), \
  186. PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, parse_opt_noop_cb }
  187. #define OPT_ALIAS(s, l, source_long_name) \
  188. { OPTION_ALIAS, (s), (l), (source_long_name) }
  189. /*
  190. * parse_options() will filter out the processed options and leave the
  191. * non-option arguments in argv[]. argv0 is assumed program name and
  192. * skipped.
  193. *
  194. * usagestr strings should be marked for translation with N_().
  195. *
  196. * Returns the number of arguments left in argv[].
  197. *
  198. * In one-shot mode, argv0 is not a program name, argv[] is left
  199. * untouched and parse_options() returns the number of options
  200. * processed.
  201. */
  202. int parse_options(int argc, const char **argv, const char *prefix,
  203. const struct option *options,
  204. const char * const usagestr[], int flags);
  205. NORETURN void usage_with_options(const char * const *usagestr,
  206. const struct option *options);
  207. NORETURN void usage_msg_opt(const char *msg,
  208. const char * const *usagestr,
  209. const struct option *options);
  210. int optbug(const struct option *opt, const char *reason);
  211. const char *optname(const struct option *opt, int flags);
  212. /*
  213. * Use these assertions for callbacks that expect to be called with NONEG and
  214. * NOARG respectively, and do not otherwise handle the "unset" and "arg"
  215. * parameters.
  216. */
  217. #define BUG_ON_OPT_NEG(unset) do { \
  218. if ((unset)) \
  219. BUG("option callback does not expect negation"); \
  220. } while (0)
  221. #define BUG_ON_OPT_ARG(arg) do { \
  222. if ((arg)) \
  223. BUG("option callback does not expect an argument"); \
  224. } while (0)
  225. /*
  226. * Similar to the assertions above, but checks that "arg" is always non-NULL.
  227. * This assertion also implies BUG_ON_OPT_NEG(), letting you declare both
  228. * assertions in a single line.
  229. */
  230. #define BUG_ON_OPT_NEG_NOARG(unset, arg) do { \
  231. BUG_ON_OPT_NEG(unset); \
  232. if(!(arg)) \
  233. BUG("option callback expects an argument"); \
  234. } while(0)
  235. /*----- incremental advanced APIs -----*/
  236. /*
  237. * It's okay for the caller to consume argv/argc in the usual way.
  238. * Other fields of that structure are private to parse-options and should not
  239. * be modified in any way.
  240. */
  241. struct parse_opt_ctx_t {
  242. const char **argv;
  243. const char **out;
  244. int argc, cpidx, total;
  245. const char *opt;
  246. int flags;
  247. const char *prefix;
  248. const char **alias_groups; /* must be in groups of 3 elements! */
  249. struct option *updated_options;
  250. };
  251. void parse_options_start(struct parse_opt_ctx_t *ctx,
  252. int argc, const char **argv, const char *prefix,
  253. const struct option *options, int flags);
  254. int parse_options_step(struct parse_opt_ctx_t *ctx,
  255. const struct option *options,
  256. const char * const usagestr[]);
  257. int parse_options_end(struct parse_opt_ctx_t *ctx);
  258. struct option *parse_options_dup(const struct option *a);
  259. struct option *parse_options_concat(const struct option *a, const struct option *b);
  260. /*----- some often used options -----*/
  261. int parse_opt_abbrev_cb(const struct option *, const char *, int);
  262. int parse_opt_expiry_date_cb(const struct option *, const char *, int);
  263. int parse_opt_color_flag_cb(const struct option *, const char *, int);
  264. int parse_opt_verbosity_cb(const struct option *, const char *, int);
  265. /* value is struct oid_array* */
  266. int parse_opt_object_name(const struct option *, const char *, int);
  267. /* value is struct object_id* */
  268. int parse_opt_object_id(const struct option *, const char *, int);
  269. int parse_opt_commits(const struct option *, const char *, int);
  270. int parse_opt_commit(const struct option *, const char *, int);
  271. int parse_opt_tertiary(const struct option *, const char *, int);
  272. int parse_opt_string_list(const struct option *, const char *, int);
  273. int parse_opt_noop_cb(const struct option *, const char *, int);
  274. enum parse_opt_result parse_opt_unknown_cb(struct parse_opt_ctx_t *ctx,
  275. const struct option *,
  276. const char *, int);
  277. int parse_opt_passthru(const struct option *, const char *, int);
  278. int parse_opt_passthru_argv(const struct option *, const char *, int);
  279. #define OPT__VERBOSE(var, h) OPT_COUNTUP('v', "verbose", (var), (h))
  280. #define OPT__QUIET(var, h) OPT_COUNTUP('q', "quiet", (var), (h))
  281. #define OPT__VERBOSITY(var) \
  282. { OPTION_CALLBACK, 'v', "verbose", (var), NULL, N_("be more verbose"), \
  283. PARSE_OPT_NOARG, &parse_opt_verbosity_cb, 0 }, \
  284. { OPTION_CALLBACK, 'q', "quiet", (var), NULL, N_("be more quiet"), \
  285. PARSE_OPT_NOARG, &parse_opt_verbosity_cb, 0 }
  286. #define OPT__DRY_RUN(var, h) OPT_BOOL('n', "dry-run", (var), (h))
  287. #define OPT__FORCE(var, h, f) OPT_COUNTUP_F('f', "force", (var), (h), (f))
  288. #define OPT__ABBREV(var) \
  289. { OPTION_CALLBACK, 0, "abbrev", (var), N_("n"), \
  290. N_("use <n> digits to display object names"), \
  291. PARSE_OPT_OPTARG, &parse_opt_abbrev_cb, 0 }
  292. #define OPT__COLOR(var, h) \
  293. OPT_COLOR_FLAG(0, "color", (var), (h))
  294. #define OPT_COLUMN(s, l, v, h) \
  295. { OPTION_CALLBACK, (s), (l), (v), N_("style"), (h), PARSE_OPT_OPTARG, parseopt_column_callback }
  296. #define OPT_PASSTHRU(s, l, v, a, h, f) \
  297. { OPTION_CALLBACK, (s), (l), (v), (a), (h), (f), parse_opt_passthru }
  298. #define OPT_PASSTHRU_ARGV(s, l, v, a, h, f) \
  299. { OPTION_CALLBACK, (s), (l), (v), (a), (h), (f), parse_opt_passthru_argv }
  300. #define _OPT_CONTAINS_OR_WITH(name, variable, help, flag) \
  301. { OPTION_CALLBACK, 0, name, (variable), N_("commit"), (help), \
  302. PARSE_OPT_LASTARG_DEFAULT | flag, \
  303. parse_opt_commits, (intptr_t) "HEAD" \
  304. }
  305. #define OPT_CONTAINS(v, h) _OPT_CONTAINS_OR_WITH("contains", v, h, PARSE_OPT_NONEG)
  306. #define OPT_NO_CONTAINS(v, h) _OPT_CONTAINS_OR_WITH("no-contains", v, h, PARSE_OPT_NONEG)
  307. #define OPT_WITH(v, h) _OPT_CONTAINS_OR_WITH("with", v, h, PARSE_OPT_HIDDEN | PARSE_OPT_NONEG)
  308. #define OPT_WITHOUT(v, h) _OPT_CONTAINS_OR_WITH("without", v, h, PARSE_OPT_HIDDEN | PARSE_OPT_NONEG)
  309. #define OPT_CLEANUP(v) OPT_STRING(0, "cleanup", v, N_("mode"), N_("how to strip spaces and #comments from message"))
  310. #define OPT_PATHSPEC_FROM_FILE(v) OPT_FILENAME(0, "pathspec-from-file", v, N_("read pathspec from file"))
  311. #define OPT_PATHSPEC_FILE_NUL(v) OPT_BOOL(0, "pathspec-file-nul", v, N_("with --pathspec-from-file, pathspec elements are separated with NUL character"))
  312. #define OPT_AUTOSTASH(v) OPT_BOOL(0, "autostash", v, N_("automatically stash/stash pop before and after"))
  313. #endif