opt.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * Copyright 2011 Rusty Russell
  3. * Copyright 2011 Con Kolivas
  4. *
  5. * This program is free software; you can redistribute it and/or modify it under
  6. * the terms of the GNU General Public License as published by the Free Software
  7. * Foundation; either version 2 of the License, or (at your option) any later
  8. * version. See LICENSE for more details.
  9. */
  10. #ifndef CCAN_OPT_H
  11. #define CCAN_OPT_H
  12. #include <ccan/compiler/compiler.h>
  13. #include <ccan/typesafe_cb/typesafe_cb.h>
  14. #include <stdbool.h>
  15. #include <stdlib.h>
  16. struct opt_table;
  17. /**
  18. * OPT_WITHOUT_ARG() - macro for initializing an opt_table entry (without arg)
  19. * @names: the names of the option eg. "--foo", "-f" or "--foo|-f|--foobar".
  20. * @cb: the callback when the option is found.
  21. * @arg: the argument to hand to @cb.
  22. * @desc: the description for opt_usage(), or opt_hidden.
  23. *
  24. * This is a typesafe wrapper for initializing a struct opt_table. The callback
  25. * of type "char *cb(type *)", "char *cb(const type *)" or "char *cb(void *)",
  26. * where "type" is the type of the @arg argument.
  27. *
  28. * If the @cb returns non-NULL, opt_parse() will stop parsing, use the
  29. * returned string to form an error message for errlog(), free() the
  30. * string and return false.
  31. *
  32. * Any number of equivalent short or long options can be listed in @names,
  33. * separated by '|'. Short options are a single hyphen followed by a single
  34. * character, long options are two hyphens followed by one or more characters.
  35. *
  36. * See Also:
  37. * OPT_WITH_ARG()
  38. */
  39. #define OPT_WITHOUT_ARG(names, cb, arg, desc) \
  40. { (names), OPT_CB_NOARG((cb), (arg)), { (arg) }, (desc) }
  41. /**
  42. * OPT_WITH_ARG() - macro for initializing long and short option (with arg)
  43. * @names: the option names eg. "--foo=<arg>", "-f" or "-f|--foo <arg>".
  44. * @cb: the callback when the option is found (along with <arg>).
  45. * @show: the callback to print the value in get_usage (or NULL)
  46. * @arg: the argument to hand to @cb and @show
  47. * @desc: the description for opt_usage(), or opt_hidden.
  48. *
  49. * This is a typesafe wrapper for initializing a struct opt_table. The callback
  50. * is of type "char *cb(const char *, type *)",
  51. * "char *cb(const char *, const type *)" or "char *cb(const char *, void *)",
  52. * where "type" is the type of the @arg argument. The first argument to the
  53. * @cb is the argument found on the commandline.
  54. *
  55. * Similarly, if @show is not NULL, it should be of type "void *show(char *,
  56. * const type *)". It should write up to OPT_SHOW_LEN bytes into the first
  57. * argument; unless it uses the entire OPT_SHOW_LEN bytes it should
  58. * nul-terminate that buffer.
  59. *
  60. * Any number of equivalent short or long options can be listed in @names,
  61. * separated by '|'. Short options are a single hyphen followed by a single
  62. * character, long options are two hyphens followed by one or more characters.
  63. * A space or equals in @names is ignored for parsing, and only used
  64. * for printing the usage.
  65. *
  66. * If the @cb returns non-NULL, opt_parse() will stop parsing, use the
  67. * returned string to form an error message for errlog(), free() the
  68. * string and return false.
  69. *
  70. * See Also:
  71. * OPT_WITHOUT_ARG()
  72. */
  73. #define OPT_WITH_ARG(name, cb, show, arg, desc) \
  74. { (name), OPT_CB_ARG((cb), (show), (arg)), { (arg) }, (desc) }
  75. /**
  76. * OPT_SUBTABLE() - macro for including another table inside a table.
  77. * @table: the table to include in this table.
  78. * @desc: description of this subtable (for opt_usage()) or NULL.
  79. */
  80. #define OPT_SUBTABLE(table, desc) \
  81. { (const char *)(table), OPT_SUBTABLE, \
  82. sizeof(_check_is_entry(table)) ? NULL : NULL, NULL, NULL, \
  83. { NULL }, (desc) }
  84. /**
  85. * OPT_ENDTABLE - macro to create final entry in table.
  86. *
  87. * This must be the final element in the opt_table array.
  88. */
  89. #define OPT_ENDTABLE { NULL, OPT_END, NULL, NULL, NULL, { NULL }, NULL }
  90. /**
  91. * opt_register_table - register a table of options
  92. * @table: the table of options
  93. * @desc: description of this subtable (for opt_usage()) or NULL.
  94. *
  95. * The table must be terminated by OPT_ENDTABLE.
  96. *
  97. * Example:
  98. * static int verbose = 0;
  99. * static struct opt_table opts[] = {
  100. * OPT_WITHOUT_ARG("--verbose", opt_inc_intval, &verbose,
  101. * "Verbose mode (can be specified more than once)"),
  102. * OPT_WITHOUT_ARG("-v", opt_inc_intval, &verbose,
  103. * "Verbose mode (can be specified more than once)"),
  104. * OPT_WITHOUT_ARG("--usage", opt_usage_and_exit,
  105. * "args...\nA silly test program.",
  106. * "Print this message."),
  107. * OPT_ENDTABLE
  108. * };
  109. *
  110. * ...
  111. * opt_register_table(opts, NULL);
  112. */
  113. void opt_register_table(const struct opt_table *table, const char *desc);
  114. /**
  115. * opt_register_noarg - register an option with no arguments
  116. * @names: the names of the option eg. "--foo", "-f" or "--foo|-f|--foobar".
  117. * @cb: the callback when the option is found.
  118. * @arg: the argument to hand to @cb.
  119. * @desc: the verbose description of the option (for opt_usage()), or NULL.
  120. *
  121. * This is used for registering a single commandline option which takes
  122. * no argument.
  123. *
  124. * The callback is of type "char *cb(type *)", "char *cb(const type *)"
  125. * or "char *cb(void *)", where "type" is the type of the @arg
  126. * argument.
  127. *
  128. * If the @cb returns non-NULL, opt_parse() will stop parsing, use the
  129. * returned string to form an error message for errlog(), free() the
  130. * string and return false.
  131. */
  132. #define opt_register_noarg(names, cb, arg, desc) \
  133. _opt_register((names), OPT_CB_NOARG((cb), (arg)), (arg), (desc))
  134. /**
  135. * opt_register_arg - register an option with an arguments
  136. * @names: the names of the option eg. "--foo", "-f" or "--foo|-f|--foobar".
  137. * @cb: the callback when the option is found.
  138. * @show: the callback to print the value in get_usage (or NULL)
  139. * @arg: the argument to hand to @cb.
  140. * @desc: the verbose description of the option (for opt_usage()), or NULL.
  141. *
  142. * This is used for registering a single commandline option which takes
  143. * an argument.
  144. *
  145. * The callback is of type "char *cb(const char *, type *)",
  146. * "char *cb(const char *, const type *)" or "char *cb(const char *, void *)",
  147. * where "type" is the type of the @arg argument. The first argument to the
  148. * @cb is the argument found on the commandline.
  149. *
  150. * At least one of @longopt and @shortopt must be non-zero. If the
  151. * @cb returns false, opt_parse() will stop parsing and return false.
  152. *
  153. * Example:
  154. * static char *explode(const char *optarg, void *unused)
  155. * {
  156. * errx(1, "BOOM! %s", optarg);
  157. * }
  158. * ...
  159. * opt_register_arg("--explode|--boom", explode, NULL, NULL, opt_hidden);
  160. */
  161. #define opt_register_arg(names, cb, show, arg, desc) \
  162. _opt_register((names), OPT_CB_ARG((cb), (show), (arg)), (arg), (desc))
  163. /**
  164. * opt_parse - parse arguments.
  165. * @argc: pointer to argc
  166. * @argv: argv array.
  167. * @errlog: the function to print errors
  168. *
  169. * This iterates through the command line and calls callbacks registered with
  170. * opt_register_table()/opt_register_arg()/opt_register_noarg(). If there
  171. * are unknown options, missing arguments or a callback returns false, then
  172. * an error message is printed and false is returned.
  173. *
  174. * On success, argc and argv are adjusted so only the non-option elements
  175. * remain, and true is returned.
  176. *
  177. * Example:
  178. * if (!opt_parse(&argc, argv, opt_log_stderr)) {
  179. * printf("You screwed up, aborting!\n");
  180. * exit(1);
  181. * }
  182. *
  183. * See Also:
  184. * opt_log_stderr, opt_log_stderr_exit
  185. */
  186. bool opt_parse(int *argc, char *argv[], void (*errlog)(const char *fmt, ...));
  187. /**
  188. * opt_free_table - free the table.
  189. *
  190. * This frees the internal memory. Call this as the last
  191. * opt function.
  192. */
  193. void opt_free_table(void);
  194. /**
  195. * opt_log_stderr - print message to stderr.
  196. * @fmt: printf-style format.
  197. *
  198. * This is a helper for opt_parse, to print errors to stderr.
  199. *
  200. * See Also:
  201. * opt_log_stderr_exit
  202. */
  203. void opt_log_stderr(const char *fmt, ...);
  204. /**
  205. * opt_log_stderr_exit - print message to stderr, then exit(1)
  206. * @fmt: printf-style format.
  207. *
  208. * Just like opt_log_stderr, only then does exit(1). This means that
  209. * when handed to opt_parse, opt_parse will never return false.
  210. *
  211. * Example:
  212. * // This never returns false; just exits if there's an erorr.
  213. * opt_parse(&argc, argv, opt_log_stderr_exit);
  214. */
  215. void opt_log_stderr_exit(const char *fmt, ...);
  216. /**
  217. * opt_invalid_argument - helper to allocate an "Invalid argument '%s'" string
  218. * @arg: the argument which was invalid.
  219. *
  220. * This is a helper for callbacks to return a simple error string.
  221. */
  222. char *opt_invalid_argument(const char *arg);
  223. /**
  224. * opt_usage - create usage message
  225. * @argv0: the program name
  226. * @extra: extra details to print after the initial command, or NULL.
  227. *
  228. * Creates a usage message, with the program name, arguments, some extra details
  229. * and a table of all the options with their descriptions. If an option has
  230. * description opt_hidden, it is not shown here.
  231. *
  232. * If "extra" is NULL, then the extra information is taken from any
  233. * registered option which calls opt_usage_and_exit(). This avoids duplicating
  234. * that string in the common case.
  235. *
  236. * The result should be passed to free().
  237. */
  238. char *opt_usage(const char *argv0, const char *extra);
  239. /**
  240. * opt_hidden - string for undocumented options.
  241. *
  242. * This can be used as the desc parameter if you want an option not to be
  243. * shown by opt_usage().
  244. */
  245. extern const char opt_hidden[];
  246. /* Maximum length of arg to show in opt_usage */
  247. #define OPT_SHOW_LEN 80
  248. /* Standard helpers. You can write your own: */
  249. /* Sets the @b to true. */
  250. char *opt_set_bool(bool *b);
  251. /* Sets @b based on arg: (yes/no/true/false). */
  252. char *opt_set_bool_arg(const char *arg, bool *b);
  253. void opt_show_bool(char buf[OPT_SHOW_LEN], const bool *b);
  254. /* The inverse */
  255. char *opt_set_invbool(bool *b);
  256. void opt_show_invbool(char buf[OPT_SHOW_LEN], const bool *b);
  257. /* Sets @b based on !arg: (yes/no/true/false). */
  258. char *opt_set_invbool_arg(const char *arg, bool *b);
  259. /* Set a char *. */
  260. char *opt_set_charp(const char *arg, char **p);
  261. void opt_show_charp(char buf[OPT_SHOW_LEN], char *const *p);
  262. /* Set an integer value, various forms. Sets to 1 on arg == NULL. */
  263. char *opt_set_intval(const char *arg, int *i);
  264. void opt_show_intval(char buf[OPT_SHOW_LEN], const int *i);
  265. char *opt_set_floatval(const char *arg, float *f);
  266. void opt_show_floatval(char buf[OPT_SHOW_LEN], const float *f);
  267. char *opt_set_uintval(const char *arg, unsigned int *ui);
  268. void opt_show_uintval(char buf[OPT_SHOW_LEN], const unsigned int *ui);
  269. char *opt_set_longval(const char *arg, long *l);
  270. void opt_show_longval(char buf[OPT_SHOW_LEN], const long *l);
  271. char *opt_set_ulongval(const char *arg, unsigned long *ul);
  272. void opt_show_ulongval(char buf[OPT_SHOW_LEN], const unsigned long *ul);
  273. /* Increment. */
  274. char *opt_inc_intval(int *i);
  275. /* Display version string to stdout, exit(0). */
  276. char *opt_version_and_exit(const char *version);
  277. /* Display usage string to stdout, exit(0). */
  278. char *opt_usage_and_exit(const char *extra);
  279. /* Below here are private declarations. */
  280. /* You can use this directly to build tables, but the macros will ensure
  281. * consistency and type safety. */
  282. enum opt_type {
  283. OPT_NOARG = 1, /* -f|--foo */
  284. OPT_HASARG = 2, /* -f arg|--foo=arg|--foo arg */
  285. OPT_SUBTABLE = 4, /* Actually, longopt points to a subtable... */
  286. OPT_END = 8, /* End of the table. */
  287. };
  288. struct opt_table {
  289. const char *names; /* pipe-separated names, --longopt or -s */
  290. enum opt_type type;
  291. char *(*cb)(void *arg); /* OPT_NOARG */
  292. char *(*cb_arg)(const char *optarg, void *arg); /* OPT_HASARG */
  293. void (*show)(char buf[OPT_SHOW_LEN], const void *arg);
  294. union {
  295. const void *carg;
  296. void *arg;
  297. size_t tlen;
  298. } u;
  299. const char *desc;
  300. };
  301. /* Resolves to the four parameters for non-arg callbacks. */
  302. #define OPT_CB_NOARG(cb, arg) \
  303. OPT_NOARG, \
  304. typesafe_cb_cast3(char *(*)(void *), \
  305. char *(*)(typeof(*(arg))*), \
  306. char *(*)(const typeof(*(arg))*), \
  307. char *(*)(const void *), (cb)), \
  308. NULL, NULL
  309. /* Resolves to the four parameters for arg callbacks. */
  310. #define OPT_CB_ARG(cb, show, arg) \
  311. OPT_HASARG, NULL, \
  312. typesafe_cb_cast3(char *(*)(const char *,void *), \
  313. char *(*)(const char *, typeof(*(arg))*), \
  314. char *(*)(const char *, const typeof(*(arg))*), \
  315. char *(*)(const char *, const void *), \
  316. (cb)), \
  317. typesafe_cb_cast(void (*)(char buf[], const void *), \
  318. void (*)(char buf[], const typeof(*(arg))*), (show))
  319. /* Non-typesafe register function. */
  320. void _opt_register(const char *names, enum opt_type type,
  321. char *(*cb)(void *arg),
  322. char *(*cb_arg)(const char *optarg, void *arg),
  323. void (*show)(char buf[OPT_SHOW_LEN], const void *arg),
  324. const void *arg, const char *desc);
  325. /* We use this to get typechecking for OPT_SUBTABLE */
  326. static inline int _check_is_entry(struct opt_table *e UNUSED) { return 0; }
  327. #endif /* CCAN_OPT_H */