Skip to content

Commit c8ba163

Browse files
peffgitster
authored andcommitted
parse-options: add OPT_STRING_LIST helper
This just adds repeated invocations of an option to a list of strings. Using the "--no-<var>" form will reset the list to empty. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f77bcca commit c8ba163

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

parse-options.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "cache.h"
44
#include "commit.h"
55
#include "color.h"
6+
#include "string-list.h"
67

78
static int parse_options_usage(struct parse_opt_ctx_t *ctx,
89
const char * const *usagestr,
@@ -687,3 +688,19 @@ int parse_options_concat(struct option *dst, size_t dst_size, struct option *src
687688
}
688689
return -1;
689690
}
691+
692+
int parse_opt_string_list(const struct option *opt, const char *arg, int unset)
693+
{
694+
struct string_list *v = opt->value;
695+
696+
if (unset) {
697+
string_list_clear(v, 0);
698+
return 0;
699+
}
700+
701+
if (!arg)
702+
return -1;
703+
704+
string_list_append(v, xstrdup(arg));
705+
return 0;
706+
}

parse-options.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ struct option {
130130
(h), PARSE_OPT_NOARG, NULL, (p) }
131131
#define OPT_INTEGER(s, l, v, h) { OPTION_INTEGER, (s), (l), (v), "n", (h) }
132132
#define OPT_STRING(s, l, v, a, h) { OPTION_STRING, (s), (l), (v), (a), (h) }
133+
#define OPT_STRING_LIST(s, l, v, a, h) \
134+
{ OPTION_CALLBACK, (s), (l), (v), (a), \
135+
(h), 0, &parse_opt_string_list }
133136
#define OPT_UYN(s, l, v, h) { OPTION_CALLBACK, (s), (l), (v), NULL, \
134137
(h), PARSE_OPT_NOARG, &parse_opt_tertiary }
135138
#define OPT_DATE(s, l, v, h) \
@@ -204,6 +207,7 @@ extern int parse_opt_color_flag_cb(const struct option *, const char *, int);
204207
extern int parse_opt_verbosity_cb(const struct option *, const char *, int);
205208
extern int parse_opt_with_commit(const struct option *, const char *, int);
206209
extern int parse_opt_tertiary(const struct option *, const char *, int);
210+
extern int parse_opt_string_list(const struct option *, const char *, int);
207211

208212
#define OPT__VERBOSE(var, h) OPT_BOOLEAN('v', "verbose", (var), (h))
209213
#define OPT__QUIET(var, h) OPT_BOOLEAN('q', "quiet", (var), (h))

t/t0040-parse-options.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ String options
2828
--st <st> get another string (pervert ordering)
2929
-o <str> get another string
3030
--default-string set string to default
31+
--list <str> add str to list
3132
3233
Magic arguments
3334
--quux means --quux
@@ -337,4 +338,20 @@ test_expect_success 'negation of OPT_NONEG flags is not ambiguous' '
337338
test_cmp expect output
338339
'
339340

341+
cat >>expect <<'EOF'
342+
list: foo
343+
list: bar
344+
list: baz
345+
EOF
346+
test_expect_success '--list keeps list of strings' '
347+
test-parse-options --list foo --list=bar --list=baz >output &&
348+
test_cmp expect output
349+
'
350+
351+
test_expect_success '--no-list resets list' '
352+
test-parse-options --list=other --list=irrelevant --list=options \
353+
--no-list --list=foo --list=bar --list=baz >output &&
354+
test_cmp expect output
355+
'
356+
340357
test_done

test-parse-options.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "cache.h"
22
#include "parse-options.h"
3+
#include "string-list.h"
34

45
static int boolean = 0;
56
static int integer = 0;
@@ -9,6 +10,7 @@ static int verbose = 0, dry_run = 0, quiet = 0;
910
static char *string = NULL;
1011
static char *file = NULL;
1112
static int ambiguous;
13+
static struct string_list list;
1214

1315
static int length_callback(const struct option *opt, const char *arg, int unset)
1416
{
@@ -54,6 +56,7 @@ int main(int argc, const char **argv)
5456
OPT_STRING('o', NULL, &string, "str", "get another string"),
5557
OPT_SET_PTR(0, "default-string", &string,
5658
"set string to default", (unsigned long)"default"),
59+
OPT_STRING_LIST(0, "list", &list, "str", "add str to list"),
5760
OPT_GROUP("Magic arguments"),
5861
OPT_ARGUMENT("quux", "means --quux"),
5962
OPT_NUMBER_CALLBACK(&integer, "set integer to NUM",
@@ -85,6 +88,9 @@ int main(int argc, const char **argv)
8588
printf("dry run: %s\n", dry_run ? "yes" : "no");
8689
printf("file: %s\n", file ? file : "(not set)");
8790

91+
for (i = 0; i < list.nr; i++)
92+
printf("list: %s\n", list.items[i].string);
93+
8894
for (i = 0; i < argc; i++)
8995
printf("arg %02d: %s\n", i, argv[i]);
9096

0 commit comments

Comments
 (0)