@matheusbsbs / matheus-bagliano / commits / b0b3a8b666
Fork 0

parse-options: allow git commands to invent new option types

parse-options provides a variety of option behaviors, including OPTION_CALLBACK, which should take care of just about any sane behavior. All supported behaviors obey the following constraint: A --foo option can only accept (and base its behavior on) one argument, which would be the following command-line argument in the "unsticked" form. Alas, some existing git commands have options that do not obey that constraint. For example, update-index --cacheinfo takes three arguments, and update-index --resolve takes all later parameters as arguments. Introduces an OPTION_LOWLEVEL_CALLBACK backdoor to parse-options so such option types can be supported without tempting inventors of other commands through mention in the public API. Commands can set the callback field to a function accepting three arguments: the option parsing context, the option itself, and a flag indicating whether the the option was negated. When the option is encountered, that function is called to take over from get_value(). The return value should be zero for success, -1 for usage errors. Thanks to Stephen Boyd for API guidance. Improved-by: Stephen Boyd <bebarino@gmail.com> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Nieder committed Dec 1, 2010 at 17:32 UTC b0b3a8b666ac9bcab93c9b05ca7de918d7fa18bc
2 files changed +10 -1
parse-options.c
+3
@@ -66,6 +66,9 @@ static int get_value(struct parse_opt_ctx_t *p,
66 return opterror(opt, "takes no value", flags);
67
68 switch (opt->type) {
69 + case OPTION_LOWLEVEL_CALLBACK:
70 + return (*(parse_opt_ll_cb *)opt->callback)(p, opt, unset);
71 +
72 case OPTION_BIT:
73 if (unset)
74 *(int *)opt->value &= ~opt->defval;
parse-options.h
+7 -1
@@ -17,6 +17,7 @@ enum parse_opt_type {
17 OPTION_STRING,
18 OPTION_INTEGER,
19 OPTION_CALLBACK,
20 + OPTION_LOWLEVEL_CALLBACK,
21 OPTION_FILENAME
22 };
23
@@ -43,6 +44,10 @@ enum parse_opt_option_flags {
44 struct option;
45 typedef int parse_opt_cb(const struct option *, const char *arg, int unset);
46
47 +struct parse_opt_ctx_t;
48 +typedef int parse_opt_ll_cb(struct parse_opt_ctx_t *ctx,
49 + const struct option *opt, int unset);
50 +
51 /*
52 * `type`::
53 * holds the type of the option, you must have an OPTION_END last in your
@@ -87,7 +92,8 @@ typedef int parse_opt_cb(const struct option *, const char *arg, int unset);
92 * useful for users of OPTION_NEGBIT.
93 *
94 * `callback`::
90 - * pointer to the callback to use for OPTION_CALLBACK.
95 + * pointer to the callback to use for OPTION_CALLBACK or
96 + * OPTION_LOWLEVEL_CALLBACK.
97 *
98 * `defval`::
99 * default value to fill (*->value) with for PARSE_OPT_OPTARG.