2 #include "parse-options.h"
4 static int boolean
= 0;
5 static unsigned long integer
= 0;
7 static int verbose
= 0, dry_run
= 0, quiet
= 0;
8 static char *string
= NULL
;
10 int length_callback(const struct option
*opt
, const char *arg
, int unset
)
12 printf("Callback: \"%s\", %d\n",
13 (arg
? arg
: "not set"), unset
);
15 return 1; /* do not support unset */
17 *(unsigned long *)opt
->value
= strlen(arg
);
21 int main(int argc
, const char **argv
)
23 const char *usage
[] = {
24 "test-parse-options <options>",
27 struct option options
[] = {
28 OPT_BOOLEAN('b', "boolean", &boolean
, "get a boolean"),
29 OPT_BIT('4', "or4", &boolean
,
30 "bitwise-or boolean with ...0100", 4),
32 OPT_INTEGER('i', "integer", &integer
, "get a integer"),
33 OPT_INTEGER('j', NULL
, &integer
, "get a integer, too"),
34 OPT_SET_INT(0, "set23", &integer
, "set integer to 23", 23),
35 OPT_DATE('t', NULL
, &integer
, "get timestamp of <time>"),
36 OPT_CALLBACK('L', "length", &integer
, "str",
37 "get length of <str>", length_callback
),
38 OPT_GROUP("String options"),
39 OPT_STRING('s', "string", &string
, "string", "get a string"),
40 OPT_STRING(0, "string2", &string
, "str", "get another string"),
41 OPT_STRING(0, "st", &string
, "st", "get another string (pervert ordering)"),
42 OPT_STRING('o', NULL
, &string
, "str", "get another string"),
43 OPT_SET_PTR(0, "default-string", &string
,
44 "set string to default", (unsigned long)"default"),
45 OPT_GROUP("Magic arguments"),
46 OPT_ARGUMENT("quux", "means --quux"),
47 OPT_GROUP("Standard options"),
49 OPT__VERBOSE(&verbose
),
50 OPT__DRY_RUN(&dry_run
),
56 argc
= parse_options(argc
, argv
, options
, usage
, 0);
58 printf("boolean: %d\n", boolean
);
59 printf("integer: %lu\n", integer
);
60 printf("string: %s\n", string
? string
: "(not set)");
61 printf("abbrev: %d\n", abbrev
);
62 printf("verbose: %d\n", verbose
);
63 printf("quiet: %s\n", quiet
? "yes" : "no");
64 printf("dry run: %s\n", dry_run
? "yes" : "no");
66 for (i
= 0; i
< argc
; i
++)
67 printf("arg %02d: %s\n", i
, argv
[i
]);