3 #include <ccan/compiler/compiler.h>
4 #include <ccan/typesafe_cb/typesafe_cb.h>
11 * OPT_WITHOUT_ARG() - macro for initializing an opt_table entry (without arg)
12 * @names: the names of the option eg. "--foo", "-f" or "--foo|-f|--foobar".
13 * @cb: the callback when the option is found.
14 * @arg: the argument to hand to @cb.
15 * @desc: the description for opt_usage(), or opt_hidden.
17 * This is a typesafe wrapper for initializing a struct opt_table. The callback
18 * of type "char *cb(type *)", "char *cb(const type *)" or "char *cb(void *)",
19 * where "type" is the type of the @arg argument.
21 * If the @cb returns non-NULL, opt_parse() will stop parsing, use the
22 * returned string to form an error message for errlog(), free() the
23 * string and return false.
25 * Any number of equivalent short or long options can be listed in @names,
26 * separated by '|'. Short options are a single hyphen followed by a single
27 * character, long options are two hyphens followed by one or more characters.
32 #define OPT_WITHOUT_ARG(names, cb, arg, desc) \
33 { (names), OPT_CB_NOARG((cb), (arg)), (desc) }
36 * OPT_WITH_ARG() - macro for initializing long and short option (with arg)
37 * @names: the option names eg. "--foo=<arg>", "-f" or "-f|--foo <arg>".
38 * @cb: the callback when the option is found (along with <arg>).
39 * @show: the callback to print the value in get_usage (or NULL)
40 * @arg: the argument to hand to @cb and @show
41 * @desc: the description for opt_usage(), or opt_hidden.
43 * This is a typesafe wrapper for initializing a struct opt_table. The callback
44 * is of type "char *cb(const char *, type *)",
45 * "char *cb(const char *, const type *)" or "char *cb(const char *, void *)",
46 * where "type" is the type of the @arg argument. The first argument to the
47 * @cb is the argument found on the commandline.
49 * Similarly, if @show is not NULL, it should be of type "void *show(char *,
50 * const type *)". It should write up to OPT_SHOW_LEN bytes into the first
51 * argument; unless it uses the entire OPT_SHOW_LEN bytes it should
52 * nul-terminate that buffer.
54 * Any number of equivalent short or long options can be listed in @names,
55 * separated by '|'. Short options are a single hyphen followed by a single
56 * character, long options are two hyphens followed by one or more characters.
57 * A space or equals in @names is ignored for parsing, and only used
58 * for printing the usage.
60 * If the @cb returns non-NULL, opt_parse() will stop parsing, use the
61 * returned string to form an error message for errlog(), free() the
62 * string and return false.
67 #define OPT_WITH_ARG(name, cb, show, arg, desc) \
68 { (name), OPT_CB_ARG((cb), (show), (arg)), (desc) }
71 * OPT_SUBTABLE() - macro for including another table inside a table.
72 * @table: the table to include in this table.
73 * @desc: description of this subtable (for opt_usage()) or NULL.
75 #define OPT_SUBTABLE(table, desc) \
76 { (const char *)(table), OPT_SUBTABLE, \
77 sizeof(_check_is_entry(table)) ? NULL : NULL, NULL, NULL, NULL, (desc) }
80 * OPT_ENDTABLE - macro to create final entry in table.
82 * This must be the final element in the opt_table array.
84 #define OPT_ENDTABLE { NULL, OPT_END, NULL, NULL, NULL, NULL, NULL }
87 * opt_register_table - register a table of options
88 * @table: the table of options
89 * @desc: description of this subtable (for opt_usage()) or NULL.
91 * The table must be terminated by OPT_ENDTABLE.
94 * static int verbose = 0;
95 * static struct opt_table opts[] = {
96 * OPT_WITHOUT_ARG("--verbose", opt_inc_intval, &verbose,
97 * "Verbose mode (can be specified more than once)"),
98 * OPT_WITHOUT_ARG("-v", opt_inc_intval, &verbose,
99 * "Verbose mode (can be specified more than once)"),
100 * OPT_WITHOUT_ARG("--usage", opt_usage_and_exit,
101 * "args...\nA silly test program.",
102 * "Print this message."),
107 * opt_register_table(opts, NULL);
109 void opt_register_table(const struct opt_table *table, const char *desc);
112 * opt_register_noarg - register an option with no arguments
113 * @names: the names of the option eg. "--foo", "-f" or "--foo|-f|--foobar".
114 * @cb: the callback when the option is found.
115 * @arg: the argument to hand to @cb.
116 * @desc: the verbose description of the option (for opt_usage()), or NULL.
118 * This is used for registering a single commandline option which takes
121 * The callback is of type "char *cb(type *)", "char *cb(const type *)"
122 * or "char *cb(void *)", where "type" is the type of the @arg
125 * If the @cb returns non-NULL, opt_parse() will stop parsing, use the
126 * returned string to form an error message for errlog(), free() the
127 * string and return false.
129 #define opt_register_noarg(names, cb, arg, desc) \
130 _opt_register((names), OPT_CB_NOARG((cb), (arg)), (desc))
133 * opt_register_arg - register an option with an arguments
134 * @names: the names of the option eg. "--foo", "-f" or "--foo|-f|--foobar".
135 * @cb: the callback when the option is found.
136 * @show: the callback to print the value in get_usage (or NULL)
137 * @arg: the argument to hand to @cb.
138 * @desc: the verbose description of the option (for opt_usage()), or NULL.
140 * This is used for registering a single commandline option which takes
143 * The callback is of type "char *cb(const char *, type *)",
144 * "char *cb(const char *, const type *)" or "char *cb(const char *, void *)",
145 * where "type" is the type of the @arg argument. The first argument to the
146 * @cb is the argument found on the commandline.
148 * At least one of @longopt and @shortopt must be non-zero. If the
149 * @cb returns false, opt_parse() will stop parsing and return false.
152 * static char *explode(const char *optarg, void *unused)
154 * errx(1, "BOOM! %s", optarg);
157 * opt_register_arg("--explode|--boom", explode, NULL, NULL, opt_hidden);
159 #define opt_register_arg(names, cb, show, arg, desc) \
160 _opt_register((names), OPT_CB_ARG((cb), (show), (arg)), (desc))
163 * opt_parse - parse arguments.
164 * @argc: pointer to argc
166 * @errlog: the function to print errors
168 * This iterates through the command line and calls callbacks registered with
169 * opt_register_table()/opt_register_arg()/opt_register_noarg(). If there
170 * are unknown options, missing arguments or a callback returns false, then
171 * an error message is printed and false is returned.
173 * On success, argc and argv are adjusted so only the non-option elements
174 * remain, and true is returned.
177 * if (!opt_parse(&argc, argv, opt_log_stderr)) {
178 * printf("You screwed up, aborting!\n");
183 * opt_log_stderr, opt_log_stderr_exit
185 bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...));
188 * opt_free_table - free the table.
190 * This frees the internal memory. Call this as the last
193 void opt_free_table(void);
196 * opt_log_stderr - print message to stderr.
197 * @fmt: printf-style format.
199 * This is a helper for opt_parse, to print errors to stderr.
202 * opt_log_stderr_exit
204 void opt_log_stderr(const char *fmt, ...);
207 * opt_log_stderr_exit - print message to stderr, then exit(1)
208 * @fmt: printf-style format.
210 * Just like opt_log_stderr, only then does exit(1). This means that
211 * when handed to opt_parse, opt_parse will never return false.
214 * // This never returns false; just exits if there's an erorr.
215 * opt_parse(&argc, argv, opt_log_stderr_exit);
217 void opt_log_stderr_exit(const char *fmt, ...);
220 * opt_invalid_argument - helper to allocate an "Invalid argument '%s'" string
221 * @arg: the argument which was invalid.
223 * This is a helper for callbacks to return a simple error string.
225 char *opt_invalid_argument(const char *arg);
228 * opt_usage - create usage message
229 * @argv0: the program name
230 * @extra: extra details to print after the initial command, or NULL.
232 * Creates a usage message, with the program name, arguments, some extra details
233 * and a table of all the options with their descriptions. If an option has
234 * description opt_hidden, it is not shown here.
236 * If "extra" is NULL, then the extra information is taken from any
237 * registered option which calls opt_usage_and_exit(). This avoids duplicating
238 * that string in the common case.
240 * The result should be passed to free().
242 char *opt_usage(const char *argv0, const char *extra);
245 * opt_hidden - string for undocumented options.
247 * This can be used as the desc parameter if you want an option not to be
248 * shown by opt_usage().
250 extern const char opt_hidden[];
252 /* Maximum length of arg to show in opt_usage */
253 #define OPT_SHOW_LEN 80
255 /* Standard helpers. You can write your own: */
256 /* Sets the @b to true. */
257 char *opt_set_bool(bool *b);
258 /* Sets @b based on arg: (yes/no/true/false). */
259 char *opt_set_bool_arg(const char *arg, bool *b);
260 void opt_show_bool(char buf[OPT_SHOW_LEN], const bool *b);
262 char *opt_set_invbool(bool *b);
263 void opt_show_invbool(char buf[OPT_SHOW_LEN], const bool *b);
264 /* Sets @b based on !arg: (yes/no/true/false). */
265 char *opt_set_invbool_arg(const char *arg, bool *b);
268 char *opt_set_charp(const char *arg, char **p);
269 void opt_show_charp(char buf[OPT_SHOW_LEN], char *const *p);
271 /* Set an integer value, various forms. Sets to 1 on arg == NULL. */
272 char *opt_set_intval(const char *arg, int *i);
273 void opt_show_intval(char buf[OPT_SHOW_LEN], const int *i);
274 char *opt_set_uintval(const char *arg, unsigned int *ui);
275 void opt_show_uintval(char buf[OPT_SHOW_LEN], const unsigned int *ui);
276 char *opt_set_longval(const char *arg, long *l);
277 void opt_show_longval(char buf[OPT_SHOW_LEN], const long *l);
278 char *opt_set_ulongval(const char *arg, unsigned long *ul);
279 void opt_show_ulongval(char buf[OPT_SHOW_LEN], const unsigned long *ul);
282 char *opt_inc_intval(int *i);
284 /* Display version string to stdout, exit(0). */
285 char *opt_version_and_exit(const char *version);
287 /* Display usage string to stdout, exit(0). */
288 char *opt_usage_and_exit(const char *extra);
290 /* Below here are private declarations. */
291 /* You can use this directly to build tables, but the macros will ensure
292 * consistency and type safety. */
294 OPT_NOARG = 1, /* -f|--foo */
295 OPT_HASARG = 2, /* -f arg|--foo=arg|--foo arg */
296 OPT_SUBTABLE = 4, /* Actually, longopt points to a subtable... */
297 OPT_END = 8, /* End of the table. */
301 const char *names; /* pipe-separated names, --longopt or -s */
303 char *(*cb)(void *arg); /* OPT_NOARG */
304 char *(*cb_arg)(const char *optarg, void *arg); /* OPT_HASARG */
305 void (*show)(char buf[OPT_SHOW_LEN], const void *arg);
310 /* Resolves to the four parameters for non-arg callbacks. */
311 #define OPT_CB_NOARG(cb, arg) \
313 cast_if_any(char *(*)(void *), (cb), (cb)+0, \
314 char *(*)(typeof(*(arg))*), \
315 char *(*)(const typeof(*(arg))*), \
316 char *(*)(const void *)), \
319 /* Resolves to the four parameters for arg callbacks. */
320 #define OPT_CB_ARG(cb, show, arg) \
322 cast_if_any(char *(*)(const char *,void *), (cb), (cb)+0, \
323 char *(*)(const char *, typeof(*(arg))*), \
324 char *(*)(const char *, const typeof(*(arg))*), \
325 char *(*)(const char *, const void *)), \
326 cast_if_type(void (*)(char buf[], const void *), (show), (show)+0, \
327 void (*)(char buf[], const typeof(*(arg))*)), \
330 /* Non-typesafe register function. */
331 void _opt_register(const char *names, enum opt_type type,
332 char *(*cb)(void *arg),
333 char *(*cb_arg)(const char *optarg, void *arg),
334 void (*show)(char buf[OPT_SHOW_LEN], const void *arg),
335 void *arg, const char *desc);
337 /* We use this to get typechecking for OPT_SUBTABLE */
338 static inline int _check_is_entry(struct opt_table *e UNUSED) { return 0; }
340 #endif /* CCAN_OPT_H */