From: Jeff King Date: Thu, 9 Jun 2011 15:55:23 +0000 (-0400) Subject: parse-options: add OPT_STRING_LIST helper X-Git-Tag: v1.7.7-rc0~90^2~3 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=c8ba163916554e9ffd3ce8cb2beeff003d90c0c3;p=git.git parse-options: add OPT_STRING_LIST helper This just adds repeated invocations of an option to a list of strings. Using the "--no-" form will reset the list to empty. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- diff --git a/parse-options.c b/parse-options.c index 73bd28ad9..879ea82a3 100644 --- a/parse-options.c +++ b/parse-options.c @@ -3,6 +3,7 @@ #include "cache.h" #include "commit.h" #include "color.h" +#include "string-list.h" static int parse_options_usage(struct parse_opt_ctx_t *ctx, const char * const *usagestr, @@ -687,3 +688,19 @@ int parse_options_concat(struct option *dst, size_t dst_size, struct option *src } return -1; } + +int parse_opt_string_list(const struct option *opt, const char *arg, int unset) +{ + struct string_list *v = opt->value; + + if (unset) { + string_list_clear(v, 0); + return 0; + } + + if (!arg) + return -1; + + string_list_append(v, xstrdup(arg)); + return 0; +} diff --git a/parse-options.h b/parse-options.h index d1b12fe97..05eb09b87 100644 --- a/parse-options.h +++ b/parse-options.h @@ -130,6 +130,9 @@ struct option { (h), PARSE_OPT_NOARG, NULL, (p) } #define OPT_INTEGER(s, l, v, h) { OPTION_INTEGER, (s), (l), (v), "n", (h) } #define OPT_STRING(s, l, v, a, h) { OPTION_STRING, (s), (l), (v), (a), (h) } +#define OPT_STRING_LIST(s, l, v, a, h) \ + { OPTION_CALLBACK, (s), (l), (v), (a), \ + (h), 0, &parse_opt_string_list } #define OPT_UYN(s, l, v, h) { OPTION_CALLBACK, (s), (l), (v), NULL, \ (h), PARSE_OPT_NOARG, &parse_opt_tertiary } #define OPT_DATE(s, l, v, h) \ @@ -204,6 +207,7 @@ extern int parse_opt_color_flag_cb(const struct option *, const char *, int); extern int parse_opt_verbosity_cb(const struct option *, const char *, int); 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); #define OPT__VERBOSE(var, h) OPT_BOOLEAN('v', "verbose", (var), (h)) #define OPT__QUIET(var, h) OPT_BOOLEAN('q', "quiet", (var), (h)) diff --git a/t/t0040-parse-options.sh b/t/t0040-parse-options.sh index ae266147b..007f39d5e 100755 --- a/t/t0040-parse-options.sh +++ b/t/t0040-parse-options.sh @@ -28,6 +28,7 @@ String options --st get another string (pervert ordering) -o get another string --default-string set string to default + --list add str to list Magic arguments --quux means --quux @@ -337,4 +338,20 @@ test_expect_success 'negation of OPT_NONEG flags is not ambiguous' ' test_cmp expect output ' +cat >>expect <<'EOF' +list: foo +list: bar +list: baz +EOF +test_expect_success '--list keeps list of strings' ' + test-parse-options --list foo --list=bar --list=baz >output && + test_cmp expect output +' + +test_expect_success '--no-list resets list' ' + test-parse-options --list=other --list=irrelevant --list=options \ + --no-list --list=foo --list=bar --list=baz >output && + test_cmp expect output +' + test_done diff --git a/test-parse-options.c b/test-parse-options.c index 4e3710b9a..91a570165 100644 --- a/test-parse-options.c +++ b/test-parse-options.c @@ -1,5 +1,6 @@ #include "cache.h" #include "parse-options.h" +#include "string-list.h" static int boolean = 0; static int integer = 0; @@ -9,6 +10,7 @@ static int verbose = 0, dry_run = 0, quiet = 0; static char *string = NULL; static char *file = NULL; static int ambiguous; +static struct string_list list; static int length_callback(const struct option *opt, const char *arg, int unset) { @@ -54,6 +56,7 @@ int main(int argc, const char **argv) OPT_STRING('o', NULL, &string, "str", "get another string"), OPT_SET_PTR(0, "default-string", &string, "set string to default", (unsigned long)"default"), + OPT_STRING_LIST(0, "list", &list, "str", "add str to list"), OPT_GROUP("Magic arguments"), OPT_ARGUMENT("quux", "means --quux"), OPT_NUMBER_CALLBACK(&integer, "set integer to NUM", @@ -85,6 +88,9 @@ int main(int argc, const char **argv) printf("dry run: %s\n", dry_run ? "yes" : "no"); printf("file: %s\n", file ? file : "(not set)"); + for (i = 0; i < list.nr; i++) + printf("list: %s\n", list.items[i].string); + for (i = 0; i < argc; i++) printf("arg %02d: %s\n", i, argv[i]);