Subject: Re: [PATCH v2 01/19] parse-options-cb: implement parse_opt_pass_strbuf()

From: Junio C Hamano <[email-masked]>
Date: Tue, 09 Jun 2015 16:11:39 -0700

Paul Tan <[email-masked]> writes:

> diff --git a/parse-options-cb.c b/parse-options-cb.c
> index be8c413..5b1dbcf 100644
> --- a/parse-options-cb.c
> +++ b/parse-options-cb.c
> @@ -134,3 +134,32 @@ int parse_opt_noop_cb(const struct option *opt, const char *arg, int unset)
>  {
>  	return 0;
>  }
> +
> +/**
> + * For an option opt, recreates the command-line option in opt->value which
> + * must be an strbuf. This is useful when we need to pass the command-line
> + * option to another command.
> + */

This is to be used to create a single argument, not an entire
command line to be executed via run_command() or a shell, right?
It wasn't very clear from the above command.

If the same option is given more than once, the earlier ones are
discarded and the last one wins.  That may be OK for your expected
callers, and I do not think this needs to have an option to instead
accumulate them, but it needs to be made clear in the comment and
the API documentation for future developers who want to use the
parse-options API.

> +int parse_opt_pass_strbuf(const struct option *opt, const char *arg, int unset)
> +{
> +	struct strbuf *sb = opt->value;
> +
> +	strbuf_reset(sb);
> +
> +	if (opt->long_name) {
> +		strbuf_addstr(sb, unset ? "--no-" : "--");
> +		strbuf_addstr(sb, opt->long_name);
> +		if (arg) {
> +			strbuf_addch(sb, '=');
> +			strbuf_addstr(sb, arg);
> +		}
> +	} else if (opt->short_name && !unset) {
> +		strbuf_addch(sb, '-');
> +		strbuf_addch(sb, opt->short_name);
> +		if (arg)
> +			strbuf_addstr(sb, arg);
> +	} else
> +		return -1;
> +
> +	return 0;
> +}
> diff --git a/parse-options.h b/parse-options.h
> index c71e9da..1d21398 100644
> --- a/parse-options.h
> +++ b/parse-options.h
> @@ -224,6 +224,7 @@ extern int parse_opt_with_commit(const struct option *, const char *, int);
>  extern int parse_opt_tertiary(const struct option *, const char *, int);
>  extern int parse_opt_string_list(const struct option *, const char *, int);
>  extern int parse_opt_noop_cb(const struct option *, const char *, int);
> +extern int parse_opt_pass_strbuf(const struct option *, const char *, int);
>  
>  #define OPT__VERBOSE(var, h)  OPT_COUNTUP('v', "verbose", (var), (h))
>  #define OPT__QUIET(var, h)    OPT_COUNTUP('q', "quiet",   (var), (h))