You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

371 lines
15 KiB

  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, write to the
  17. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  18. * Boston, MA 02111-1307, USA.
  19. */
  20. #if defined(G_DISABLE_SINGLE_INCLUDES) && !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
  21. #error "Only <glib.h> can be included directly."
  22. #endif
  23. #ifndef __G_OPTION_H__
  24. #define __G_OPTION_H__
  25. #include <glib/gerror.h>
  26. #include <glib/gquark.h>
  27. G_BEGIN_DECLS
  28. /**
  29. * GOptionContext:
  30. *
  31. * A <structname>GOptionContext</structname> 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 <structname>GOptionGroup</structname> 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 <structname>GOptionGroup</structname> 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_HIDDEN: The option doesn't appear in <option>--help</option>
  52. * output.
  53. * @G_OPTION_FLAG_IN_MAIN: The option appears in the main section of the
  54. * <option>--help</option> output, even if it is defined in a group.
  55. * @G_OPTION_FLAG_REVERSE: For options of the %G_OPTION_ARG_NONE kind, this flag
  56. * indicates that the sense of the option is reversed.
  57. * @G_OPTION_FLAG_NO_ARG: For options of the %G_OPTION_ARG_CALLBACK kind,
  58. * this flag indicates that the callback does not take any argument
  59. * (like a %G_OPTION_ARG_NONE option). Since 2.8
  60. * @G_OPTION_FLAG_FILENAME: For options of the %G_OPTION_ARG_CALLBACK
  61. * kind, this flag indicates that the argument should be passed to the
  62. * callback in the GLib filename encoding rather than UTF-8. Since 2.8
  63. * @G_OPTION_FLAG_OPTIONAL_ARG: For options of the %G_OPTION_ARG_CALLBACK
  64. * kind, this flag indicates that the argument supply is optional. If no argument
  65. * is given then data of %GOptionParseFunc will be set to NULL. Since 2.8
  66. * @G_OPTION_FLAG_NOALIAS: This flag turns off the automatic conflict resolution
  67. * which prefixes long option names with <literal>groupname-</literal> if
  68. * there is a conflict. This option should only be used in situations where
  69. * aliasing is necessary to model some legacy commandline interface. It is
  70. * not safe to use this option, unless all option groups are under your
  71. * direct control. Since 2.8.
  72. *
  73. * Flags which modify individual options.
  74. */
  75. typedef enum
  76. {
  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 to parse the
  91. * 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. Since 2.12
  99. * @G_OPTION_ARG_INT64: The option takes a 64-bit integer. Like %G_OPTION_ARG_INT
  100. * but for larger numbers. The number can be in decimal base, or in hexadecimal
  101. * (when prefixed with <literal>0x</literal>, for example, <literal>0xffffffff</literal>).
  102. * Since 2.12
  103. *
  104. * The #GOptionArg enum values determine which type of extra argument the
  105. * options expect to find. If an option expects an extra argument, it
  106. * can be specified in several ways; with a short option:
  107. * <option>-x arg</option>, with a long option: <option>--name arg</option>
  108. * or combined in a single argument: <option>--name=arg</option>.
  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. GQuark g_option_error_quark (void);
  199. /**
  200. * GOptionEntry:
  201. * @long_name: The long name of an option can be used to specify it
  202. * in a commandline as --<replaceable>long_name</replaceable>. Every
  203. * option must have a long name. To resolve conflicts if multiple
  204. * option groups contain the same long name, it is also possible to
  205. * specify the option as
  206. * --<replaceable>groupname</replaceable>-<replaceable>long_name</replaceable>.
  207. * @short_name: If an option has a short name, it can be specified
  208. * -<replaceable>short_name</replaceable> in a commandline. @short_name must be
  209. * a printable 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 must
  214. * point to a #GOptionArgFunc callback function, which will be called to handle
  215. * the extra argument. Otherwise, @arg_data is a pointer to a location to store
  216. * the value, the required type of the location depends on the @arg type:
  217. * <variablelist>
  218. * <varlistentry>
  219. * <term>%G_OPTION_ARG_NONE</term>
  220. * <listitem><para>%gboolean</para></listitem>
  221. * </varlistentry>
  222. * <varlistentry>
  223. * <term>%G_OPTION_ARG_STRING</term>
  224. * <listitem><para>%gchar*</para></listitem>
  225. * </varlistentry>
  226. * <varlistentry>
  227. * <term>%G_OPTION_ARG_INT</term>
  228. * <listitem><para>%gint</para></listitem>
  229. * </varlistentry>
  230. * <varlistentry>
  231. * <term>%G_OPTION_ARG_FILENAME</term>
  232. * <listitem><para>%gchar*</para></listitem>
  233. * </varlistentry>
  234. * <varlistentry>
  235. * <term>%G_OPTION_ARG_STRING_ARRAY</term>
  236. * <listitem><para>%gchar**</para></listitem>
  237. * </varlistentry>
  238. * <varlistentry>
  239. * <term>%G_OPTION_ARG_FILENAME_ARRAY</term>
  240. * <listitem><para>%gchar**</para></listitem>
  241. * </varlistentry>
  242. * <varlistentry>
  243. * <term>%G_OPTION_ARG_DOUBLE</term>
  244. * <listitem><para>%gdouble</para></listitem>
  245. * </varlistentry>
  246. * </variablelist>
  247. * If @arg type is %G_OPTION_ARG_STRING or %G_OPTION_ARG_FILENAME the location
  248. * will contain a newly allocated string if the option was given. That string
  249. * needs to be freed by the callee using g_free(). Likewise if @arg type is
  250. * %G_OPTION_ARG_STRING_ARRAY or %G_OPTION_ARG_FILENAME_ARRAY, the data should
  251. * be freed using g_strfreev().
  252. * @description: the description for the option in <option>--help</option>
  253. * output. The @description is translated using the @translate_func of the
  254. * group, see g_option_group_set_translation_domain().
  255. * @arg_description: The placeholder to use for the extra argument parsed
  256. * by the option in <option>--help</option>
  257. * output. The @arg_description is translated using the @translate_func of the
  258. * group, see g_option_group_set_translation_domain().
  259. *
  260. * A <structname>GOptionEntry</structname> defines a single option.
  261. * To have an effect, they must be added to a #GOptionGroup with
  262. * g_option_context_add_main_entries() or g_option_group_add_entries().
  263. */
  264. struct _GOptionEntry
  265. {
  266. const gchar *long_name;
  267. gchar short_name;
  268. gint flags;
  269. GOptionArg arg;
  270. gpointer arg_data;
  271. const gchar *description;
  272. const gchar *arg_description;
  273. };
  274. /**
  275. * G_OPTION_REMAINING:
  276. *
  277. * If a long option in the main group has this name, it is not treated as a
  278. * regular option. Instead it collects all non-option arguments which would
  279. * otherwise be left in <literal>argv</literal>. The option must be of type
  280. * %G_OPTION_ARG_CALLBACK, %G_OPTION_ARG_STRING_ARRAY
  281. * or %G_OPTION_ARG_FILENAME_ARRAY.
  282. *
  283. *
  284. * Using #G_OPTION_REMAINING instead of simply scanning <literal>argv</literal>
  285. * for leftover arguments has the advantage that GOption takes care of
  286. * necessary encoding conversions for strings or filenames.
  287. *
  288. * Since: 2.6
  289. */
  290. #define G_OPTION_REMAINING ""
  291. GOptionContext *g_option_context_new (const gchar *parameter_string);
  292. void g_option_context_set_summary (GOptionContext *context,
  293. const gchar *summary);
  294. G_CONST_RETURN gchar *g_option_context_get_summary (GOptionContext *context);
  295. void g_option_context_set_description (GOptionContext *context,
  296. const gchar *description);
  297. G_CONST_RETURN gchar *g_option_context_get_description (GOptionContext *context);
  298. void g_option_context_free (GOptionContext *context);
  299. void g_option_context_set_help_enabled (GOptionContext *context,
  300. gboolean help_enabled);
  301. gboolean g_option_context_get_help_enabled (GOptionContext *context);
  302. void g_option_context_set_ignore_unknown_options (GOptionContext *context,
  303. gboolean ignore_unknown);
  304. gboolean g_option_context_get_ignore_unknown_options (GOptionContext *context);
  305. void g_option_context_add_main_entries (GOptionContext *context,
  306. const GOptionEntry *entries,
  307. const gchar *translation_domain);
  308. gboolean g_option_context_parse (GOptionContext *context,
  309. gint *argc,
  310. gchar ***argv,
  311. GError **error);
  312. void g_option_context_set_translate_func (GOptionContext *context,
  313. GTranslateFunc func,
  314. gpointer data,
  315. GDestroyNotify destroy_notify);
  316. void g_option_context_set_translation_domain (GOptionContext *context,
  317. const gchar *domain);
  318. void g_option_context_add_group (GOptionContext *context,
  319. GOptionGroup *group);
  320. void g_option_context_set_main_group (GOptionContext *context,
  321. GOptionGroup *group);
  322. GOptionGroup *g_option_context_get_main_group (GOptionContext *context);
  323. gchar *g_option_context_get_help (GOptionContext *context,
  324. gboolean main_help,
  325. GOptionGroup *group);
  326. GOptionGroup *g_option_group_new (const gchar *name,
  327. const gchar *description,
  328. const gchar *help_description,
  329. gpointer user_data,
  330. GDestroyNotify destroy);
  331. void g_option_group_set_parse_hooks (GOptionGroup *group,
  332. GOptionParseFunc pre_parse_func,
  333. GOptionParseFunc post_parse_func);
  334. void g_option_group_set_error_hook (GOptionGroup *group,
  335. GOptionErrorFunc error_func);
  336. void g_option_group_free (GOptionGroup *group);
  337. void g_option_group_add_entries (GOptionGroup *group,
  338. const GOptionEntry *entries);
  339. void g_option_group_set_translate_func (GOptionGroup *group,
  340. GTranslateFunc func,
  341. gpointer data,
  342. GDestroyNotify destroy_notify);
  343. void g_option_group_set_translation_domain (GOptionGroup *group,
  344. const gchar *domain);
  345. G_END_DECLS
  346. #endif /* __G_OPTION_H__ */