parse-options: add OPT_STRING_LIST helper
[git/git.git] / test-parse-options.c
CommitLineData
beb47437
JS
1#include "cache.h"
2#include "parse-options.h"
c8ba1639 3#include "string-list.h"
beb47437
JS
4
5static int boolean = 0;
c4aca9cc
JH
6static int integer = 0;
7static unsigned long timestamp;
010a2dac
SB
8static int abbrev = 7;
9static int verbose = 0, dry_run = 0, quiet = 0;
beb47437 10static char *string = NULL;
df217ed6 11static char *file = NULL;
6bbfd1fa 12static int ambiguous;
c8ba1639 13static struct string_list list;
beb47437 14
2af202be 15static int length_callback(const struct option *opt, const char *arg, int unset)
010a2dac
SB
16{
17 printf("Callback: \"%s\", %d\n",
18 (arg ? arg : "not set"), unset);
19 if (unset)
20 return 1; /* do not support unset */
21
8caa3acf 22 *(int *)opt->value = strlen(arg);
010a2dac
SB
23 return 0;
24}
25
2af202be 26static int number_callback(const struct option *opt, const char *arg, int unset)
e0319ff5
RS
27{
28 *(int *)opt->value = strtol(arg, NULL, 10);
29 return 0;
30}
31
beb47437
JS
32int main(int argc, const char **argv)
33{
df217ed6 34 const char *prefix = "prefix/";
beb47437
JS
35 const char *usage[] = {
36 "test-parse-options <options>",
37 NULL
38 };
39 struct option options[] = {
40 OPT_BOOLEAN('b', "boolean", &boolean, "get a boolean"),
010a2dac
SB
41 OPT_BIT('4', "or4", &boolean,
42 "bitwise-or boolean with ...0100", 4),
2f4b97f9 43 OPT_NEGBIT(0, "neg-or4", &boolean, "same as --no-or4", 4),
010a2dac 44 OPT_GROUP(""),
beb47437
JS
45 OPT_INTEGER('i', "integer", &integer, "get a integer"),
46 OPT_INTEGER('j', NULL, &integer, "get a integer, too"),
010a2dac 47 OPT_SET_INT(0, "set23", &integer, "set integer to 23", 23),
c4aca9cc 48 OPT_DATE('t', NULL, &timestamp, "get timestamp of <time>"),
010a2dac
SB
49 OPT_CALLBACK('L', "length", &integer, "str",
50 "get length of <str>", length_callback),
41dbcd45 51 OPT_FILENAME('F', "file", &file, "set file to <file>"),
010a2dac 52 OPT_GROUP("String options"),
beb47437
JS
53 OPT_STRING('s', "string", &string, "string", "get a string"),
54 OPT_STRING(0, "string2", &string, "str", "get another string"),
243e0614 55 OPT_STRING(0, "st", &string, "st", "get another string (pervert ordering)"),
3a9f0f41 56 OPT_STRING('o', NULL, &string, "str", "get another string"),
010a2dac
SB
57 OPT_SET_PTR(0, "default-string", &string,
58 "set string to default", (unsigned long)"default"),
c8ba1639 59 OPT_STRING_LIST(0, "list", &list, "str", "add str to list"),
010a2dac 60 OPT_GROUP("Magic arguments"),
580d5bff 61 OPT_ARGUMENT("quux", "means --quux"),
e0319ff5
RS
62 OPT_NUMBER_CALLBACK(&integer, "set integer to NUM",
63 number_callback),
51a9949e
RS
64 { OPTION_BOOLEAN, '+', NULL, &boolean, NULL, "same as -b",
65 PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH },
6bbfd1fa
AS
66 { OPTION_BOOLEAN, 0, "ambiguous", &ambiguous, NULL,
67 "positive ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },
68 { OPTION_BOOLEAN, 0, "no-ambiguous", &ambiguous, NULL,
69 "negative ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },
010a2dac
SB
70 OPT_GROUP("Standard options"),
71 OPT__ABBREV(&abbrev),
fd03881a 72 OPT__VERBOSE(&verbose, "be verbose"),
e21adb8c 73 OPT__DRY_RUN(&dry_run, "dry run"),
d52ee6e6 74 OPT__QUIET(&quiet, "be quiet"),
beb47437
JS
75 OPT_END(),
76 };
77 int i;
78
df217ed6 79 argc = parse_options(argc, argv, prefix, options, usage, 0);
beb47437
JS
80
81 printf("boolean: %d\n", boolean);
c4aca9cc
JH
82 printf("integer: %u\n", integer);
83 printf("timestamp: %lu\n", timestamp);
beb47437 84 printf("string: %s\n", string ? string : "(not set)");
010a2dac
SB
85 printf("abbrev: %d\n", abbrev);
86 printf("verbose: %d\n", verbose);
87 printf("quiet: %s\n", quiet ? "yes" : "no");
88 printf("dry run: %s\n", dry_run ? "yes" : "no");
df217ed6 89 printf("file: %s\n", file ? file : "(not set)");
beb47437 90
c8ba1639
JK
91 for (i = 0; i < list.nr; i++)
92 printf("list: %s\n", list.items[i].string);
93
beb47437
JS
94 for (i = 0; i < argc; i++)
95 printf("arg %02d: %s\n", i, argv[i]);
96
97 return 0;
98}