opt.h 12 KB

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