goption.h 16 KB

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