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 Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 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 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public License
16 * 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_NONE: No flags. Since: 2.42.
57 * @G_OPTION_FLAG_HIDDEN: The option doesn't appear in `--help` output.
58 * @G_OPTION_FLAG_IN_MAIN: The option appears in the main section of the
59 * `--help` output, even if it is defined in a group.
60 * @G_OPTION_FLAG_REVERSE: For options of the %G_OPTION_ARG_NONE kind, this
61 * flag indicates that the sense of the option is reversed.
62 * @G_OPTION_FLAG_NO_ARG: For options of the %G_OPTION_ARG_CALLBACK kind,
63 * this flag indicates that the callback does not take any argument
64 * (like a %G_OPTION_ARG_NONE option). Since 2.8
65 * @G_OPTION_FLAG_FILENAME: For options of the %G_OPTION_ARG_CALLBACK
66 * kind, this flag indicates that the argument should be passed to the
67 * callback in the GLib filename encoding rather than UTF-8. Since 2.8
68 * @G_OPTION_FLAG_OPTIONAL_ARG: For options of the %G_OPTION_ARG_CALLBACK
69 * kind, this flag indicates that the argument supply is optional.
70 * If no argument is given then data of %GOptionParseFunc will be
71 * set to NULL. Since 2.8
72 * @G_OPTION_FLAG_NOALIAS: This flag turns off the automatic conflict
73 * resolution which prefixes long option names with `groupname-` if
74 * there is a conflict. This option should only be used in situations
75 * where aliasing is necessary to model some legacy commandline interface.
76 * It is not safe to use this option, unless all option groups are under
77 * your direct control. Since 2.8.
79 * Flags which modify individual options.
83 G_OPTION_FLAG_NONE
= 0,
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 (of type
99 * #GOptionArgFunc) to parse the extra argument.
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.
108 * @G_OPTION_ARG_INT64: The option takes a 64-bit integer. Like
109 * %G_OPTION_ARG_INT but for larger numbers. The number can be in
110 * decimal base, or in hexadecimal (when prefixed with `0x`, for
111 * example, `0xffffffff`). Since 2.12
113 * The #GOptionArg enum values determine which type of extra argument the
114 * options expect to find. If an option expects an extra argument, it can
115 * be specified in several ways; with a short option: `-x arg`, with a long
116 * option: `--name arg` or combined in a single argument: `--name=arg`.
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 `--long_name`. Every option must have a
219 * long name. To resolve conflicts if multiple option groups contain
220 * the same long name, it is also possible to specify the option as
221 * `--groupname-long_name`.
222 * @short_name: If an option has a short name, it can be specified
223 * `-short_name` in a commandline. @short_name must be a printable
224 * 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
229 * must point to a #GOptionArgFunc callback function, which will be
230 * called to handle the extra argument. Otherwise, @arg_data is a
231 * pointer to a location to store the value, the required type of
232 * the location depends on the @arg type:
233 * - %G_OPTION_ARG_NONE: %gboolean
234 * - %G_OPTION_ARG_STRING: %gchar*
235 * - %G_OPTION_ARG_INT: %gint
236 * - %G_OPTION_ARG_FILENAME: %gchar*
237 * - %G_OPTION_ARG_STRING_ARRAY: %gchar**
238 * - %G_OPTION_ARG_FILENAME_ARRAY: %gchar**
239 * - %G_OPTION_ARG_DOUBLE: %gdouble
240 * If @arg type is %G_OPTION_ARG_STRING or %G_OPTION_ARG_FILENAME,
241 * the location will contain a newly allocated string if the option
242 * was given. That string needs to be freed by the callee using g_free().
243 * Likewise if @arg type is %G_OPTION_ARG_STRING_ARRAY or
244 * %G_OPTION_ARG_FILENAME_ARRAY, the data should be freed using g_strfreev().
245 * @description: the description for the option in `--help`
246 * output. The @description is translated using the @translate_func
247 * of the group, see g_option_group_set_translation_domain().
248 * @arg_description: The placeholder to use for the extra argument parsed
249 * by the option in `--help` output. The @arg_description is translated
250 * using the @translate_func of the group, see
251 * g_option_group_set_translation_domain().
253 * A GOptionEntry struct defines a single option. To have an effect, they
254 * must be added to a #GOptionGroup with g_option_context_add_main_entries()
255 * or g_option_group_add_entries().
259 const gchar
*long_name
;
266 const gchar
*description
;
267 const gchar
*arg_description
;
271 * G_OPTION_REMAINING:
273 * If a long option in the main group has this name, it is not treated as a
274 * regular option. Instead it collects all non-option arguments which would
275 * otherwise be left in `argv`. The option must be of type
276 * %G_OPTION_ARG_CALLBACK, %G_OPTION_ARG_STRING_ARRAY
277 * or %G_OPTION_ARG_FILENAME_ARRAY.
280 * Using #G_OPTION_REMAINING instead of simply scanning `argv`
281 * for leftover arguments has the advantage that GOption takes care of
282 * necessary encoding conversions for strings or filenames.
286 #define G_OPTION_REMAINING ""
288 GLIB_AVAILABLE_IN_ALL
289 GOptionContext
*g_option_context_new (const gchar
*parameter_string
);
290 GLIB_AVAILABLE_IN_ALL
291 void g_option_context_set_summary (GOptionContext
*context
,
292 const gchar
*summary
);
293 GLIB_AVAILABLE_IN_ALL
294 const gchar
* g_option_context_get_summary (GOptionContext
*context
);
295 GLIB_AVAILABLE_IN_ALL
296 void g_option_context_set_description (GOptionContext
*context
,
297 const gchar
*description
);
298 GLIB_AVAILABLE_IN_ALL
299 const gchar
* g_option_context_get_description (GOptionContext
*context
);
300 GLIB_AVAILABLE_IN_ALL
301 void g_option_context_free (GOptionContext
*context
);
302 GLIB_AVAILABLE_IN_ALL
303 void g_option_context_set_help_enabled (GOptionContext
*context
,
304 gboolean help_enabled
);
305 GLIB_AVAILABLE_IN_ALL
306 gboolean
g_option_context_get_help_enabled (GOptionContext
*context
);
307 GLIB_AVAILABLE_IN_ALL
308 void g_option_context_set_ignore_unknown_options (GOptionContext
*context
,
309 gboolean ignore_unknown
);
310 GLIB_AVAILABLE_IN_ALL
311 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
);
319 GLIB_AVAILABLE_IN_ALL
320 void g_option_context_add_main_entries (GOptionContext
*context
,
321 const GOptionEntry
*entries
,
322 const gchar
*translation_domain
);
323 GLIB_AVAILABLE_IN_ALL
324 gboolean
g_option_context_parse (GOptionContext
*context
,
328 GLIB_AVAILABLE_IN_2_40
329 gboolean
g_option_context_parse_strv (GOptionContext
*context
,
332 GLIB_AVAILABLE_IN_ALL
333 void g_option_context_set_translate_func (GOptionContext
*context
,
336 GDestroyNotify destroy_notify
);
337 GLIB_AVAILABLE_IN_ALL
338 void g_option_context_set_translation_domain (GOptionContext
*context
,
339 const gchar
*domain
);
341 GLIB_AVAILABLE_IN_ALL
342 void g_option_context_add_group (GOptionContext
*context
,
343 GOptionGroup
*group
);
344 GLIB_AVAILABLE_IN_ALL
345 void g_option_context_set_main_group (GOptionContext
*context
,
346 GOptionGroup
*group
);
347 GLIB_AVAILABLE_IN_ALL
348 GOptionGroup
*g_option_context_get_main_group (GOptionContext
*context
);
349 GLIB_AVAILABLE_IN_ALL
350 gchar
*g_option_context_get_help (GOptionContext
*context
,
352 GOptionGroup
*group
);
354 GLIB_AVAILABLE_IN_ALL
355 GOptionGroup
*g_option_group_new (const gchar
*name
,
356 const gchar
*description
,
357 const gchar
*help_description
,
359 GDestroyNotify destroy
);
360 GLIB_AVAILABLE_IN_ALL
361 void g_option_group_set_parse_hooks (GOptionGroup
*group
,
362 GOptionParseFunc pre_parse_func
,
363 GOptionParseFunc post_parse_func
);
364 GLIB_AVAILABLE_IN_ALL
365 void g_option_group_set_error_hook (GOptionGroup
*group
,
366 GOptionErrorFunc error_func
);
367 GLIB_DEPRECATED_IN_2_44
368 void g_option_group_free (GOptionGroup
*group
);
369 GLIB_AVAILABLE_IN_2_44
370 GOptionGroup
*g_option_group_ref (GOptionGroup
*group
);
371 GLIB_AVAILABLE_IN_2_44
372 void g_option_group_unref (GOptionGroup
*group
);
373 GLIB_AVAILABLE_IN_ALL
374 void g_option_group_add_entries (GOptionGroup
*group
,
375 const GOptionEntry
*entries
);
376 GLIB_AVAILABLE_IN_ALL
377 void g_option_group_set_translate_func (GOptionGroup
*group
,
380 GDestroyNotify destroy_notify
);
381 GLIB_AVAILABLE_IN_ALL
382 void g_option_group_set_translation_domain (GOptionGroup
*group
,
383 const gchar
*domain
);
387 #endif /* __G_OPTION_H__ */