Re: [PATCH/GSoC] parse-options: Add a new nousage opt

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This is being discussed in the "Re: "git tag --contains <id>" is too
chatty, if <id> is invalid" thread.
$gmane/289312

On Sun, Mar 20, 2016 at 12:16 PM, Chirayu Desai <chirayudesai1@xxxxxxxxx> wrote:
> * To show only error text on an error instead of the full usage
> * Currently used only by commands with options "--with" or "--contains",
>   such as 'tag', 'branch', 'for-each-ref'.
>
> It now prints only
>   $ git tag --contains qq
>   error: malformed object name qq
> instead of the full usage text after the error text.
>
> TODO: Add tests
> ---
>  parse-options-cb.c | 12 ++++++++----
>  parse-options.c    |  5 +++++
>  parse-options.h    |  6 ++++--
>  3 files changed, 17 insertions(+), 6 deletions(-)
>
> diff --git a/parse-options-cb.c b/parse-options-cb.c
> index 239898d946..ac2ea4d674 100644
> --- a/parse-options-cb.c
> +++ b/parse-options-cb.c
> @@ -85,11 +85,15 @@ int parse_opt_commits(const struct option *opt, const char *arg, int unset)
>
>         if (!arg)
>                 return -1;
> -       if (get_sha1(arg, sha1))
> -               return error("malformed object name %s", arg);
> +       if (get_sha1(arg, sha1)) {
> +               error("malformed object name %s", arg);
> +               return -3;
> +       }
>         commit = lookup_commit_reference(sha1);
> -       if (!commit)
> -               return error("no such commit %s", arg);
> +       if (!commit) {
> +               error("no such commit %s", arg);
> +               return -3;
> +       }
>         commit_list_insert(commit, opt->value);
>         return 0;
>  }
> diff --git a/parse-options.c b/parse-options.c
> index 47a9192060..d136c1afd0 100644
> --- a/parse-options.c
> +++ b/parse-options.c
> @@ -158,6 +158,9 @@ static int get_value(struct parse_opt_ctx_t *p,
>                         return (*opt->callback)(opt, NULL, 0) ? (-1) : 0;
>                 if (get_arg(p, opt, flags, &arg))
>                         return -1;
> +               if (opt->flags & PARSE_OPT_NOUSAGE) {
> +                       return (*opt->callback)(opt, arg, 0);
> +               }
>                 return (*opt->callback)(opt, arg, 0) ? (-1) : 0;
>
>         case OPTION_INTEGER:
> @@ -504,6 +507,8 @@ int parse_options_step(struct parse_opt_ctx_t *ctx,
>                         goto show_usage_error;
>                 case -2:
>                         goto unknown;
> +               case -3:
> +                       return PARSE_OPT_DONE;
>                 }
>                 continue;
>  unknown:
> diff --git a/parse-options.h b/parse-options.h
> index ea4af92a51..628e34c5af 100644
> --- a/parse-options.h
> +++ b/parse-options.h
> @@ -38,7 +38,8 @@ enum parse_opt_option_flags {
>         PARSE_OPT_LASTARG_DEFAULT = 16,
>         PARSE_OPT_NODASH = 32,
>         PARSE_OPT_LITERAL_ARGHELP = 64,
> -       PARSE_OPT_SHELL_EVAL = 256
> +       PARSE_OPT_SHELL_EVAL = 256,
> +       PARSE_OPT_NOUSAGE = 512
Perhaps a _ON_ERR should be appended at the end to make it clearer?
>  };
>
>  struct option;
> @@ -89,6 +90,7 @@ typedef int parse_opt_ll_cb(struct parse_opt_ctx_t *ctx,
>   *   PARSE_OPT_LITERAL_ARGHELP: says that argh shouldn't be enclosed in brackets
>   *                             (i.e. '<argh>') in the help message.
>   *                             Useful for options with multiple parameters.
> + *   PARSE_OPT_NOUSAGE: do not print usage / help on error.
>   *
>   * `callback`::
>   *   pointer to the callback to use for OPTION_CALLBACK or
> @@ -254,7 +256,7 @@ extern int parse_opt_passthru_argv(const struct option *, const char *, int);
>         { OPTION_CALLBACK, (s), (l), (v), (a), (h), (f), parse_opt_passthru_argv }
>  #define _OPT_CONTAINS_OR_WITH(name, variable, help, flag) \
>         { OPTION_CALLBACK, 0, name, (variable), N_("commit"), (help), \
> -         PARSE_OPT_LASTARG_DEFAULT | flag, \
> +         PARSE_OPT_LASTARG_DEFAULT | PARSE_OPT_NOUSAGE | flag, \
>           parse_opt_commits, (intptr_t) "HEAD" \
>         }
>  #define OPT_CONTAINS(v, h) _OPT_CONTAINS_OR_WITH("contains", v, h, 0)
> --
> 2.7.4
>
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]