goption.h 16 KB

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