goption.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /* goption.h - Option parser
  2. *
  3. * Copyright (C) 2004 Anders Carlsson <[email protected]>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Library General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Library General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Library General Public
  16. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef __G_OPTION_H__
  19. #define __G_OPTION_H__
  20. #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
  21. #error "Only <glib.h> can be included directly."
  22. #endif
  23. #include <glib/gerror.h>
  24. #include <glib/gquark.h>
  25. G_BEGIN_DECLS
  26. /**
  27. * GOptionContext:
  28. *
  29. * A <structname>GOptionContext</structname> struct defines which options
  30. * are accepted by the commandline option parser. The struct has only private
  31. * fields and should not be directly accessed.
  32. */
  33. typedef struct _GOptionContext GOptionContext;
  34. /**
  35. * GOptionGroup:
  36. *
  37. * A <structname>GOptionGroup</structname> struct defines the options in a single
  38. * group. The struct has only private fields and should not be directly accessed.
  39. *
  40. * All options in a group share the same translation function. Libraries which
  41. * need to parse commandline options are expected to provide a function for
  42. * getting a <structname>GOptionGroup</structname> holding their options, which
  43. * the application can then add to its #GOptionContext.
  44. */
  45. typedef struct _GOptionGroup GOptionGroup;
  46. typedef struct _GOptionEntry GOptionEntry;
  47. /**
  48. * GOptionFlags:
  49. * @G_OPTION_FLAG_HIDDEN: The option doesn't appear in `--help` output.
  50. * @G_OPTION_FLAG_IN_MAIN: The option appears in the main section of the
  51. * `--help` output, even if it is defined in a group.
  52. * @G_OPTION_FLAG_REVERSE: For options of the %G_OPTION_ARG_NONE kind, this
  53. * flag indicates that the sense of the option is reversed.
  54. * @G_OPTION_FLAG_NO_ARG: For options of the %G_OPTION_ARG_CALLBACK kind,
  55. * this flag indicates that the callback does not take any argument
  56. * (like a %G_OPTION_ARG_NONE option). Since 2.8
  57. * @G_OPTION_FLAG_FILENAME: For options of the %G_OPTION_ARG_CALLBACK
  58. * kind, this flag indicates that the argument should be passed to the
  59. * callback in the GLib filename encoding rather than UTF-8. Since 2.8
  60. * @G_OPTION_FLAG_OPTIONAL_ARG: For options of the %G_OPTION_ARG_CALLBACK
  61. * kind, this flag indicates that the argument supply is optional.
  62. * If no argument is given then data of %GOptionParseFunc will be
  63. * set to NULL. Since 2.8
  64. * @G_OPTION_FLAG_NOALIAS: This flag turns off the automatic conflict
  65. * resolution which prefixes long option names with `groupname-` if
  66. * there is a conflict. This option should only be used in situations
  67. * where aliasing is necessary to model some legacy commandline interface.
  68. * It is not safe to use this option, unless all option groups are under
  69. * your direct control. Since 2.8.
  70. *
  71. * Flags which modify individual options.
  72. */
  73. typedef enum
  74. {
  75. G_OPTION_FLAG_HIDDEN = 1 << 0,
  76. G_OPTION_FLAG_IN_MAIN = 1 << 1,
  77. G_OPTION_FLAG_REVERSE = 1 << 2,
  78. G_OPTION_FLAG_NO_ARG = 1 << 3,
  79. G_OPTION_FLAG_FILENAME = 1 << 4,
  80. G_OPTION_FLAG_OPTIONAL_ARG = 1 << 5,
  81. G_OPTION_FLAG_NOALIAS = 1 << 6
  82. } GOptionFlags;
  83. /**
  84. * GOptionArg:
  85. * @G_OPTION_ARG_NONE: No extra argument. This is useful for simple flags.
  86. * @G_OPTION_ARG_STRING: The option takes a string argument.
  87. * @G_OPTION_ARG_INT: The option takes an integer argument.
  88. * @G_OPTION_ARG_CALLBACK: The option provides a callback to parse the
  89. * extra argument.
  90. * @G_OPTION_ARG_FILENAME: The option takes a filename as argument.
  91. * @G_OPTION_ARG_STRING_ARRAY: The option takes a string argument, multiple
  92. * uses of the option are collected into an array of strings.
  93. * @G_OPTION_ARG_FILENAME_ARRAY: The option takes a filename as argument,
  94. * multiple uses of the option are collected into an array of strings.
  95. * @G_OPTION_ARG_DOUBLE: The option takes a double argument. The argument
  96. * can be formatted either for the user's locale or for the "C" locale.
  97. * Since 2.12
  98. * @G_OPTION_ARG_INT64: The option takes a 64-bit integer. Like
  99. * %G_OPTION_ARG_INT but for larger numbers. The number can be in
  100. * decimal base, or in hexadecimal (when prefixed with `0x`, for
  101. * example, `0xffffffff`). Since 2.12
  102. *
  103. * The #GOptionArg enum values determine which type of extra argument the
  104. * options expect to find. If an option expects an extra argument, it can
  105. * be specified in several ways; with a short option: `-x arg`, with a long
  106. * option: `--name arg` or combined in a single argument: `--name=arg`.
  107. */
  108. typedef enum
  109. {
  110. G_OPTION_ARG_NONE,
  111. G_OPTION_ARG_STRING,
  112. G_OPTION_ARG_INT,
  113. G_OPTION_ARG_CALLBACK,
  114. G_OPTION_ARG_FILENAME,
  115. G_OPTION_ARG_STRING_ARRAY,
  116. G_OPTION_ARG_FILENAME_ARRAY,
  117. G_OPTION_ARG_DOUBLE,
  118. G_OPTION_ARG_INT64
  119. } GOptionArg;
  120. /**
  121. * GOptionArgFunc:
  122. * @option_name: The name of the option being parsed. This will be either a
  123. * single dash followed by a single letter (for a short name) or two dashes
  124. * followed by a long option name.
  125. * @value: The value to be parsed.
  126. * @data: User data added to the #GOptionGroup containing the option when it
  127. * was created with g_option_group_new()
  128. * @error: A return location for errors. The error code %G_OPTION_ERROR_FAILED
  129. * is intended to be used for errors in #GOptionArgFunc callbacks.
  130. *
  131. * The type of function to be passed as callback for %G_OPTION_ARG_CALLBACK
  132. * options.
  133. *
  134. * Returns: %TRUE if the option was successfully parsed, %FALSE if an error
  135. * occurred, in which case @error should be set with g_set_error()
  136. */
  137. typedef gboolean (*GOptionArgFunc) (const gchar *option_name,
  138. const gchar *value,
  139. gpointer data,
  140. GError **error);
  141. /**
  142. * GOptionParseFunc:
  143. * @context: The active #GOptionContext
  144. * @group: The group to which the function belongs
  145. * @data: User data added to the #GOptionGroup containing the option when it
  146. * was created with g_option_group_new()
  147. * @error: A return location for error details
  148. *
  149. * The type of function that can be called before and after parsing.
  150. *
  151. * Returns: %TRUE if the function completed successfully, %FALSE if an error
  152. * occurred, in which case @error should be set with g_set_error()
  153. */
  154. typedef gboolean (*GOptionParseFunc) (GOptionContext *context,
  155. GOptionGroup *group,
  156. gpointer data,
  157. GError **error);
  158. /**
  159. * GOptionErrorFunc:
  160. * @context: The active #GOptionContext
  161. * @group: The group to which the function belongs
  162. * @data: User data added to the #GOptionGroup containing the option when it
  163. * was created with g_option_group_new()
  164. * @error: The #GError containing details about the parse error
  165. *
  166. * The type of function to be used as callback when a parse error occurs.
  167. */
  168. typedef void (*GOptionErrorFunc) (GOptionContext *context,
  169. GOptionGroup *group,
  170. gpointer data,
  171. GError **error);
  172. /**
  173. * G_OPTION_ERROR:
  174. *
  175. * Error domain for option parsing. Errors in this domain will
  176. * be from the #GOptionError enumeration. See #GError for information on
  177. * error domains.
  178. */
  179. #define G_OPTION_ERROR (g_option_error_quark ())
  180. /**
  181. * GOptionError:
  182. * @G_OPTION_ERROR_UNKNOWN_OPTION: An option was not known to the parser.
  183. * This error will only be reported, if the parser hasn't been instructed
  184. * to ignore unknown options, see g_option_context_set_ignore_unknown_options().
  185. * @G_OPTION_ERROR_BAD_VALUE: A value couldn't be parsed.
  186. * @G_OPTION_ERROR_FAILED: A #GOptionArgFunc callback failed.
  187. *
  188. * Error codes returned by option parsing.
  189. */
  190. typedef enum
  191. {
  192. G_OPTION_ERROR_UNKNOWN_OPTION,
  193. G_OPTION_ERROR_BAD_VALUE,
  194. G_OPTION_ERROR_FAILED
  195. } GOptionError;
  196. GLIB_AVAILABLE_IN_ALL
  197. GQuark g_option_error_quark (void);
  198. /**
  199. * GOptionEntry:
  200. * @long_name: The long name of an option can be used to specify it
  201. * in a commandline as `--long_name`. Every option must have a
  202. * long name. To resolve conflicts if multiple option groups contain
  203. * the same long name, it is also possible to specify the option as
  204. * `--groupname-long_name`.
  205. * @short_name: If an option has a short name, it can be specified
  206. * `-short_name` in a commandline. @short_name must be a printable
  207. * ASCII character different from '-', or zero if the option has no
  208. * short name.
  209. * @flags: Flags from #GOptionFlags
  210. * @arg: The type of the option, as a #GOptionArg
  211. * @arg_data: If the @arg type is %G_OPTION_ARG_CALLBACK, then @arg_data
  212. * must point to a #GOptionArgFunc callback function, which will be
  213. * called to handle the extra argument. Otherwise, @arg_data is a
  214. * pointer to a location to store the value, the required type of
  215. * the location depends on the @arg type:
  216. * - %G_OPTION_ARG_NONE: %gboolean
  217. * - %G_OPTION_ARG_STRING: %gchar*
  218. * - %G_OPTION_ARG_INT: %gint
  219. * - %G_OPTION_ARG_FILENAME: %gchar*
  220. * - %G_OPTION_ARG_STRING_ARRAY: %gchar**
  221. * - %G_OPTION_ARG_FILENAME_ARRAY: %gchar**
  222. * - %G_OPTION_ARG_DOUBLE: %gdouble
  223. * If @arg type is %G_OPTION_ARG_STRING or %G_OPTION_ARG_FILENAME,
  224. * the location will contain a newly allocated string if the option
  225. * was given. That string needs to be freed by the callee using g_free().
  226. * Likewise if @arg type is %G_OPTION_ARG_STRING_ARRAY or
  227. * %G_OPTION_ARG_FILENAME_ARRAY, the data should be freed using g_strfreev().
  228. * @description: the description for the option in `--help`
  229. * output. The @description is translated using the @translate_func
  230. * of the group, see g_option_group_set_translation_domain().
  231. * @arg_description: The placeholder to use for the extra argument parsed
  232. * by the option in `--help` output. The @arg_description is translated
  233. * using the @translate_func of the group, see
  234. * g_option_group_set_translation_domain().
  235. *
  236. * A GOptionEntry struct defines a single option. To have an effect, they
  237. * must be added to a #GOptionGroup with g_option_context_add_main_entries()
  238. * or g_option_group_add_entries().
  239. */
  240. struct _GOptionEntry
  241. {
  242. const gchar *long_name;
  243. gchar short_name;
  244. gint flags;
  245. GOptionArg arg;
  246. gpointer arg_data;
  247. const gchar *description;
  248. const gchar *arg_description;
  249. };
  250. /**
  251. * G_OPTION_REMAINING:
  252. *
  253. * If a long option in the main group has this name, it is not treated as a
  254. * regular option. Instead it collects all non-option arguments which would
  255. * otherwise be left in `argv`. The option must be of type
  256. * %G_OPTION_ARG_CALLBACK, %G_OPTION_ARG_STRING_ARRAY
  257. * or %G_OPTION_ARG_FILENAME_ARRAY.
  258. *
  259. *
  260. * Using #G_OPTION_REMAINING instead of simply scanning `argv`
  261. * for leftover arguments has the advantage that GOption takes care of
  262. * necessary encoding conversions for strings or filenames.
  263. *
  264. * Since: 2.6
  265. */
  266. #define G_OPTION_REMAINING ""
  267. GLIB_AVAILABLE_IN_ALL
  268. GOptionContext *g_option_context_new (const gchar *parameter_string);
  269. GLIB_AVAILABLE_IN_ALL
  270. void g_option_context_set_summary (GOptionContext *context,
  271. const gchar *summary);
  272. GLIB_AVAILABLE_IN_ALL
  273. const gchar * g_option_context_get_summary (GOptionContext *context);
  274. GLIB_AVAILABLE_IN_ALL
  275. void g_option_context_set_description (GOptionContext *context,
  276. const gchar *description);
  277. GLIB_AVAILABLE_IN_ALL
  278. const gchar * g_option_context_get_description (GOptionContext *context);
  279. GLIB_AVAILABLE_IN_ALL
  280. void g_option_context_free (GOptionContext *context);
  281. GLIB_AVAILABLE_IN_ALL
  282. void g_option_context_set_help_enabled (GOptionContext *context,
  283. gboolean help_enabled);
  284. GLIB_AVAILABLE_IN_ALL
  285. gboolean g_option_context_get_help_enabled (GOptionContext *context);
  286. GLIB_AVAILABLE_IN_ALL
  287. void g_option_context_set_ignore_unknown_options (GOptionContext *context,
  288. gboolean ignore_unknown);
  289. GLIB_AVAILABLE_IN_ALL
  290. gboolean g_option_context_get_ignore_unknown_options (GOptionContext *context);
  291. GLIB_AVAILABLE_IN_ALL
  292. void g_option_context_add_main_entries (GOptionContext *context,
  293. const GOptionEntry *entries,
  294. const gchar *translation_domain);
  295. GLIB_AVAILABLE_IN_ALL
  296. gboolean g_option_context_parse (GOptionContext *context,
  297. gint *argc,
  298. gchar ***argv,
  299. GError **error);
  300. GLIB_AVAILABLE_IN_2_40
  301. gboolean g_option_context_parse_strv (GOptionContext *context,
  302. gchar ***arguments,
  303. GError **error);
  304. GLIB_AVAILABLE_IN_ALL
  305. void g_option_context_set_translate_func (GOptionContext *context,
  306. GTranslateFunc func,
  307. gpointer data,
  308. GDestroyNotify destroy_notify);
  309. GLIB_AVAILABLE_IN_ALL
  310. void g_option_context_set_translation_domain (GOptionContext *context,
  311. const gchar *domain);
  312. GLIB_AVAILABLE_IN_ALL
  313. void g_option_context_add_group (GOptionContext *context,
  314. GOptionGroup *group);
  315. GLIB_AVAILABLE_IN_ALL
  316. void g_option_context_set_main_group (GOptionContext *context,
  317. GOptionGroup *group);
  318. GLIB_AVAILABLE_IN_ALL
  319. GOptionGroup *g_option_context_get_main_group (GOptionContext *context);
  320. GLIB_AVAILABLE_IN_ALL
  321. gchar *g_option_context_get_help (GOptionContext *context,
  322. gboolean main_help,
  323. GOptionGroup *group);
  324. GLIB_AVAILABLE_IN_ALL
  325. GOptionGroup *g_option_group_new (const gchar *name,
  326. const gchar *description,
  327. const gchar *help_description,
  328. gpointer user_data,
  329. GDestroyNotify destroy);
  330. GLIB_AVAILABLE_IN_ALL
  331. void g_option_group_set_parse_hooks (GOptionGroup *group,
  332. GOptionParseFunc pre_parse_func,
  333. GOptionParseFunc post_parse_func);
  334. GLIB_AVAILABLE_IN_ALL
  335. void g_option_group_set_error_hook (GOptionGroup *group,
  336. GOptionErrorFunc error_func);
  337. GLIB_AVAILABLE_IN_ALL
  338. void g_option_group_free (GOptionGroup *group);
  339. GLIB_AVAILABLE_IN_ALL
  340. void g_option_group_add_entries (GOptionGroup *group,
  341. const GOptionEntry *entries);
  342. GLIB_AVAILABLE_IN_ALL
  343. void g_option_group_set_translate_func (GOptionGroup *group,
  344. GTranslateFunc func,
  345. gpointer data,
  346. GDestroyNotify destroy_notify);
  347. GLIB_AVAILABLE_IN_ALL
  348. void g_option_group_set_translation_domain (GOptionGroup *group,
  349. const gchar *domain);
  350. G_END_DECLS
  351. #endif /* __G_OPTION_H__ */