2 #include "parse-options.h"
3 #include "string-list.h"
5 static int boolean
= 0;
6 static int integer
= 0;
7 static unsigned long magnitude
= 0;
8 static unsigned long timestamp
;
10 static int verbose
= -1; /* unspecified */
11 static int dry_run
= 0, quiet
= 0;
12 static char *string
= NULL
;
13 static char *file
= NULL
;
15 static struct string_list list
;
17 static int length_callback(const struct option
*opt
, const char *arg
, int unset
)
19 printf("Callback: \"%s\", %d\n",
20 (arg
? arg
: "not set"), unset
);
22 return 1; /* do not support unset */
24 *(int *)opt
->value
= strlen(arg
);
28 static int number_callback(const struct option
*opt
, const char *arg
, int unset
)
30 *(int *)opt
->value
= strtol(arg
, NULL
, 10);
34 int main(int argc
, char **argv
)
36 const char *prefix
= "prefix/";
37 const char *usage
[] = {
38 "test-parse-options <options>",
41 struct option options
[] = {
42 OPT_BOOL(0, "yes", &boolean
, "get a boolean"),
43 OPT_BOOL('D', "no-doubt", &boolean
, "begins with 'no-'"),
44 { OPTION_SET_INT
, 'B', "no-fear", &boolean
, NULL
,
45 "be brave", PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, NULL
, 1 },
46 OPT_COUNTUP('b', "boolean", &boolean
, "increment by one"),
47 OPT_BIT('4', "or4", &boolean
,
48 "bitwise-or boolean with ...0100", 4),
49 OPT_NEGBIT(0, "neg-or4", &boolean
, "same as --no-or4", 4),
51 OPT_INTEGER('i', "integer", &integer
, "get a integer"),
52 OPT_INTEGER('j', NULL
, &integer
, "get a integer, too"),
53 OPT_MAGNITUDE('m', "magnitude", &magnitude
, "get a magnitude"),
54 OPT_SET_INT(0, "set23", &integer
, "set integer to 23", 23),
55 OPT_DATE('t', NULL
, ×tamp
, "get timestamp of <time>"),
56 OPT_CALLBACK('L', "length", &integer
, "str",
57 "get length of <str>", length_callback
),
58 OPT_FILENAME('F', "file", &file
, "set file to <file>"),
59 OPT_GROUP("String options"),
60 OPT_STRING('s', "string", &string
, "string", "get a string"),
61 OPT_STRING(0, "string2", &string
, "str", "get another string"),
62 OPT_STRING(0, "st", &string
, "st", "get another string (pervert ordering)"),
63 OPT_STRING('o', NULL
, &string
, "str", "get another string"),
64 OPT_NOOP_NOARG(0, "obsolete"),
65 OPT_STRING_LIST(0, "list", &list
, "str", "add str to list"),
66 OPT_GROUP("Magic arguments"),
67 OPT_ARGUMENT("quux", "means --quux"),
68 OPT_NUMBER_CALLBACK(&integer
, "set integer to NUM",
70 { OPTION_COUNTUP
, '+', NULL
, &boolean
, NULL
, "same as -b",
71 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
| PARSE_OPT_NODASH
},
72 { OPTION_COUNTUP
, 0, "ambiguous", &ambiguous
, NULL
,
73 "positive ambiguity", PARSE_OPT_NOARG
| PARSE_OPT_NONEG
},
74 { OPTION_COUNTUP
, 0, "no-ambiguous", &ambiguous
, NULL
,
75 "negative ambiguity", PARSE_OPT_NOARG
| PARSE_OPT_NONEG
},
76 OPT_GROUP("Standard options"),
78 OPT__VERBOSE(&verbose
, "be verbose"),
79 OPT__DRY_RUN(&dry_run
, "dry run"),
80 OPT__QUIET(&quiet
, "be quiet"),
85 argc
= parse_options(argc
, (const char **)argv
, prefix
, options
, usage
, 0);
87 printf("boolean: %d\n", boolean
);
88 printf("integer: %d\n", integer
);
89 printf("magnitude: %lu\n", magnitude
);
90 printf("timestamp: %lu\n", timestamp
);
91 printf("string: %s\n", string
? string
: "(not set)");
92 printf("abbrev: %d\n", abbrev
);
93 printf("verbose: %d\n", verbose
);
94 printf("quiet: %d\n", quiet
);
95 printf("dry run: %s\n", dry_run
? "yes" : "no");
96 printf("file: %s\n", file
? file
: "(not set)");
98 for (i
= 0; i
< list
.nr
; i
++)
99 printf("list: %s\n", list
.items
[i
].string
);
101 for (i
= 0; i
< argc
; i
++)
102 printf("arg %02d: %s\n", i
, argv
[i
]);