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, see <http://www.gnu.org/licenses/>.
19 #ifndef __G_OPTION_H__
20 #define __G_OPTION_H__
22 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
23 #error "Only <glib.h> can be included directly."
26 #include <glib/gerror.h>
27 #include <glib/gquark.h>
34 * A `GOptionContext` struct defines which options
35 * are accepted by the commandline option parser. The struct has only private
36 * fields and should not be directly accessed.
38 typedef struct _GOptionContext GOptionContext;
43 * A `GOptionGroup` struct defines the options in a single
44 * group. The struct has only private fields and should not be directly accessed.
46 * All options in a group share the same translation function. Libraries which
47 * need to parse commandline options are expected to provide a function for
48 * getting a `GOptionGroup` holding their options, which
49 * the application can then add to its #GOptionContext.
51 typedef struct _GOptionGroup GOptionGroup;
52 typedef struct _GOptionEntry GOptionEntry;
56 * @G_OPTION_FLAG_HIDDEN: The option doesn't appear in `--help` output.
57 * @G_OPTION_FLAG_IN_MAIN: The option appears in the main section of the
58 * `--help` output, even if it is defined in a group.
59 * @G_OPTION_FLAG_REVERSE: For options of the %G_OPTION_ARG_NONE kind, this
60 * flag indicates that the sense of the option is reversed.
61 * @G_OPTION_FLAG_NO_ARG: For options of the %G_OPTION_ARG_CALLBACK kind,
62 * this flag indicates that the callback does not take any argument
63 * (like a %G_OPTION_ARG_NONE option). Since 2.8
64 * @G_OPTION_FLAG_FILENAME: For options of the %G_OPTION_ARG_CALLBACK
65 * kind, this flag indicates that the argument should be passed to the
66 * callback in the GLib filename encoding rather than UTF-8. Since 2.8
67 * @G_OPTION_FLAG_OPTIONAL_ARG: For options of the %G_OPTION_ARG_CALLBACK
68 * kind, this flag indicates that the argument supply is optional.
69 * If no argument is given then data of %GOptionParseFunc will be
70 * set to NULL. Since 2.8
71 * @G_OPTION_FLAG_NOALIAS: This flag turns off the automatic conflict
72 * resolution which prefixes long option names with `groupname-` if
73 * there is a conflict. This option should only be used in situations
74 * where aliasing is necessary to model some legacy commandline interface.
75 * It is not safe to use this option, unless all option groups are under
76 * your direct control. Since 2.8.
78 * Flags which modify individual options.
82 G_OPTION_FLAG_HIDDEN = 1 << 0,
83 G_OPTION_FLAG_IN_MAIN = 1 << 1,
84 G_OPTION_FLAG_REVERSE = 1 << 2,
85 G_OPTION_FLAG_NO_ARG = 1 << 3,
86 G_OPTION_FLAG_FILENAME = 1 << 4,
87 G_OPTION_FLAG_OPTIONAL_ARG = 1 << 5,
88 G_OPTION_FLAG_NOALIAS = 1 << 6
93 * @G_OPTION_ARG_NONE: No extra argument. This is useful for simple flags.
94 * @G_OPTION_ARG_STRING: The option takes a string argument.
95 * @G_OPTION_ARG_INT: The option takes an integer argument.
96 * @G_OPTION_ARG_CALLBACK: The option provides a callback to parse the
98 * @G_OPTION_ARG_FILENAME: The option takes a filename as argument.
99 * @G_OPTION_ARG_STRING_ARRAY: The option takes a string argument, multiple
100 * uses of the option are collected into an array of strings.
101 * @G_OPTION_ARG_FILENAME_ARRAY: The option takes a filename as argument,
102 * multiple uses of the option are collected into an array of strings.
103 * @G_OPTION_ARG_DOUBLE: The option takes a double argument. The argument
104 * can be formatted either for the user's locale or for the "C" locale.
106 * @G_OPTION_ARG_INT64: The option takes a 64-bit integer. Like
107 * %G_OPTION_ARG_INT but for larger numbers. The number can be in
108 * decimal base, or in hexadecimal (when prefixed with `0x`, for
109 * example, `0xffffffff`). Since 2.12
111 * The #GOptionArg enum values determine which type of extra argument the
112 * options expect to find. If an option expects an extra argument, it can
113 * be specified in several ways; with a short option: `-x arg`, with a long
114 * option: `--name arg` or combined in a single argument: `--name=arg`.
121 G_OPTION_ARG_CALLBACK,
122 G_OPTION_ARG_FILENAME,
123 G_OPTION_ARG_STRING_ARRAY,
124 G_OPTION_ARG_FILENAME_ARRAY,
131 * @option_name: The name of the option being parsed. This will be either a
132 * single dash followed by a single letter (for a short name) or two dashes
133 * followed by a long option name.
134 * @value: The value to be parsed.
135 * @data: User data added to the #GOptionGroup containing the option when it
136 * was created with g_option_group_new()
137 * @error: A return location for errors. The error code %G_OPTION_ERROR_FAILED
138 * is intended to be used for errors in #GOptionArgFunc callbacks.
140 * The type of function to be passed as callback for %G_OPTION_ARG_CALLBACK
143 * Returns: %TRUE if the option was successfully parsed, %FALSE if an error
144 * occurred, in which case @error should be set with g_set_error()
146 typedef gboolean (*GOptionArgFunc) (const gchar *option_name,
153 * @context: The active #GOptionContext
154 * @group: The group to which the function belongs
155 * @data: User data added to the #GOptionGroup containing the option when it
156 * was created with g_option_group_new()
157 * @error: A return location for error details
159 * The type of function that can be called before and after parsing.
161 * Returns: %TRUE if the function completed successfully, %FALSE if an error
162 * occurred, in which case @error should be set with g_set_error()
164 typedef gboolean (*GOptionParseFunc) (GOptionContext *context,
171 * @context: The active #GOptionContext
172 * @group: The group to which the function belongs
173 * @data: User data added to the #GOptionGroup containing the option when it
174 * was created with g_option_group_new()
175 * @error: The #GError containing details about the parse error
177 * The type of function to be used as callback when a parse error occurs.
179 typedef void (*GOptionErrorFunc) (GOptionContext *context,
187 * Error domain for option parsing. Errors in this domain will
188 * be from the #GOptionError enumeration. See #GError for information on
191 #define G_OPTION_ERROR (g_option_error_quark ())
195 * @G_OPTION_ERROR_UNKNOWN_OPTION: An option was not known to the parser.
196 * This error will only be reported, if the parser hasn't been instructed
197 * to ignore unknown options, see g_option_context_set_ignore_unknown_options().
198 * @G_OPTION_ERROR_BAD_VALUE: A value couldn't be parsed.
199 * @G_OPTION_ERROR_FAILED: A #GOptionArgFunc callback failed.
201 * Error codes returned by option parsing.
205 G_OPTION_ERROR_UNKNOWN_OPTION,
206 G_OPTION_ERROR_BAD_VALUE,
207 G_OPTION_ERROR_FAILED
210 GLIB_AVAILABLE_IN_ALL
211 GQuark g_option_error_quark (void);
215 * @long_name: The long name of an option can be used to specify it
216 * in a commandline as `--long_name`. Every option must have a
217 * long name. To resolve conflicts if multiple option groups contain
218 * the same long name, it is also possible to specify the option as
219 * `--groupname-long_name`.
220 * @short_name: If an option has a short name, it can be specified
221 * `-short_name` in a commandline. @short_name must be a printable
222 * ASCII character different from '-', or zero if the option has no
224 * @flags: Flags from #GOptionFlags
225 * @arg: The type of the option, as a #GOptionArg
226 * @arg_data: If the @arg type is %G_OPTION_ARG_CALLBACK, then @arg_data
227 * must point to a #GOptionArgFunc callback function, which will be
228 * called to handle the extra argument. Otherwise, @arg_data is a
229 * pointer to a location to store the value, the required type of
230 * the location depends on the @arg type:
231 * - %G_OPTION_ARG_NONE: %gboolean
232 * - %G_OPTION_ARG_STRING: %gchar*
233 * - %G_OPTION_ARG_INT: %gint
234 * - %G_OPTION_ARG_FILENAME: %gchar*
235 * - %G_OPTION_ARG_STRING_ARRAY: %gchar**
236 * - %G_OPTION_ARG_FILENAME_ARRAY: %gchar**
237 * - %G_OPTION_ARG_DOUBLE: %gdouble
238 * If @arg type is %G_OPTION_ARG_STRING or %G_OPTION_ARG_FILENAME,
239 * the location will contain a newly allocated string if the option
240 * was given. That string needs to be freed by the callee using g_free().
241 * Likewise if @arg type is %G_OPTION_ARG_STRING_ARRAY or
242 * %G_OPTION_ARG_FILENAME_ARRAY, the data should be freed using g_strfreev().
243 * @description: the description for the option in `--help`
244 * output. The @description is translated using the @translate_func
245 * of the group, see g_option_group_set_translation_domain().
246 * @arg_description: The placeholder to use for the extra argument parsed
247 * by the option in `--help` output. The @arg_description is translated
248 * using the @translate_func of the group, see
249 * g_option_group_set_translation_domain().
251 * A GOptionEntry struct defines a single option. To have an effect, they
252 * must be added to a #GOptionGroup with g_option_context_add_main_entries()
253 * or g_option_group_add_entries().
257 const gchar *long_name;
264 const gchar *description;
265 const gchar *arg_description;
269 * G_OPTION_REMAINING:
271 * If a long option in the main group has this name, it is not treated as a
272 * regular option. Instead it collects all non-option arguments which would
273 * otherwise be left in `argv`. The option must be of type
274 * %G_OPTION_ARG_CALLBACK, %G_OPTION_ARG_STRING_ARRAY
275 * or %G_OPTION_ARG_FILENAME_ARRAY.
278 * Using #G_OPTION_REMAINING instead of simply scanning `argv`
279 * for leftover arguments has the advantage that GOption takes care of
280 * necessary encoding conversions for strings or filenames.
284 #define G_OPTION_REMAINING ""
286 GLIB_AVAILABLE_IN_ALL
287 GOptionContext *g_option_context_new (const gchar *parameter_string);
288 GLIB_AVAILABLE_IN_ALL
289 void g_option_context_set_summary (GOptionContext *context,
290 const gchar *summary);
291 GLIB_AVAILABLE_IN_ALL
292 const gchar * g_option_context_get_summary (GOptionContext *context);
293 GLIB_AVAILABLE_IN_ALL
294 void g_option_context_set_description (GOptionContext *context,
295 const gchar *description);
296 GLIB_AVAILABLE_IN_ALL
297 const gchar * g_option_context_get_description (GOptionContext *context);
298 GLIB_AVAILABLE_IN_ALL
299 void g_option_context_free (GOptionContext *context);
300 GLIB_AVAILABLE_IN_ALL
301 void g_option_context_set_help_enabled (GOptionContext *context,
302 gboolean help_enabled);
303 GLIB_AVAILABLE_IN_ALL
304 gboolean g_option_context_get_help_enabled (GOptionContext *context);
305 GLIB_AVAILABLE_IN_ALL
306 void g_option_context_set_ignore_unknown_options (GOptionContext *context,
307 gboolean ignore_unknown);
308 GLIB_AVAILABLE_IN_ALL
309 gboolean g_option_context_get_ignore_unknown_options (GOptionContext *context);
311 GLIB_AVAILABLE_IN_ALL
312 void g_option_context_add_main_entries (GOptionContext *context,
313 const GOptionEntry *entries,
314 const gchar *translation_domain);
315 GLIB_AVAILABLE_IN_ALL
316 gboolean g_option_context_parse (GOptionContext *context,
320 GLIB_AVAILABLE_IN_2_40
321 gboolean g_option_context_parse_strv (GOptionContext *context,
324 GLIB_AVAILABLE_IN_ALL
325 void g_option_context_set_translate_func (GOptionContext *context,
328 GDestroyNotify destroy_notify);
329 GLIB_AVAILABLE_IN_ALL
330 void g_option_context_set_translation_domain (GOptionContext *context,
331 const gchar *domain);
333 GLIB_AVAILABLE_IN_ALL
334 void g_option_context_add_group (GOptionContext *context,
335 GOptionGroup *group);
336 GLIB_AVAILABLE_IN_ALL
337 void g_option_context_set_main_group (GOptionContext *context,
338 GOptionGroup *group);
339 GLIB_AVAILABLE_IN_ALL
340 GOptionGroup *g_option_context_get_main_group (GOptionContext *context);
341 GLIB_AVAILABLE_IN_ALL
342 gchar *g_option_context_get_help (GOptionContext *context,
344 GOptionGroup *group);
346 GLIB_AVAILABLE_IN_ALL
347 GOptionGroup *g_option_group_new (const gchar *name,
348 const gchar *description,
349 const gchar *help_description,
351 GDestroyNotify destroy);
352 GLIB_AVAILABLE_IN_ALL
353 void g_option_group_set_parse_hooks (GOptionGroup *group,
354 GOptionParseFunc pre_parse_func,
355 GOptionParseFunc post_parse_func);
356 GLIB_AVAILABLE_IN_ALL
357 void g_option_group_set_error_hook (GOptionGroup *group,
358 GOptionErrorFunc error_func);
359 GLIB_AVAILABLE_IN_ALL
360 void g_option_group_free (GOptionGroup *group);
361 GLIB_AVAILABLE_IN_ALL
362 void g_option_group_add_entries (GOptionGroup *group,
363 const GOptionEntry *entries);
364 GLIB_AVAILABLE_IN_ALL
365 void g_option_group_set_translate_func (GOptionGroup *group,
368 GDestroyNotify destroy_notify);
369 GLIB_AVAILABLE_IN_ALL
370 void g_option_group_set_translation_domain (GOptionGroup *group,
371 const gchar *domain);
375 #endif /* __G_OPTION_H__ */