goption.h 16 KB

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