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