gnunet_getopt_lib.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2001-2013 GNUnet e.V.
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. /**
  18. * @author Christian Grothoff
  19. *
  20. * @file
  21. * Command line parsing and --help formatting
  22. *
  23. * @defgroup getopt Getopt library
  24. * Command line parsing and --help formatting
  25. * @{
  26. */
  27. #ifndef GNUNET_GETOPT_LIB_H
  28. #define GNUNET_GETOPT_LIB_H
  29. #ifdef __cplusplus
  30. extern "C"
  31. {
  32. #if 0 /* keep Emacsens' auto-indent happy */
  33. }
  34. #endif
  35. #endif
  36. #include "gnunet_configuration_lib.h"
  37. /**
  38. * @brief General context for command line processors.
  39. */
  40. struct GNUNET_GETOPT_CommandLineProcessorContext
  41. {
  42. /**
  43. * Name of the application
  44. */
  45. const char *binaryName;
  46. /**
  47. * Name of application with option summary
  48. */
  49. const char *binaryOptions;
  50. /**
  51. * Array with all command line options.
  52. */
  53. const struct GNUNET_GETOPT_CommandLineOption *allOptions;
  54. /**
  55. * Original command line
  56. */
  57. char *const *argv;
  58. /**
  59. * Total number of argv's.
  60. */
  61. unsigned int argc;
  62. /**
  63. * Current argument.
  64. */
  65. unsigned int currentArgument;
  66. };
  67. /**
  68. * @brief Process a command line option
  69. *
  70. * @param ctx context for all options
  71. * @param scls specific closure (for this processor)
  72. * @param option long name of the option (i.e. "config" for --config)
  73. * @param value argument, NULL if none was given
  74. * @return #GNUNET_OK to continue processing other options, #GNUNET_SYSERR to abort
  75. */
  76. typedef int
  77. (*GNUNET_GETOPT_CommandLineOptionProcessor) (struct
  78. GNUNET_GETOPT_CommandLineProcessorContext *ctx,
  79. void *scls,
  80. const char *option,
  81. const char *value);
  82. /**
  83. * @brief Definition of a command line option.
  84. */
  85. struct GNUNET_GETOPT_CommandLineOption
  86. {
  87. /**
  88. * Short name of the option.
  89. */
  90. const char shortName;
  91. /**
  92. * Long name of the option (may not be NULL)
  93. */
  94. const char *name;
  95. /**
  96. * Name of the argument for the user in help text
  97. */
  98. const char *argumentHelp;
  99. /**
  100. * Help text for the option (description)
  101. */
  102. const char *description;
  103. /**
  104. * Is an argument required? #GNUNET_NO (includes optional) or
  105. * #GNUNET_YES (required)
  106. */
  107. int require_argument;
  108. /**
  109. * Is the presence of this option mandatory?
  110. */
  111. int option_mandatory;
  112. /**
  113. * Handler for the option.
  114. */
  115. GNUNET_GETOPT_CommandLineOptionProcessor processor;
  116. /**
  117. * Function to call on @e scls to clean up after processing all
  118. * the arguments. Can be NULL.
  119. */
  120. void (*cleaner)(void *cls);
  121. /**
  122. * Specific closure to pass to the processor.
  123. */
  124. void *scls;
  125. };
  126. /**
  127. * Defining the option to print the command line
  128. * help text (-h option).
  129. *
  130. * @param about string with brief description of the application
  131. */
  132. struct GNUNET_GETOPT_CommandLineOption
  133. GNUNET_GETOPT_option_help (const char *about);
  134. /**
  135. * Define the option to print the version of
  136. * the application (-v option)
  137. *
  138. * @param version string with the version number
  139. */
  140. struct GNUNET_GETOPT_CommandLineOption
  141. GNUNET_GETOPT_option_version (const char *version);
  142. /**
  143. * Allow user to specify log file name (-l option)
  144. *
  145. * @param[out] logfn set to the name of the logfile
  146. */
  147. struct GNUNET_GETOPT_CommandLineOption
  148. GNUNET_GETOPT_option_logfile (char **logfn);
  149. /**
  150. * Allow user to specify a string.
  151. *
  152. * @param shortName short name of the option
  153. * @param name long name of the option
  154. * @param argumentHelp help text for the option argument
  155. * @param description long help text for the option
  156. * @param[out] str set to the string
  157. */
  158. struct GNUNET_GETOPT_CommandLineOption
  159. GNUNET_GETOPT_option_string (char shortName,
  160. const char *name,
  161. const char *argumentHelp,
  162. const char *description,
  163. char **str);
  164. /**
  165. * Allow user to specify a filename (automatically path expanded).
  166. *
  167. * @param shortName short name of the option
  168. * @param name long name of the option
  169. * @param argumentHelp help text for the option argument
  170. * @param description long help text for the option
  171. * @param[out] str set to the string
  172. */
  173. struct GNUNET_GETOPT_CommandLineOption
  174. GNUNET_GETOPT_option_filename (char shortName,
  175. const char *name,
  176. const char *argumentHelp,
  177. const char *description,
  178. char **str);
  179. /**
  180. * Allow user to specify a binary value using Crockford
  181. * Base32 encoding.
  182. *
  183. * @param shortName short name of the option
  184. * @param name long name of the option
  185. * @param argumentHelp help text for the option argument
  186. * @param description long help text for the option
  187. * @param[out] val binary value decoded from Crockford Base32-encoded argument
  188. * @param val_size size of @a val in bytes
  189. */
  190. struct GNUNET_GETOPT_CommandLineOption
  191. GNUNET_GETOPT_option_base32_fixed_size (char shortName,
  192. const char *name,
  193. const char *argumentHelp,
  194. const char *description,
  195. void *val,
  196. size_t val_size);
  197. /**
  198. * Allow user to specify a binary value using Crockford
  199. * Base32 encoding where the size of the binary value is
  200. * automatically determined from its type.
  201. *
  202. * @param shortName short name of the option
  203. * @param name long name of the option
  204. * @param argumentHelp help text for the option argument
  205. * @param description long help text for the option
  206. * @param[out] val binary value decoded from Crockford Base32-encoded argument;
  207. * size is determined by type (sizeof (*val)).
  208. */
  209. #define GNUNET_GETOPT_option_base32_auto(shortName,name,argumentHelp,description,val) \
  210. GNUNET_GETOPT_option_base32_fixed_size(shortName,name,argumentHelp,description,val,sizeof(*val))
  211. /**
  212. * Allow user to specify a flag (which internally means setting
  213. * an integer to 1/#GNUNET_YES/#GNUNET_OK.
  214. *
  215. * @param shortName short name of the option
  216. * @param name long name of the option
  217. * @param description long help text for the option
  218. * @param[out] val set to 1 if the option is present
  219. */
  220. struct GNUNET_GETOPT_CommandLineOption
  221. GNUNET_GETOPT_option_flag (char shortName,
  222. const char *name,
  223. const char *description,
  224. int *val);
  225. /**
  226. * Allow user to specify an `unsigned int`.
  227. *
  228. * @param shortName short name of the option
  229. * @param name long name of the option
  230. * @param argumentHelp help text for the option argument
  231. * @param description long help text for the option
  232. * @param[out] val set to the value specified at the command line
  233. */
  234. struct GNUNET_GETOPT_CommandLineOption
  235. GNUNET_GETOPT_option_uint (char shortName,
  236. const char *name,
  237. const char *argumentHelp,
  238. const char *description,
  239. unsigned int *val);
  240. /**
  241. * Allow user to specify an `unsigned long long`.
  242. *
  243. * @param shortName short name of the option
  244. * @param name long name of the option
  245. * @param argumentHelp help text for the option argument
  246. * @param description long help text for the option
  247. * @param[out] val set to the value specified at the command line
  248. */
  249. struct GNUNET_GETOPT_CommandLineOption
  250. GNUNET_GETOPT_option_ulong (char shortName,
  251. const char *name,
  252. const char *argumentHelp,
  253. const char *description,
  254. unsigned long long *val);
  255. /**
  256. * Allow user to specify a `struct GNUNET_TIME_Relative`
  257. * (using human-readable "fancy" time).
  258. *
  259. * @param shortName short name of the option
  260. * @param name long name of the option
  261. * @param argumentHelp help text for the option argument
  262. * @param description long help text for the option
  263. * @param[out] val set to the time specified at the command line
  264. */
  265. struct GNUNET_GETOPT_CommandLineOption
  266. GNUNET_GETOPT_option_relative_time (char shortName,
  267. const char *name,
  268. const char *argumentHelp,
  269. const char *description,
  270. struct GNUNET_TIME_Relative *val);
  271. /**
  272. * Allow user to specify a `struct GNUNET_TIME_Absolute`
  273. * (using human-readable "fancy" time).
  274. *
  275. * @param shortName short name of the option
  276. * @param name long name of the option
  277. * @param argumentHelp help text for the option argument
  278. * @param description long help text for the option
  279. * @param[out] val set to the time specified at the command line
  280. */
  281. struct GNUNET_GETOPT_CommandLineOption
  282. GNUNET_GETOPT_option_absolute_time (char shortName,
  283. const char *name,
  284. const char *argumentHelp,
  285. const char *description,
  286. struct GNUNET_TIME_Absolute *val);
  287. /**
  288. * Increment @a val each time the option flag is given by one.
  289. *
  290. * @param shortName short name of the option
  291. * @param name long name of the option
  292. * @param argumentHelp help text for the option argument
  293. * @param description long help text for the option
  294. * @param[out] val set to 1 if the option is present
  295. */
  296. struct GNUNET_GETOPT_CommandLineOption
  297. GNUNET_GETOPT_option_increment_uint (char shortName,
  298. const char *name,
  299. const char *description,
  300. unsigned int *val);
  301. /**
  302. * Define the '-L' log level option. Note that we do not check
  303. * that the log level is valid here.
  304. *
  305. * @param[out] level set to the log level
  306. */
  307. struct GNUNET_GETOPT_CommandLineOption
  308. GNUNET_GETOPT_option_loglevel (char **level);
  309. /**
  310. * Define the '-V' verbosity option. Using the option more
  311. * than once increments @a level each time.
  312. *
  313. * @param[out] level set to the verbosity level
  314. */
  315. struct GNUNET_GETOPT_CommandLineOption
  316. GNUNET_GETOPT_option_verbose (unsigned int *level);
  317. /**
  318. * Allow user to specify log file name (-l option)
  319. *
  320. * @param[out] logfn set to the name of the logfile
  321. */
  322. struct GNUNET_GETOPT_CommandLineOption
  323. GNUNET_GETOPT_option_logfile (char **logfn);
  324. /**
  325. * Allow user to specify configuration file name (-c option)
  326. *
  327. * @param[out] fn set to the name of the configuration file
  328. */
  329. struct GNUNET_GETOPT_CommandLineOption
  330. GNUNET_GETOPT_option_cfgfile (char **fn);
  331. /**
  332. * Make the given option mandatory.
  333. *
  334. * @param opt option to modify
  335. * @return @a opt with the mandatory flag set.
  336. */
  337. struct GNUNET_GETOPT_CommandLineOption
  338. GNUNET_GETOPT_option_mandatory (struct GNUNET_GETOPT_CommandLineOption opt);
  339. /**
  340. * Marker for the end of the list of options.
  341. */
  342. #define GNUNET_GETOPT_OPTION_END \
  343. { '\0', NULL, NULL, NULL, 0, 0, NULL, NULL, NULL }
  344. /**
  345. * Parse the command line.
  346. *
  347. * @param binaryOptions Name of application with option summary
  348. * @param allOptions defined options and handlers
  349. * @param argc number of arguments in @a argv
  350. * @param argv actual arguments
  351. * @return index into argv with first non-option
  352. * argument, or #GNUNET_SYSERR on error
  353. */
  354. int
  355. GNUNET_GETOPT_run (const char *binaryOptions,
  356. const struct GNUNET_GETOPT_CommandLineOption *allOptions,
  357. unsigned int argc,
  358. char *const *argv);
  359. #if 0 /* keep Emacsens' auto-indent happy */
  360. {
  361. #endif
  362. #ifdef __cplusplus
  363. }
  364. #endif
  365. /* ifndef GNUNET_GETOPT_LIB_H */
  366. #endif
  367. /** @} */ /* end of group getopt */
  368. /* end of gnunet_getopt_lib.h */