dropt.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /** dropt.h
  2. *
  3. * A deliberately rudimentary command-line option parser.
  4. *
  5. * Version 1.1.1
  6. *
  7. * Copyright (c) 2006-2012 James D. Lin <jameslin@cal.berkeley.edu>
  8. *
  9. * The latest version of this file can be downloaded from:
  10. * <http://www.taenarum.com/software/dropt/>
  11. *
  12. * This software is provided 'as-is', without any express or implied
  13. * warranty. In no event will the authors be held liable for any damages
  14. * arising from the use of this software.
  15. *
  16. * Permission is granted to anyone to use this software for any purpose,
  17. * including commercial applications, and to alter it and redistribute it
  18. * freely, subject to the following restrictions:
  19. *
  20. * 1. The origin of this software must not be misrepresented; you must not
  21. * claim that you wrote the original software. If you use this software
  22. * in a product, an acknowledgment in the product documentation would be
  23. * appreciated but is not required.
  24. *
  25. * 2. Altered source versions must be plainly marked as such, and must not be
  26. * misrepresented as being the original software.
  27. *
  28. * 3. This notice may not be removed or altered from any source distribution.
  29. */
  30. #ifndef DROPT_H
  31. #define DROPT_H
  32. #include <stdio.h>
  33. #include <wchar.h>
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. #ifndef DROPT_USE_WCHAR
  38. #if defined _UNICODE && (defined _MSC_VER || defined DROPT_NO_STRING_BUFFERS)
  39. #define DROPT_USE_WCHAR 1
  40. #endif
  41. #endif
  42. #ifdef DROPT_USE_WCHAR
  43. /* This may be used for both char and string literals. */
  44. #define DROPT_TEXT_LITERAL(s) L ## s
  45. typedef wchar_t dropt_char;
  46. #else
  47. #define DROPT_TEXT_LITERAL(s) s
  48. typedef char dropt_char;
  49. #endif
  50. enum
  51. {
  52. /* Errors in the range [0x00, 0x7F] are reserved for dropt. */
  53. dropt_error_none,
  54. dropt_error_unknown,
  55. dropt_error_bad_configuration,
  56. dropt_error_insufficient_memory,
  57. dropt_error_invalid_option,
  58. dropt_error_insufficient_arguments,
  59. dropt_error_mismatch,
  60. dropt_error_overflow,
  61. dropt_error_underflow,
  62. /* Errors in the range [0x80, 0xFFFF] are free for clients to use. */
  63. dropt_error_custom_start = 0x80,
  64. dropt_error_custom_last = 0xFFFF
  65. };
  66. typedef unsigned int dropt_error;
  67. typedef unsigned char dropt_bool;
  68. /* Opaque. */
  69. typedef struct dropt_context dropt_context;
  70. /** dropt_option_handler_func callbacks are responsible for parsing
  71. * individual options.
  72. *
  73. * dropt_option_handler_decl may be used for declaring the callback
  74. * functions; dropt_option_handler_func is the actual function pointer
  75. * type.
  76. *
  77. * optionArgument will be NULL if no argument is specified for an option.
  78. * It will be the empty string if the user explicitly passed an empty
  79. * string as the argument (e.g. --option="").
  80. *
  81. * An option that doesn't expect an argument still can receive a non-NULL
  82. * value for optionArgument if the user explicitly specified one (e.g.
  83. * --option=arg).
  84. *
  85. * If the option's argument is optional, the handler might be called
  86. * twice: once with a candidate argument, and if that argument is rejected
  87. * by the handler, again with no argument. Handlers should be aware of
  88. * this if they have side-effects.
  89. *
  90. * handlerData is the client-specified value specified in the dropt_option
  91. * table.
  92. */
  93. typedef dropt_error dropt_option_handler_decl(dropt_context* context,
  94. const dropt_char* optionArgument,
  95. void* handlerData);
  96. typedef dropt_option_handler_decl* dropt_option_handler_func;
  97. /** dropt_error_handler_func callbacks are responsible for generating error
  98. * messages. The returned string must be allocated on the heap and must
  99. * be freeable with free().
  100. */
  101. typedef dropt_char* (*dropt_error_handler_func)(dropt_error error,
  102. const dropt_char* optionName,
  103. const dropt_char* optionArgument,
  104. void* handlerData);
  105. /** dropt_strncmp_func callbacks allow callers to provide their own (possibly
  106. * case-insensitive) string comparison function.
  107. */
  108. typedef int (*dropt_strncmp_func)(const dropt_char* s, const dropt_char* t, size_t n);
  109. /** Properties defining each option:
  110. *
  111. * short_name:
  112. * The option's short name (e.g. the 'h' in -h).
  113. * Use '\0' if the option has no short name.
  114. *
  115. * long_name:
  116. * The option's long name (e.g. "help" in --help).
  117. * Use NULL if the option has no long name.
  118. *
  119. * description:
  120. * The description shown when generating help.
  121. * May be NULL for undocumented options.
  122. *
  123. * arg_description:
  124. * The description for the option's argument (e.g. --option=argument
  125. * or --option argument), printed when generating help. If NULL, the
  126. * option does not take an argument.
  127. *
  128. * handler:
  129. * The handler callback and data invoked in response to encountering
  130. * the option.
  131. *
  132. * handler_data:
  133. * Callback data for the handler. For typical handlers, this is
  134. * usually the address of a variable for the handler to modify.
  135. *
  136. * attr:
  137. * Miscellaneous attributes. See below.
  138. */
  139. typedef struct dropt_option
  140. {
  141. dropt_char short_name;
  142. const dropt_char* long_name;
  143. const dropt_char* description;
  144. const dropt_char* arg_description;
  145. dropt_option_handler_func handler;
  146. void* handler_data;
  147. unsigned int attr;
  148. } dropt_option;
  149. /** Bitwise flags for option attributes:
  150. *
  151. * dropt_attr_halt:
  152. * Stop processing when this option is encountered.
  153. *
  154. * dropt_attr_hidden:
  155. * Don't list the option when generating help. Use this for
  156. * undocumented options.
  157. *
  158. * dropt_attr_optional_val:
  159. * The option's argument is optional. If an option has this
  160. * attribute, the handler callback may be invoked twice (once with a
  161. * potential argument, and if that fails, again with a NULL argument).
  162. */
  163. enum
  164. {
  165. dropt_attr_halt = (1 << 0),
  166. dropt_attr_hidden = (1 << 1),
  167. dropt_attr_optional_val = (1 << 2)
  168. };
  169. typedef struct dropt_help_params
  170. {
  171. unsigned int indent;
  172. unsigned int description_start_column;
  173. dropt_bool blank_lines_between_options;
  174. } dropt_help_params;
  175. dropt_context* dropt_new_context(const dropt_option* options);
  176. void dropt_free_context(dropt_context* context);
  177. const dropt_option* dropt_get_options(const dropt_context* context);
  178. void dropt_set_error_handler(dropt_context* context,
  179. dropt_error_handler_func handler, void* handlerData);
  180. void dropt_set_strncmp(dropt_context* context, dropt_strncmp_func cmp);
  181. /* Use this only for backward compatibility purposes. */
  182. void dropt_allow_concatenated_arguments(dropt_context* context, dropt_bool allow);
  183. dropt_char** dropt_parse(dropt_context* context, int argc, dropt_char** argv);
  184. dropt_error dropt_get_error(const dropt_context* context);
  185. void dropt_get_error_details(const dropt_context* context,
  186. dropt_char** optionName,
  187. dropt_char** optionArgument);
  188. const dropt_char* dropt_get_error_message(dropt_context* context);
  189. void dropt_clear_error(dropt_context* context);
  190. #ifndef DROPT_NO_STRING_BUFFERS
  191. dropt_char* dropt_default_error_handler(dropt_error error,
  192. const dropt_char* optionName,
  193. const dropt_char* optionArgument);
  194. void dropt_init_help_params(dropt_help_params* helpParams);
  195. dropt_char* dropt_get_help(const dropt_context* context,
  196. const dropt_help_params* helpParams);
  197. void dropt_print_help(FILE* f, const dropt_context* context,
  198. const dropt_help_params* helpParams);
  199. #endif
  200. /* Stock option handlers for common types. */
  201. dropt_option_handler_decl dropt_handle_bool;
  202. dropt_option_handler_decl dropt_handle_verbose_bool;
  203. dropt_option_handler_decl dropt_handle_int;
  204. dropt_option_handler_decl dropt_handle_uint;
  205. dropt_option_handler_decl dropt_handle_double;
  206. dropt_option_handler_decl dropt_handle_string;
  207. #define DROPT_MISUSE(message) dropt_misuse(message, __FILE__, __LINE__)
  208. void dropt_misuse(const char* message, const char* filename, int line);
  209. #ifdef __cplusplus
  210. } /* extern "C" */
  211. #endif
  212. #endif /* DROPT_H */