2 #include "parse-options.h"
4 static int boolean
= 0;
5 static int integer
= 0;
6 static unsigned long timestamp
;
8 static int verbose
= 0, dry_run
= 0, quiet
= 0;
9 static char *string
= NULL
;
11 int length_callback(const struct option
*opt
, const char *arg
, int unset
)
13 printf("Callback: \"%s\", %d\n",
14 (arg
? arg
: "not set"), unset
);
16 return 1; /* do not support unset */
18 *(int *)opt
->value
= strlen(arg
);
22 int main(int argc
, const char **argv
)
24 const char *usage
[] = {
25 "test-parse-options <options>",
28 struct option options
[] = {
29 OPT_BOOLEAN('b', "boolean", &boolean
, "get a boolean"),
30 OPT_BIT('4', "or4", &boolean
,
31 "bitwise-or boolean with ...0100", 4),
32 OPT_NEGBIT(0, "neg-or4", &boolean
, "same as --no-or4", 4),
34 OPT_INTEGER('i', "integer", &integer
, "get a integer"),
35 OPT_INTEGER('j', NULL
, &integer
, "get a integer, too"),
36 OPT_SET_INT(0, "set23", &integer
, "set integer to 23", 23),
37 OPT_DATE('t', NULL
, ×tamp
, "get timestamp of <time>"),
38 OPT_CALLBACK('L', "length", &integer
, "str",
39 "get length of <str>", length_callback
),
40 OPT_GROUP("String options"),
41 OPT_STRING('s', "string", &string
, "string", "get a string"),
42 OPT_STRING(0, "string2", &string
, "str", "get another string"),
43 OPT_STRING(0, "st", &string
, "st", "get another string (pervert ordering)"),
44 OPT_STRING('o', NULL
, &string
, "str", "get another string"),
45 OPT_SET_PTR(0, "default-string", &string
,
46 "set string to default", (unsigned long)"default"),
47 OPT_GROUP("Magic arguments"),
48 OPT_ARGUMENT("quux", "means --quux"),
49 OPT_GROUP("Standard options"),
51 OPT__VERBOSE(&verbose
),
52 OPT__DRY_RUN(&dry_run
),
58 argc
= parse_options(argc
, argv
, options
, usage
, 0);
60 printf("boolean: %d\n", boolean
);
61 printf("integer: %u\n", integer
);
62 printf("timestamp: %lu\n", timestamp
);
63 printf("string: %s\n", string
? string
: "(not set)");
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");
69 for (i
= 0; i
< argc
; i
++)
70 printf("arg %02d: %s\n", i
, argv
[i
]);