mirror of
https://github.com/git/git
synced 2024-10-28 19:25:47 +00:00
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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
f77bccaeba
commit
c8ba163916
4 changed files with 44 additions and 0 deletions
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -28,6 +28,7 @@ String options
|
|||
--st <st> get another string (pervert ordering)
|
||||
-o <str> get another string
|
||||
--default-string set string to default
|
||||
--list <str> 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
|
||||
|
|
|
@ -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]);
|
||||
|
||||
|
|
Loading…
Reference in a new issue