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 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
22 #error "Only <glib.h> can be included directly."
25 #ifndef __G_OPTION_H__
26 #define __G_OPTION_H__
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 GQuark g_option_error_quark (void);
216 * @long_name: The long name of an option can be used to specify it
217 * in a commandline as --<replaceable>long_name</replaceable>. Every
218 * option must have a long name. To resolve conflicts if multiple
219 * option groups contain the same long name, it is also possible to
220 * specify the option as
221 * --<replaceable>groupname</replaceable>-<replaceable>long_name</replaceable>.
222 * @short_name: If an option has a short name, it can be specified
223 * -<replaceable>short_name</replaceable> in a commandline. @short_name must be
224 * a printable ASCII character different from '-', or zero if the option has no
226 * @flags: Flags from #GOptionFlags.
227 * @arg: The type of the option, as a #GOptionArg.
228 * @arg_data: If the @arg type is %G_OPTION_ARG_CALLBACK, then @arg_data must
229 * point to a #GOptionArgFunc callback function, which will be called to handle
230 * the extra argument. Otherwise, @arg_data is a pointer to a location to store
231 * the value, the required type of the location depends on the @arg type:
234 * <term>%G_OPTION_ARG_NONE</term>
235 * <listitem><para>%gboolean</para></listitem>
238 * <term>%G_OPTION_ARG_STRING</term>
239 * <listitem><para>%gchar*</para></listitem>
242 * <term>%G_OPTION_ARG_INT</term>
243 * <listitem><para>%gint</para></listitem>
246 * <term>%G_OPTION_ARG_FILENAME</term>
247 * <listitem><para>%gchar*</para></listitem>
250 * <term>%G_OPTION_ARG_STRING_ARRAY</term>
251 * <listitem><para>%gchar**</para></listitem>
254 * <term>%G_OPTION_ARG_FILENAME_ARRAY</term>
255 * <listitem><para>%gchar**</para></listitem>
258 * <term>%G_OPTION_ARG_DOUBLE</term>
259 * <listitem><para>%gdouble</para></listitem>
262 * If @arg type is %G_OPTION_ARG_STRING or %G_OPTION_ARG_FILENAME the location
263 * will contain a newly allocated string if the option was given. That string
264 * needs to be freed by the callee using g_free(). Likewise if @arg type is
265 * %G_OPTION_ARG_STRING_ARRAY or %G_OPTION_ARG_FILENAME_ARRAY, the data should
266 * be freed using g_strfreev().
267 * @description: the description for the option in <option>--help</option>
268 * output. The @description is translated using the @translate_func of the
269 * group, see g_option_group_set_translation_domain().
270 * @arg_description: The placeholder to use for the extra argument parsed
271 * by the option in <option>--help</option>
272 * output. The @arg_description is translated using the @translate_func of the
273 * group, see g_option_group_set_translation_domain().
275 * A <structname>GOptionEntry</structname> defines a single option.
276 * To have an effect, they must be added to a #GOptionGroup with
277 * g_option_context_add_main_entries() or g_option_group_add_entries().
281 const gchar *long_name;
288 const gchar *description;
289 const gchar *arg_description;
293 * G_OPTION_REMAINING:
295 * If a long option in the main group has this name, it is not treated as a
296 * regular option. Instead it collects all non-option arguments which would
297 * otherwise be left in <literal>argv</literal>. The option must be of type
298 * %G_OPTION_ARG_CALLBACK, %G_OPTION_ARG_STRING_ARRAY
299 * or %G_OPTION_ARG_FILENAME_ARRAY.
302 * Using #G_OPTION_REMAINING instead of simply scanning <literal>argv</literal>
303 * for leftover arguments has the advantage that GOption takes care of
304 * necessary encoding conversions for strings or filenames.
308 #define G_OPTION_REMAINING ""
310 GOptionContext *g_option_context_new (const gchar *parameter_string);
311 void g_option_context_set_summary (GOptionContext *context,
312 const gchar *summary);
313 const gchar * g_option_context_get_summary (GOptionContext *context);
314 void g_option_context_set_description (GOptionContext *context,
315 const gchar *description);
316 const gchar * g_option_context_get_description (GOptionContext *context);
317 void g_option_context_free (GOptionContext *context);
318 void g_option_context_set_help_enabled (GOptionContext *context,
319 gboolean help_enabled);
320 gboolean g_option_context_get_help_enabled (GOptionContext *context);
321 void g_option_context_set_ignore_unknown_options (GOptionContext *context,
322 gboolean ignore_unknown);
323 gboolean g_option_context_get_ignore_unknown_options (GOptionContext *context);
325 void g_option_context_add_main_entries (GOptionContext *context,
326 const GOptionEntry *entries,
327 const gchar *translation_domain);
328 gboolean g_option_context_parse (GOptionContext *context,
332 void g_option_context_set_translate_func (GOptionContext *context,
335 GDestroyNotify destroy_notify);
336 void g_option_context_set_translation_domain (GOptionContext *context,
337 const gchar *domain);
339 void g_option_context_add_group (GOptionContext *context,
340 GOptionGroup *group);
341 void g_option_context_set_main_group (GOptionContext *context,
342 GOptionGroup *group);
343 GOptionGroup *g_option_context_get_main_group (GOptionContext *context);
344 gchar *g_option_context_get_help (GOptionContext *context,
346 GOptionGroup *group);
348 GOptionGroup *g_option_group_new (const gchar *name,
349 const gchar *description,
350 const gchar *help_description,
352 GDestroyNotify destroy);
353 void g_option_group_set_parse_hooks (GOptionGroup *group,
354 GOptionParseFunc pre_parse_func,
355 GOptionParseFunc post_parse_func);
356 void g_option_group_set_error_hook (GOptionGroup *group,
357 GOptionErrorFunc error_func);
358 void g_option_group_free (GOptionGroup *group);
359 void g_option_group_add_entries (GOptionGroup *group,
360 const GOptionEntry *entries);
361 void g_option_group_set_translate_func (GOptionGroup *group,
364 GDestroyNotify destroy_notify);
365 void g_option_group_set_translation_domain (GOptionGroup *group,
366 const gchar *domain);
370 #endif /* __G_OPTION_H__ */