1 <!-- ##### SECTION Title ##### -->
2 Commandline option parser
4 <!-- ##### SECTION Short_Description ##### -->
5 parses commandline options
7 <!-- ##### SECTION Long_Description ##### -->
9 The GOption commandline parser is intended to be a simpler replacement for the
10 popt library. It supports short and long commandline options, as shown in the
15 <literal>test -x 1 --long-y foo --flag --long-z=baz -uvw -- file1 file2</literal>
19 The example demonstrates a number of features of the GOption commandline parser
22 Options can be single letters, prefixed by a single dash. Multiple
23 short options can be grouped behind a single dash.
26 Long options are prefixed by two consecutive dashes.
29 Options can have an extra argument, which can be a number, a string or a filename.
30 For long options, the extra argument can be appended with an equals sign after the
34 An argument consisting solely of two dashes turns off further parsing, any remaining
35 arguments are returned to the application as rest arguments.
41 Another important feature of GOption is that it can automatically generate nicely
42 formatted help output. Unless it is explicitly turned off with
43 g_option_context_set_help_enabled(), GOption will recognize the
44 <option>--help</option>, <option>-?</option>, <option>--help-all</option>
45 and <option>--help-</option><replaceable>groupname</replaceable> options (where
46 <replaceable>groupname</replaceable> is the name of a #GOptionGroup) and
47 write a text similar to the one shown in the following example to stdout.
50 <informalexample><screen>
52 testtreemodel [OPTION...]
55 --help Show help options
56 --help-all Show all help options
57 --help-gtk Show GTK+ Options
60 -r, --repeats=N Average over N repetitions
61 -m, --max-size=M Test up to 2^M items
62 --display=DISPLAY X display to use
63 </screen></informalexample>
66 GOption groups options in #GOptionGroup<!-- -->s, which makes it easy to
67 incorporate options from multiple sources. The intended use for this is
68 to let applications collect option groups from the libraries it uses,
69 add them to their #GOptionContext, and parse all options by a single call
70 to g_option_context_parse(). See gtk_get_option_group() for an example.
74 If an option is declared to be of type string or filename, GOption takes
75 care of converting it to the right encoding; strings are returned in UTF-8,
76 filenames are returned in the GLib filename encoding.
79 <!-- ##### SECTION See_Also ##### -->
84 <!-- ##### ENUM GOptionError ##### -->
86 Error codes returned by option parsing.
89 @G_OPTION_ERROR_UNKNOWN_OPTION: An option was not known to the parser.
90 This error will only be reported, if the parser hasn't been instructed
91 to ignore unknown options, see g_option_context_set_ignore_unknown_options().
92 @G_OPTION_ERROR_BAD_VALUE: A value couldn't be parsed.
93 @G_OPTION_ERROR_FAILED: A #GOptionArgFunc callback failed.
95 <!-- ##### MACRO G_OPTION_ERROR ##### -->
97 Error domain for option parsing. Errors in this domain will
98 be from the #GOptionError enumeration. See #GError for information on
104 <!-- ##### USER_FUNCTION GOptionArgFunc ##### -->
106 The type of function to be passed as callback for %G_OPTION_ARG_CALLBACK
110 @option_name: The name of the option being parsed. This will be either a
111 single dash followed by a single letter (for a short name) or two dashes
112 followed by a long option name.
113 @value: The value to be parsed.
114 @data: User data added to the #GOptionGroup containing the option when it
115 was created with g_option_group_new()
116 @error: A return location for errors. The error code %G_OPTION_ERROR_FAILED
117 is intended to be used for errors in #GOptionArgFunc callbacks.
118 @Returns: %TRUE if the option was successfully parsed, %FALSE if an error
122 <!-- ##### STRUCT GOptionContext ##### -->
124 A <structname>GOptionContext</structname> struct defines which options
125 are accepted by the commandline option parser. The struct has only private
126 fields and should not be directly accessed.
130 <!-- ##### FUNCTION g_option_context_new ##### -->
139 <!-- ##### FUNCTION g_option_context_free ##### -->
147 <!-- ##### FUNCTION g_option_context_parse ##### -->
159 <!-- ##### FUNCTION g_option_context_set_help_enabled ##### -->
168 <!-- ##### FUNCTION g_option_context_get_help_enabled ##### -->
177 <!-- ##### FUNCTION g_option_context_set_ignore_unknown_options ##### -->
186 <!-- ##### FUNCTION g_option_context_get_ignore_unknown_options ##### -->
195 <!-- ##### ENUM GOptionArg ##### -->
197 The #GOptionArg enum values determine which type of extra argument the
198 options expect to find. If an option expects an extra argument, it
199 can be specified in several ways; with a short option:
200 <option>-x arg</option>, with a long option: <option>--name arg</option>
201 or combined in a single argument: <option>--name=arg</option>.
204 @G_OPTION_ARG_NONE: No extra argument. This is useful for simple flags.
205 @G_OPTION_ARG_STRING: The option takes a string argument.
206 @G_OPTION_ARG_INT: The option takes an integer argument.
207 @G_OPTION_ARG_CALLBACK: The option provides a callback to parse the
209 @G_OPTION_ARG_FILENAME: The option takes a filename as argument.
210 @G_OPTION_ARG_STRING_ARRAY: The option takes a string argument, multiple
211 uses of the option are collected into an array of strings.
212 @G_OPTION_ARG_FILENAME_ARRAY: The option takes a filename as argument,
213 multiple uses of the option are collected into an array of strings.
215 <!-- ##### ENUM GOptionFlags ##### -->
217 Flags which modify individual options.
220 @G_OPTION_FLAG_HIDDEN: The option doesn't appear in <option>--help</option>
222 @G_OPTION_FLAG_IN_MAIN: The option appears in the main section of the
223 <option>--help</option> output, even if it is defined in a group.
225 <!-- ##### STRUCT GOptionEntry ##### -->
227 A <structname>GOptionEntry</structname> defines a single option.
228 To have an effect, they must be added to a #GOptionGroup with
229 g_option_context_add_main_entries() or g_option_group_add_entries().
232 @long_name: The long name of an option can be used to specify it
233 in a commandline as --<replaceable>long_name</replaceable>. Every
234 option must have a long name.
235 @short_name: If an option has a short name, it can be specified
236 -<replaceable>short_name</replaceable> in a commandline.
237 @flags: Flags from #GOptionEntryFlags.
238 @arg: The type of the option, as a #GOptionArg.
239 @arg_data: If the @arg type is %G_OPTION_ARG_CALLBACK, then @arg_data must
240 point to a #GOptionArgFunc callback function, which will be called to handle
241 the extra argument. Otherwise, @arg_data is a pointer to a location to store
242 the value, the required type of the location depends on the @arg type:
245 <term>%G_OPTION_ARG_NONE</term>
246 <listitem><para>%gboolean</para></listitem>
249 <term>%G_OPTION_ARG_STRING</term>
250 <listitem><para>%gchar*</para></listitem>
253 <term>%G_OPTION_ARG_INT</term>
254 <listitem><para>%gint</para></listitem>
257 <term>%G_OPTION_ARG_FILENAME</term>
258 <listitem><para>%gchar*</para></listitem>
261 <term>%G_OPTION_ARG_STRING_ARRAY</term>
262 <listitem><para>%gchar**</para></listitem>
265 <term>%G_OPTION_ARG_FILENAME_ARRAY</term>
266 <listitem><para>%gchar**</para></listitem>
269 @description: the description for the option in <option>--help</option>
270 output. The @description is translated using the @translate_func of the
271 group, see g_option_group_set_translation_domain().
272 @arg_description: The placeholder to use for the extra argument parsed
273 by the option in <option>--help</option>
274 output. The @arg_description is translated using the @translate_func of the
275 group, see g_option_group_set_translation_domain().
277 <!-- ##### FUNCTION g_option_context_add_main_entries ##### -->
287 <!-- ##### STRUCT GOptionGroup ##### -->
289 A <structname>GOptionGroup</structname> struct defines the options in a single
290 group. The struct has only private fields and should not be directly accessed.
293 All options in a group share the same translation function. Libaries which
294 need to parse commandline options are expected to provide a function for
295 getting a <structname>GOptionGroup</structname> holding their options, which
296 the application can then add to its #GOptionContext.
300 <!-- ##### FUNCTION g_option_context_add_group ##### -->
309 <!-- ##### FUNCTION g_option_context_set_main_group ##### -->
318 <!-- ##### FUNCTION g_option_context_get_main_group ##### -->
327 <!-- ##### FUNCTION g_option_group_new ##### -->
340 <!-- ##### FUNCTION g_option_group_free ##### -->
348 <!-- ##### FUNCTION g_option_group_add_entries ##### -->
357 <!-- ##### USER_FUNCTION GOptionParseFunc ##### -->
359 The type of function that can be called before and after parsing.
362 @context The active #GOptionContext
365 @group: The group to which the function belongs
366 @data: User data added to the #GOptionGroup containing the option when it
367 was created with g_option_group_new()
368 @error: A return location for error details
369 @Returns: %TRUE if the function completed successfully, %FALSE if an error
373 <!-- ##### FUNCTION g_option_group_set_parse_hooks ##### -->
383 <!-- ##### USER_FUNCTION GOptionErrorFunc ##### -->
385 The type of function to be used as callback when a parse error
389 @context The active #GOptionContext
392 @group: The group to which the function belongs
393 @data: User data added to the #GOptionGroup containing the option when it
394 was created with g_option_group_new()
395 @error: The #GError containing details about the parse error
398 <!-- ##### FUNCTION g_option_group_set_error_hook ##### -->
407 <!-- ##### USER_FUNCTION GTranslateFunc ##### -->
409 The type of functions which are used to translate user-visible
410 strings, for <option>--help</option> output.
413 @str: the untranslated string
414 @data: user data specified when installing the function, e.g.
415 in g_option_group_set_translate_func()
416 @Returns: a translation of the string for the current locale.
417 The returned string is owned by GLib and must not be freed.
420 <!-- ##### FUNCTION g_option_group_set_translate_func ##### -->
431 <!-- ##### FUNCTION g_option_group_set_translation_domain ##### -->