parse-options: allow git commands to invent new option types
authorJonathan Nieder <jrnieder@gmail.com>
Wed, 1 Dec 2010 23:32:16 +0000 (17:32 -0600)
committerJunio C Hamano <gitster@pobox.com>
Tue, 7 Dec 2010 22:19:32 +0000 (14:19 -0800)
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>
parse-options.c
parse-options.h
index 632c1346c6bc6fd17a516b82771ba607961d3e4f..cd926869166fe28d4c116840be6921de7aa3cbda 100644 (file)
@@ -66,6 +66,9 @@ static int get_value(struct parse_opt_ctx_t *p,
                return opterror(opt, "takes no value", flags);
 
        switch (opt->type) {
+       case OPTION_LOWLEVEL_CALLBACK:
+               return (*(parse_opt_ll_cb *)opt->callback)(p, opt, unset);
+
        case OPTION_BIT:
                if (unset)
                        *(int *)opt->value &= ~opt->defval;
index 5eb499b9923224825c330292f3ac281a4b6fa675..470bb33298bbeba6ae650f926d390233d0ee8d9f 100644 (file)
@@ -17,6 +17,7 @@ enum parse_opt_type {
        OPTION_STRING,
        OPTION_INTEGER,
        OPTION_CALLBACK,
+       OPTION_LOWLEVEL_CALLBACK,
        OPTION_FILENAME
 };
 
@@ -43,6 +44,10 @@ enum parse_opt_option_flags {
 struct option;
 typedef int parse_opt_cb(const struct option *, const char *arg, int unset);
 
+struct parse_opt_ctx_t;
+typedef int parse_opt_ll_cb(struct parse_opt_ctx_t *ctx,
+                               const struct option *opt, int unset);
+
 /*
  * `type`::
  *   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);
  *                             useful for users of OPTION_NEGBIT.
  *
  * `callback`::
- *   pointer to the callback to use for OPTION_CALLBACK.
+ *   pointer to the callback to use for OPTION_CALLBACK or
+ *   OPTION_LOWLEVEL_CALLBACK.
  *
  * `defval`::
  *   default value to fill (*->value) with for PARSE_OPT_OPTARG.