gnunet_getopt_lib.h 14 KB

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