Skip to content

Commit c1debc6

Browse files
nasamuffingitster
authored andcommitted
parse-options: parse into argv_array
parse-options already knows how to read into a string_list, and it knows how to read into an argv_array as a passthrough (that is, including the argument as well as its value). string_list and argv_array serve similar purposes but are somewhat painful to convert between; so, let's teach parse-options to read values of string arguments directly into an argv_array without preserving the argument name. This is useful if collecting generic arguments to pass through to another command, for example, 'git hook run --arg "--quiet" --arg "--format=pretty" some-hook'. The resulting argv_array would contain { "--quiet", "--format=pretty" }. The implementation is based on that of OPT_STRING_LIST. Signed-off-by: Emily Shaffer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d365c9c commit c1debc6

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

Documentation/technical/api-parse-options.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ There are some macros to easily define options:
173173
The string argument is stored as an element in `string_list`.
174174
Use of `--no-option` will clear the list of preceding values.
175175

176+
`OPT_ARGV_ARRAY(short, long, &struct argv_array, arg_str, description)`::
177+
Introduce an option with a string argument.
178+
The string argument is stored as an element in `argv_array`.
179+
Use of `--no-option` will clear the list of preceding values.
180+
176181
`OPT_INTEGER(short, long, &int_var, description)`::
177182
Introduce an option with integer argument.
178183
The integer is put into `int_var`.

parse-options-cb.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,22 @@ int parse_opt_string_list(const struct option *opt, const char *arg, int unset)
205205
return 0;
206206
}
207207

208+
int parse_opt_argv_array(const struct option *opt, const char *arg, int unset)
209+
{
210+
struct argv_array *v = opt->value;
211+
212+
if (unset) {
213+
argv_array_clear(v);
214+
return 0;
215+
}
216+
217+
if (!arg)
218+
return -1;
219+
220+
argv_array_push(v, arg);
221+
return 0;
222+
}
223+
208224
int parse_opt_noop_cb(const struct option *opt, const char *arg, int unset)
209225
{
210226
return 0;

parse-options.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ struct option {
177177
#define OPT_STRING_LIST(s, l, v, a, h) \
178178
{ OPTION_CALLBACK, (s), (l), (v), (a), \
179179
(h), 0, &parse_opt_string_list }
180+
#define OPT_ARGV_ARRAY(s, l, v, a, h) \
181+
{ OPTION_CALLBACK, (s), (l), (v), (a), \
182+
(h), 0, &parse_opt_argv_array }
180183
#define OPT_UYN(s, l, v, h) { OPTION_CALLBACK, (s), (l), (v), NULL, \
181184
(h), PARSE_OPT_NOARG, &parse_opt_tertiary }
182185
#define OPT_EXPIRY_DATE(s, l, v, h) \
@@ -296,6 +299,7 @@ int parse_opt_commits(const struct option *, const char *, int);
296299
int parse_opt_commit(const struct option *, const char *, int);
297300
int parse_opt_tertiary(const struct option *, const char *, int);
298301
int parse_opt_string_list(const struct option *, const char *, int);
302+
int parse_opt_argv_array(const struct option *, const char *, int);
299303
int parse_opt_noop_cb(const struct option *, const char *, int);
300304
enum parse_opt_result parse_opt_unknown_cb(struct parse_opt_ctx_t *ctx,
301305
const struct option *,

0 commit comments

Comments
 (0)