1#include "cache.h"
2#include "parse-options.h"
3#include "string-list.h"
45
static int boolean = 0;
6static int integer = 0;
7static unsigned long timestamp;
8static int abbrev = 7;
9static int verbose = 0, dry_run = 0, quiet = 0;
10static char *string = NULL;
11static char *file = NULL;
12static int ambiguous;
13static struct string_list list;
1415
static int length_callback(const struct option *opt, const char *arg, int unset)
16{
17printf("Callback: \"%s\", %d\n",
18(arg ? arg : "not set"), unset);
19if (unset)
20return 1; /* do not support unset */
2122
*(int *)opt->value = strlen(arg);
23return 0;
24}
2526
static int number_callback(const struct option *opt, const char *arg, int unset)
27{
28*(int *)opt->value = strtol(arg, NULL, 10);
29return 0;
30}
3132
int main(int argc, const char **argv)
33{
34const char *prefix = "prefix/";
35const char *usage[] = {
36"test-parse-options <options>",
37NULL
38};
39struct option options[] = {
40OPT_BOOLEAN('b', "boolean", &boolean, "get a boolean"),
41OPT_BIT('4', "or4", &boolean,
42"bitwise-or boolean with ...0100", 4),
43OPT_NEGBIT(0, "neg-or4", &boolean, "same as --no-or4", 4),
44OPT_GROUP(""),
45OPT_INTEGER('i', "integer", &integer, "get a integer"),
46OPT_INTEGER('j', NULL, &integer, "get a integer, too"),
47OPT_SET_INT(0, "set23", &integer, "set integer to 23", 23),
48OPT_DATE('t', NULL, ×tamp, "get timestamp of <time>"),
49OPT_CALLBACK('L', "length", &integer, "str",
50"get length of <str>", length_callback),
51OPT_FILENAME('F', "file", &file, "set file to <file>"),
52OPT_GROUP("String options"),
53OPT_STRING('s', "string", &string, "string", "get a string"),
54OPT_STRING(0, "string2", &string, "str", "get another string"),
55OPT_STRING(0, "st", &string, "st", "get another string (pervert ordering)"),
56OPT_STRING('o', NULL, &string, "str", "get another string"),
57OPT_SET_PTR(0, "default-string", &string,
58"set string to default", (unsigned long)"default"),
59OPT_STRING_LIST(0, "list", &list, "str", "add str to list"),
60OPT_GROUP("Magic arguments"),
61OPT_ARGUMENT("quux", "means --quux"),
62OPT_NUMBER_CALLBACK(&integer, "set integer to NUM",
63number_callback),
64{ OPTION_BOOLEAN, '+', NULL, &boolean, NULL, "same as -b",
65PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH },
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 },
70OPT_GROUP("Standard options"),
71OPT__ABBREV(&abbrev),
72OPT__VERBOSE(&verbose, "be verbose"),
73OPT__DRY_RUN(&dry_run, "dry run"),
74OPT__QUIET(&quiet, "be quiet"),
75OPT_END(),
76};
77int i;
7879
argc = parse_options(argc, argv, prefix, options, usage, 0);
8081
printf("boolean: %d\n", boolean);
82printf("integer: %u\n", integer);
83printf("timestamp: %lu\n", timestamp);
84printf("string: %s\n", string ? string : "(not set)");
85printf("abbrev: %d\n", abbrev);
86printf("verbose: %d\n", verbose);
87printf("quiet: %s\n", quiet ? "yes" : "no");
88printf("dry run: %s\n", dry_run ? "yes" : "no");
89printf("file: %s\n", file ? file : "(not set)");
9091
for (i = 0; i < list.nr; i++)
92printf("list: %s\n", list.items[i].string);
9394
for (i = 0; i < argc; i++)
95printf("arg %02d: %s\n", i, argv[i]);
9697
return 0;
98}