parseopt: add OPT_NEGBIT
[git/git.git] / test-parse-options.c
CommitLineData
beb47437
JS
1#include "cache.h"
2#include "parse-options.h"
3
4static int boolean = 0;
c4aca9cc
JH
5static int integer = 0;
6static unsigned long timestamp;
010a2dac
SB
7static int abbrev = 7;
8static int verbose = 0, dry_run = 0, quiet = 0;
beb47437
JS
9static char *string = NULL;
10
010a2dac
SB
11int length_callback(const struct option *opt, const char *arg, int unset)
12{
13 printf("Callback: \"%s\", %d\n",
14 (arg ? arg : "not set"), unset);
15 if (unset)
16 return 1; /* do not support unset */
17
8caa3acf 18 *(int *)opt->value = strlen(arg);
010a2dac
SB
19 return 0;
20}
21
beb47437
JS
22int main(int argc, const char **argv)
23{
24 const char *usage[] = {
25 "test-parse-options <options>",
26 NULL
27 };
28 struct option options[] = {
29 OPT_BOOLEAN('b', "boolean", &boolean, "get a boolean"),
010a2dac
SB
30 OPT_BIT('4', "or4", &boolean,
31 "bitwise-or boolean with ...0100", 4),
2f4b97f9 32 OPT_NEGBIT(0, "neg-or4", &boolean, "same as --no-or4", 4),
010a2dac 33 OPT_GROUP(""),
beb47437
JS
34 OPT_INTEGER('i', "integer", &integer, "get a integer"),
35 OPT_INTEGER('j', NULL, &integer, "get a integer, too"),
010a2dac 36 OPT_SET_INT(0, "set23", &integer, "set integer to 23", 23),
c4aca9cc 37 OPT_DATE('t', NULL, &timestamp, "get timestamp of <time>"),
010a2dac
SB
38 OPT_CALLBACK('L', "length", &integer, "str",
39 "get length of <str>", length_callback),
40 OPT_GROUP("String options"),
beb47437
JS
41 OPT_STRING('s', "string", &string, "string", "get a string"),
42 OPT_STRING(0, "string2", &string, "str", "get another string"),
243e0614 43 OPT_STRING(0, "st", &string, "st", "get another string (pervert ordering)"),
3a9f0f41 44 OPT_STRING('o', NULL, &string, "str", "get another string"),
010a2dac
SB
45 OPT_SET_PTR(0, "default-string", &string,
46 "set string to default", (unsigned long)"default"),
47 OPT_GROUP("Magic arguments"),
580d5bff 48 OPT_ARGUMENT("quux", "means --quux"),
010a2dac
SB
49 OPT_GROUP("Standard options"),
50 OPT__ABBREV(&abbrev),
51 OPT__VERBOSE(&verbose),
52 OPT__DRY_RUN(&dry_run),
53 OPT__QUIET(&quiet),
beb47437
JS
54 OPT_END(),
55 };
56 int i;
57
58 argc = parse_options(argc, argv, options, usage, 0);
59
60 printf("boolean: %d\n", boolean);
c4aca9cc
JH
61 printf("integer: %u\n", integer);
62 printf("timestamp: %lu\n", timestamp);
beb47437 63 printf("string: %s\n", string ? string : "(not set)");
010a2dac
SB
64 printf("abbrev: %d\n", abbrev);
65 printf("verbose: %d\n", verbose);
66 printf("quiet: %s\n", quiet ? "yes" : "no");
67 printf("dry run: %s\n", dry_run ? "yes" : "no");
beb47437
JS
68
69 for (i = 0; i < argc; i++)
70 printf("arg %02d: %s\n", i, argv[i]);
71
72 return 0;
73}