3 #include <ccan/typesafe_cb/typesafe_cb.h>
6 /* You can use this directly to build tables, but the macros will ensure
7 * consistency and type safety. */
9 OPT_NOARG = 1, /* -f/--foo */
10 OPT_HASARG = 2, /* -f arg/--foo=arg/--foo arg */
11 OPT_SUBTABLE = 4, /* Actually, longopt points to a subtable... */
12 OPT_END = 8, /* End of the table. */
15 /* Maximum length of arg to show in opt_usage */
16 #define OPT_SHOW_LEN 80
19 const char *names; /* slash-separated names, --longopt or -s */
21 char *(*cb)(void *arg); /* OPT_NOARG */
22 char *(*cb_arg)(const char *optarg, void *arg); /* OPT_HASARG */
23 void (*show)(char buf[OPT_SHOW_LEN], const void *arg);
29 * OPT_WITHOUT_ARG() - macro for initializing an opt_table entry (without arg)
30 * @names: the names of the option eg. "--foo", "-f" or "--foo/-f/--foobar".
31 * @cb: the callback when the option is found.
32 * @arg: the argument to hand to @cb.
33 * @desc: the description for opt_usage(), or opt_hidden.
35 * This is a typesafe wrapper for intializing a struct opt_table. The callback
36 * of type "char *cb(type *)", "char *cb(const type *)" or "char *cb(void *)",
37 * where "type" is the type of the @arg argument.
39 * If the @cb returns non-NULL, opt_parse() will stop parsing, use the
40 * returned string to form an error message for errlog(), free() the
41 * string and return false.
43 * Any number of equivalent short or long options can be listed in @names,
44 * separated by '/'. Short options are a single hyphen followed by a single
45 * character, long options are two hypens followed by one or more characters.
50 #define OPT_WITHOUT_ARG(names, cb, arg, desc) \
51 (names), OPT_CB_NOARG((cb), (arg)), (desc)
54 * OPT_WITH_ARG() - macro for initializing long and short option (with arg)
55 * @names: the option names eg. "--foo=<arg>", "-f" or "-f/--foo <arg>".
56 * @cb: the callback when the option is found (along with <arg>).
57 * @show: the callback to print the value in get_usage (or NULL)
58 * @arg: the argument to hand to @cb and @show
59 * @desc: the description for opt_usage(), or opt_hidden.
61 * This is a typesafe wrapper for intializing a struct opt_table. The callback
62 * is of type "char *cb(const char *, type *)",
63 * "char *cb(const char *, const type *)" or "char *cb(const char *, void *)",
64 * where "type" is the type of the @arg argument. The first argument to the
65 * @cb is the argument found on the commandline.
67 * Similarly, if @show is not NULL, it should be of type "void *show(char *,
68 * const type *)". It should write up to OPT_SHOW_LEN bytes into the first
69 * argument; unless it uses the entire OPT_SHOW_LEN bytes it should
70 * nul-terminate that buffer.
72 * Any number of equivalent short or long options can be listed in @names,
73 * separated by '/'. Short options are a single hyphen followed by a single
74 * character, long options are two hypens followed by one or more characters.
75 * A space or equals in @names is ignored for parsing, and only used
76 * for printing the usage.
78 * If the @cb returns non-NULL, opt_parse() will stop parsing, use the
79 * returned string to form an error message for errlog(), free() the
80 * string and return false.
85 #define OPT_WITH_ARG(name, cb, show, arg, desc) \
86 (name), OPT_CB_ARG((cb), (show), (arg)), (desc)
89 * OPT_SUBTABLE() - macro for including another table inside a table.
90 * @table: the table to include in this table.
91 * @desc: description of this subtable (for opt_usage()) or NULL.
93 #define OPT_SUBTABLE(table, desc) \
94 { (const char *)(table), OPT_SUBTABLE, \
95 sizeof(_check_is_entry(table)) ? NULL : NULL, NULL, NULL, NULL, (desc) }
98 * OPT_ENDTABLE - macro to create final entry in table.
100 * This must be the final element in the opt_table array.
102 #define OPT_ENDTABLE { NULL, OPT_END }
105 * opt_register_table - register a table of options
106 * @table: the table of options
107 * @desc: description of this subtable (for opt_usage()) or NULL.
109 * The table must be terminated by OPT_ENDTABLE.
112 * static struct opt_table opts[] = {
113 * { OPT_WITHOUT_ARG("--verbose", opt_inc_intval, &verbose),
114 * "Verbose mode (can be specified more than once)" },
115 * { OPT_WITHOUT_ARG("-v", opt_inc_intval, &verbose),
116 * "Verbose mode (can be specified more than once)" },
117 * { OPT_WITHOUT_ARG("--usage", opt_usage_and_exit,
118 * "args...\nA silly test program."),
119 * "Print this message." },
124 * opt_register_table(opts, NULL);
126 void opt_register_table(const struct opt_table table[], const char *desc);
129 * opt_register_noarg - register an option with no arguments
130 * @names: the names of the option eg. "--foo", "-f" or "--foo/-f/--foobar".
131 * @cb: the callback when the option is found.
132 * @arg: the argument to hand to @cb.
133 * @desc: the verbose desction of the option (for opt_usage()), or NULL.
135 * This is used for registering a single commandline option which takes
138 * The callback is of type "char *cb(type *)", "char *cb(const type *)"
139 * or "char *cb(void *)", where "type" is the type of the @arg
142 * If the @cb returns non-NULL, opt_parse() will stop parsing, use the
143 * returned string to form an error message for errlog(), free() the
144 * string and return false.
146 #define opt_register_noarg(names, cb, arg, desc) \
147 _opt_register((names), OPT_CB_NOARG((cb), (arg)), (desc))
150 * opt_register_arg - register an option with an arguments
151 * @names: the names of the option eg. "--foo", "-f" or "--foo/-f/--foobar".
152 * @cb: the callback when the option is found.
153 * @show: the callback when the option is found.
154 * @arg: the argument to hand to @cb.
155 * @desc: the verbose desction of the option (for opt_usage()), or NULL.
157 * This is used for registering a single commandline option which takes
160 * The callback is of type "char *cb(const char *, type *)",
161 * "char *cb(const char *, const type *)" or "char *cb(const char *, void *)",
162 * where "type" is the type of the @arg argument. The first argument to the
163 * @cb is the argument found on the commandline.
165 * At least one of @longopt and @shortopt must be non-zero. If the
166 * @cb returns false, opt_parse() will stop parsing and return false.
169 * opt_register_arg("--explode", explode_cb, NULL,
170 * "Make the machine explode (developers only)");
172 #define opt_register_arg(names, cb, show, arg, desc) \
173 _opt_register((names), OPT_CB_ARG((cb), (show), (arg)), (desc))
176 * opt_parse - parse arguments.
177 * @argc: pointer to argc
179 * @errlog: the function to print errors (usually opt_log_stderr).
181 * This iterates through the command line and calls callbacks registered with
182 * opt_register_table()/opt_register_arg()/opt_register_noarg(). If there
183 * are unknown options, missing arguments or a callback returns false, then
184 * an error message is printed and false is returned.
186 * On success, argc and argv are adjusted so only the non-option elements
187 * remain, and true is returned.
190 * if (!opt_parse(argc, argv, opt_log_stderr)) {
191 * printf("%s", opt_usage(argv[0], "<args>..."));
195 bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...));
198 * opt_log_stderr - print message to stderr.
199 * @fmt: printf-style format.
201 * This is the standard helper for opt_parse, to print errors.
203 void opt_log_stderr(const char *fmt, ...);
206 * opt_invalid_argument - helper to allocate an "Invalid argument '%s'" string
207 * @arg: the argument which was invalid.
209 * This is a helper for callbacks to return a simple error string.
211 char *opt_invalid_argument(const char *arg);
214 * opt_usage - create usage message
215 * @argv0: the program name
216 * @extra: extra details to print after the initial command, or NULL.
218 * Creates a usage message, with the program name, arguments, some extra details
219 * and a table of all the options with their descriptions. If an option has
220 * description opt_hidden, it is not shown here.
222 * The result should be passed to free().
224 char *opt_usage(const char *argv0, const char *extra);
227 * opt_hidden - string for undocumented options.
229 * This can be used as the desc parameter if you want an option not to be
230 * shown by opt_usage().
232 extern const char opt_hidden[];
234 /* Standard helpers. You can write your own: */
235 /* Sets the @b to true. */
236 char *opt_set_bool(bool *b);
237 /* Sets @b based on arg: (yes/no/true/false). */
238 char *opt_set_bool_arg(const char *arg, bool *b);
239 void opt_show_bool(char buf[OPT_SHOW_LEN], const bool *b);
241 char *opt_set_invbool(bool *b);
242 void opt_show_invbool(char buf[OPT_SHOW_LEN], const bool *b);
243 /* Sets @b based on !arg: (yes/no/true/false). */
244 char *opt_set_invbool_arg(const char *arg, bool *b);
247 char *opt_set_charp(const char *arg, char **p);
248 void opt_show_charp(char buf[OPT_SHOW_LEN], char *const *p);
250 /* Set an integer value, various forms. Sets to 1 on arg == NULL. */
251 char *opt_set_intval(const char *arg, int *i);
252 void opt_show_intval(char buf[OPT_SHOW_LEN], const int *i);
253 char *opt_set_uintval(const char *arg, unsigned int *ui);
254 void opt_show_uintval(char buf[OPT_SHOW_LEN], const unsigned int *ui);
255 char *opt_set_longval(const char *arg, long *l);
256 void opt_show_longval(char buf[OPT_SHOW_LEN], const long *l);
257 char *opt_set_ulongval(const char *arg, unsigned long *ul);
258 void opt_show_ulongval(char buf[OPT_SHOW_LEN], const unsigned long *ul);
261 char *opt_inc_intval(int *i);
263 /* Display version string to stdout, exit(0). */
264 char *opt_version_and_exit(const char *version);
266 /* Display usage string to stdout, exit(0). */
267 char *opt_usage_and_exit(const char *extra);
269 /* Below here are private declarations. */
270 /* Resolves to the four parameters for non-arg callbacks. */
271 #define OPT_CB_NOARG(cb, arg) \
273 cast_if_any(char *(*)(void *), (cb), &*(cb), \
274 char *(*)(typeof(*(arg))*), \
275 char *(*)(const typeof(*(arg))*), \
276 char *(*)(const void *)), \
279 /* Resolves to the four parameters for arg callbacks. */
280 #define OPT_CB_ARG(cb, show, arg) \
282 cast_if_any(char *(*)(const char *,void *), (cb), &*(cb), \
283 char *(*)(const char *, typeof(*(arg))*), \
284 char *(*)(const char *, const typeof(*(arg))*), \
285 char *(*)(const char *, const void *)), \
286 cast_if_type(void (*)(char buf[], const void *), (show), &*(show), \
287 void (*)(char buf[], const typeof(*(arg))*)), \
290 /* Non-typesafe register function. */
291 void _opt_register(const char *names, enum opt_type type,
292 char *(*cb)(void *arg),
293 char *(*cb_arg)(const char *optarg, void *arg),
294 void (*show)(char buf[OPT_SHOW_LEN], const void *arg),
295 void *arg, const char *desc);
297 /* We use this to get typechecking for OPT_SUBTABLE */
298 static inline int _check_is_entry(struct opt_table *e) { return 0; }
300 #endif /* CCAN_OPT_H */