parse-options-cb: implement parse_opt_passthru_argv()

Certain git commands, such as git-pull, are simply wrappers around other git commands like git-fetch, git-merge and git-rebase. As such, these wrapper commands will typically need to "pass through" command-line options of the commands they wrap. Implement the parse_opt_passthru_argv() parse-options callback, which will reconstruct all the provided command-line options into an argv_array, such that it can be passed to another git command. This is useful for passing command-line options that can be specified multiple times. Helped-by: Stefan Beller <sbeller@google.com> Signed-off-by: Paul Tan <pyokagan@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Paul Tan committed Jun 14, 2015 at 16:41 UTC ffad85c599307441323de565c3fafde227e04a8f
3 files changed +29
Documentation/technical/api-parse-options.txt
+6
@@ -219,6 +219,12 @@ There are some macros to easily define options:
219 will be overwritten, so this should only be used for options where
220 the last one specified on the command line wins.
221
222 +`OPT_PASSTHRU_ARGV(short, long, &argv_array_var, arg_str, description, flags)`::
223 + Introduce an option where all instances of it on the command-line will
224 + be reconstructed into an argv_array. This is useful when you need to
225 + pass the command-line option, which can be specified multiple times,
226 + to another command.
227 +
228
229 The last element of the array must be `OPT_END()`.
230
parse-options-cb.c
+20
@@ -4,6 +4,7 @@
4 #include "commit.h"
5 #include "color.h"
6 #include "string-list.h"
7 +#include "argv-array.h"
8
9 /*----- some often used options -----*/
10
@@ -183,3 +184,22 @@ int parse_opt_passthru(const struct option *opt, const char *arg, int unset)
184
185 return 0;
186 }
187 +
188 +/**
189 + * For an option opt, recreate the command-line option, appending it to
190 + * opt->value which must be a argv_array. This is useful when we need to pass
191 + * the command-line option, which can be specified multiple times, to another
192 + * command.
193 + */
194 +int parse_opt_passthru_argv(const struct option *opt, const char *arg, int unset)
195 +{
196 + static struct strbuf sb = STRBUF_INIT;
197 + struct argv_array *opt_value = opt->value;
198 +
199 + if (recreate_opt(&sb, opt, arg, unset) < 0)
200 + return -1;
201 +
202 + argv_array_push(opt_value, sb.buf);
203 +
204 + return 0;
205 +}
parse-options.h
+3
@@ -225,6 +225,7 @@ extern int parse_opt_tertiary(const struct option *, const char *, int);
225 extern int parse_opt_string_list(const struct option *, const char *, int);
226 extern int parse_opt_noop_cb(const struct option *, const char *, int);
227 extern int parse_opt_passthru(const struct option *, const char *, int);
228 +extern int parse_opt_passthru_argv(const struct option *, const char *, int);
229
230 #define OPT__VERBOSE(var, h) OPT_COUNTUP('v', "verbose", (var), (h))
231 #define OPT__QUIET(var, h) OPT_COUNTUP('q', "quiet", (var), (h))
@@ -245,5 +246,7 @@ extern int parse_opt_passthru(const struct option *, const char *, int);
246 { OPTION_CALLBACK, (s), (l), (v), N_("style"), (h), PARSE_OPT_OPTARG, parseopt_column_callback }
247 #define OPT_PASSTHRU(s, l, v, a, h, f) \
248 { OPTION_CALLBACK, (s), (l), (v), (a), (h), (f), parse_opt_passthru }
249 +#define OPT_PASSTHRU_ARGV(s, l, v, a, h, f) \
250 + { OPTION_CALLBACK, (s), (l), (v), (a), (h), (f), parse_opt_passthru_argv }
251
252 #endif