1 /* goption.h - Option parser
3 * Copyright (C) 2004 Anders Carlsson <andersca@gnome.org>
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.
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.
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.
21 #ifndef __G_OPTION_H__
22 #define __G_OPTION_H__
24 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
25 #error "Only <glib.h> can be included directly."
28 #include <glib/gerror.h>
29 #include <glib/gquark.h>
36 * A <structname>GOptionContext</structname> struct defines which options
37 * are accepted by the commandline option parser. The struct has only private
38 * fields and should not be directly accessed.
40 typedef struct _GOptionContext GOptionContext;
45 * A <structname>GOptionGroup</structname> struct defines the options in a single
46 * group. The struct has only private fields and should not be directly accessed.
48 * All options in a group share the same translation function. Libraries which
49 * need to parse commandline options are expected to provide a function for
50 * getting a <structname>GOptionGroup</structname> holding their options, which
51 * the application can then add to its #GOptionContext.
53 typedef struct _GOptionGroup GOptionGroup;
54 typedef struct _GOptionEntry GOptionEntry;
58 * @G_OPTION_FLAG_HIDDEN: The option doesn't appear in <option>--help</option>
60 * @G_OPTION_FLAG_IN_MAIN: The option appears in the main section of the
61 * <option>--help</option> output, even if it is defined in a group.
62 * @G_OPTION_FLAG_REVERSE: For options of the %G_OPTION_ARG_NONE kind, this flag
63 * indicates that the sense of the option is reversed.
64 * @G_OPTION_FLAG_NO_ARG: For options of the %G_OPTION_ARG_CALLBACK kind,
65 * this flag indicates that the callback does not take any argument
66 * (like a %G_OPTION_ARG_NONE option). Since 2.8
67 * @G_OPTION_FLAG_FILENAME: For options of the %G_OPTION_ARG_CALLBACK
68 * kind, this flag indicates that the argument should be passed to the
69 * callback in the GLib filename encoding rather than UTF-8. Since 2.8
70 * @G_OPTION_FLAG_OPTIONAL_ARG: For options of the %G_OPTION_ARG_CALLBACK
71 * kind, this flag indicates that the argument supply is optional. If no argument
72 * is given then data of %GOptionParseFunc will be set to NULL. Since 2.8
73 * @G_OPTION_FLAG_NOALIAS: This flag turns off the automatic conflict resolution
74 * which prefixes long option names with <literal>groupname-</literal> if
75 * there is a conflict. This option should only be used in situations where
76 * aliasing is necessary to model some legacy commandline interface. It is
77 * not safe to use this option, unless all option groups are under your
78 * direct control. Since 2.8.
80 * Flags which modify individual options.
84 G_OPTION_FLAG_HIDDEN = 1 << 0,
85 G_OPTION_FLAG_IN_MAIN = 1 << 1,
86 G_OPTION_FLAG_REVERSE = 1 << 2,
87 G_OPTION_FLAG_NO_ARG = 1 << 3,
88 G_OPTION_FLAG_FILENAME = 1 << 4,
89 G_OPTION_FLAG_OPTIONAL_ARG = 1 << 5,
90 G_OPTION_FLAG_NOALIAS = 1 << 6
95 * @G_OPTION_ARG_NONE: No extra argument. This is useful for simple flags.
96 * @G_OPTION_ARG_STRING: The option takes a string argument.
97 * @G_OPTION_ARG_INT: The option takes an integer argument.
98 * @G_OPTION_ARG_CALLBACK: The option provides a callback to parse the
100 * @G_OPTION_ARG_FILENAME: The option takes a filename as argument.
101 * @G_OPTION_ARG_STRING_ARRAY: The option takes a string argument, multiple
102 * uses of the option are collected into an array of strings.
103 * @G_OPTION_ARG_FILENAME_ARRAY: The option takes a filename as argument,
104 * multiple uses of the option are collected into an array of strings.
105 * @G_OPTION_ARG_DOUBLE: The option takes a double argument. The argument
106 * can be formatted either for the user's locale or for the "C" locale. Since 2.12
107 * @G_OPTION_ARG_INT64: The option takes a 64-bit integer. Like %G_OPTION_ARG_INT
108 * but for larger numbers. The number can be in decimal base, or in hexadecimal
109 * (when prefixed with <literal>0x</literal>, for example, <literal>0xffffffff</literal>).
112 * The #GOptionArg enum values determine which type of extra argument the
113 * options expect to find. If an option expects an extra argument, it
114 * can be specified in several ways; with a short option:
115 * <option>-x arg</option>, with a long option: <option>--name arg</option>
116 * or combined in a single argument: <option>--name=arg</option>.
123 G_OPTION_ARG_CALLBACK,
124 G_OPTION_ARG_FILENAME,
125 G_OPTION_ARG_STRING_ARRAY,
126 G_OPTION_ARG_FILENAME_ARRAY,
133 * @option_name: The name of the option being parsed. This will be either a
134 * single dash followed by a single letter (for a short name) or two dashes
135 * followed by a long option name.
136 * @value: The value to be parsed.
137 * @data: User data added to the #GOptionGroup containing the option when it
138 * was created with g_option_group_new()
139 * @error: A return location for errors. The error code %G_OPTION_ERROR_FAILED
140 * is intended to be used for errors in #GOptionArgFunc callbacks.
142 * The type of function to be passed as callback for %G_OPTION_ARG_CALLBACK
145 * Returns: %TRUE if the option was successfully parsed, %FALSE if an error
146 * occurred, in which case @error should be set with g_set_error()
148 typedef gboolean (*GOptionArgFunc) (const gchar *option_name,
155 * @context: The active #GOptionContext
156 * @group: The group to which the function belongs
157 * @data: User data added to the #GOptionGroup containing the option when it
158 * was created with g_option_group_new()
159 * @error: A return location for error details
161 * The type of function that can be called before and after parsing.
163 * Returns: %TRUE if the function completed successfully, %FALSE if an error
164 * occurred, in which case @error should be set with g_set_error()
166 typedef gboolean (*GOptionParseFunc) (GOptionContext *context,
173 * @context: The active #GOptionContext
174 * @group: The group to which the function belongs
175 * @data: User data added to the #GOptionGroup containing the option when it
176 * was created with g_option_group_new()
177 * @error: The #GError containing details about the parse error
179 * The type of function to be used as callback when a parse error occurs.
181 typedef void (*GOptionErrorFunc) (GOptionContext *context,
189 * Error domain for option parsing. Errors in this domain will
190 * be from the #GOptionError enumeration. See #GError for information on
193 #define G_OPTION_ERROR (g_option_error_quark ())
197 * @G_OPTION_ERROR_UNKNOWN_OPTION: An option was not known to the parser.
198 * This error will only be reported, if the parser hasn't been instructed
199 * to ignore unknown options, see g_option_context_set_ignore_unknown_options().
200 * @G_OPTION_ERROR_BAD_VALUE: A value couldn't be parsed.
201 * @G_OPTION_ERROR_FAILED: A #GOptionArgFunc callback failed.
203 * Error codes returned by option parsing.
207 G_OPTION_ERROR_UNKNOWN_OPTION,
208 G_OPTION_ERROR_BAD_VALUE,
209 G_OPTION_ERROR_FAILED
212 GLIB_AVAILABLE_IN_ALL
213 GQuark g_option_error_quark (void);
217 * @long_name: The long name of an option can be used to specify it
218 * in a commandline as --<replaceable>long_name</replaceable>. Every
219 * option must have a long name. To resolve conflicts if multiple
220 * option groups contain the same long name, it is also possible to
221 * specify the option as
222 * --<replaceable>groupname</replaceable>-<replaceable>long_name</replaceable>.
223 * @short_name: If an option has a short name, it can be specified
224 * -<replaceable>short_name</replaceable> in a commandline. @short_name must be
225 * a printable ASCII character different from '-', or zero if the option has no
227 * @flags: Flags from #GOptionFlags.
228 * @arg: The type of the option, as a #GOptionArg.
229 * @arg_data: If the @arg type is %G_OPTION_ARG_CALLBACK, then @arg_data must
230 * point to a #GOptionArgFunc callback function, which will be called to handle
231 * the extra argument. Otherwise, @arg_data is a pointer to a location to store
232 * the value, the required type of the location depends on the @arg type:
235 * <term>%G_OPTION_ARG_NONE</term>
236 * <listitem><para>%gboolean</para></listitem>
239 * <term>%G_OPTION_ARG_STRING</term>
240 * <listitem><para>%gchar*</para></listitem>
243 * <term>%G_OPTION_ARG_INT</term>
244 * <listitem><para>%gint</para></listitem>
247 * <term>%G_OPTION_ARG_FILENAME</term>
248 * <listitem><para>%gchar*</para></listitem>
251 * <term>%G_OPTION_ARG_STRING_ARRAY</term>
252 * <listitem><para>%gchar**</para></listitem>
255 * <term>%G_OPTION_ARG_FILENAME_ARRAY</term>
256 * <listitem><para>%gchar**</para></listitem>
259 * <term>%G_OPTION_ARG_DOUBLE</term>
260 * <listitem><para>%gdouble</para></listitem>
263 * If @arg type is %G_OPTION_ARG_STRING or %G_OPTION_ARG_FILENAME the location
264 * will contain a newly allocated string if the option was given. That string
265 * needs to be freed by the callee using g_free(). Likewise if @arg type is
266 * %G_OPTION_ARG_STRING_ARRAY or %G_OPTION_ARG_FILENAME_ARRAY, the data should
267 * be freed using g_strfreev().
268 * @description: the description for the option in <option>--help</option>
269 * output. The @description is translated using the @translate_func of the
270 * group, see g_option_group_set_translation_domain().
271 * @arg_description: The placeholder to use for the extra argument parsed
272 * by the option in <option>--help</option>
273 * output. The @arg_description is translated using the @translate_func of the
274 * group, see g_option_group_set_translation_domain().
276 * A <structname>GOptionEntry</structname> defines a single option.
277 * To have an effect, they must be added to a #GOptionGroup with
278 * g_option_context_add_main_entries() or g_option_group_add_entries().
282 const gchar *long_name;
289 const gchar *description;
290 const gchar *arg_description;
294 * G_OPTION_REMAINING:
296 * If a long option in the main group has this name, it is not treated as a
297 * regular option. Instead it collects all non-option arguments which would
298 * otherwise be left in <literal>argv</literal>. The option must be of type
299 * %G_OPTION_ARG_CALLBACK, %G_OPTION_ARG_STRING_ARRAY
300 * or %G_OPTION_ARG_FILENAME_ARRAY.
303 * Using #G_OPTION_REMAINING instead of simply scanning <literal>argv</literal>
304 * for leftover arguments has the advantage that GOption takes care of
305 * necessary encoding conversions for strings or filenames.
309 #define G_OPTION_REMAINING ""
311 GLIB_AVAILABLE_IN_ALL
312 GOptionContext *g_option_context_new (const gchar *parameter_string);
313 GLIB_AVAILABLE_IN_ALL
314 void g_option_context_set_summary (GOptionContext *context,
315 const gchar *summary);
316 GLIB_AVAILABLE_IN_ALL
317 const gchar * g_option_context_get_summary (GOptionContext *context);
318 GLIB_AVAILABLE_IN_ALL
319 void g_option_context_set_description (GOptionContext *context,
320 const gchar *description);
321 GLIB_AVAILABLE_IN_ALL
322 const gchar * g_option_context_get_description (GOptionContext *context);
323 GLIB_AVAILABLE_IN_ALL
324 void g_option_context_free (GOptionContext *context);
325 GLIB_AVAILABLE_IN_ALL
326 void g_option_context_set_help_enabled (GOptionContext *context,
327 gboolean help_enabled);
328 GLIB_AVAILABLE_IN_ALL
329 gboolean g_option_context_get_help_enabled (GOptionContext *context);
330 GLIB_AVAILABLE_IN_ALL
331 void g_option_context_set_ignore_unknown_options (GOptionContext *context,
332 gboolean ignore_unknown);
333 GLIB_AVAILABLE_IN_ALL
334 gboolean g_option_context_get_ignore_unknown_options (GOptionContext *context);
336 GLIB_AVAILABLE_IN_ALL
337 void g_option_context_add_main_entries (GOptionContext *context,
338 const GOptionEntry *entries,
339 const gchar *translation_domain);
340 GLIB_AVAILABLE_IN_ALL
341 gboolean g_option_context_parse (GOptionContext *context,
345 GLIB_AVAILABLE_IN_ALL
346 void g_option_context_set_translate_func (GOptionContext *context,
349 GDestroyNotify destroy_notify);
350 GLIB_AVAILABLE_IN_ALL
351 void g_option_context_set_translation_domain (GOptionContext *context,
352 const gchar *domain);
354 GLIB_AVAILABLE_IN_ALL
355 void g_option_context_add_group (GOptionContext *context,
356 GOptionGroup *group);
357 GLIB_AVAILABLE_IN_ALL
358 void g_option_context_set_main_group (GOptionContext *context,
359 GOptionGroup *group);
360 GLIB_AVAILABLE_IN_ALL
361 GOptionGroup *g_option_context_get_main_group (GOptionContext *context);
362 GLIB_AVAILABLE_IN_ALL
363 gchar *g_option_context_get_help (GOptionContext *context,
365 GOptionGroup *group);
367 GLIB_AVAILABLE_IN_ALL
368 GOptionGroup *g_option_group_new (const gchar *name,
369 const gchar *description,
370 const gchar *help_description,
372 GDestroyNotify destroy);
373 GLIB_AVAILABLE_IN_ALL
374 void g_option_group_set_parse_hooks (GOptionGroup *group,
375 GOptionParseFunc pre_parse_func,
376 GOptionParseFunc post_parse_func);
377 GLIB_AVAILABLE_IN_ALL
378 void g_option_group_set_error_hook (GOptionGroup *group,
379 GOptionErrorFunc error_func);
380 GLIB_AVAILABLE_IN_ALL
381 void g_option_group_free (GOptionGroup *group);
382 GLIB_AVAILABLE_IN_ALL
383 void g_option_group_add_entries (GOptionGroup *group,
384 const GOptionEntry *entries);
385 GLIB_AVAILABLE_IN_ALL
386 void g_option_group_set_translate_func (GOptionGroup *group,
389 GDestroyNotify destroy_notify);
390 GLIB_AVAILABLE_IN_ALL
391 void g_option_group_set_translation_domain (GOptionGroup *group,
392 const gchar *domain);
396 #endif /* __G_OPTION_H__ */