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>testtreemodel -r 1 --max-size 20 --rand --display=:1.0 -vb -- 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 Non-option arguments are returned to the application as rest arguments.
37 An argument consisting solely of two dashes turns off further parsing, any remaining
38 arguments (even those starting with a dash) are returned to the application as rest
45 Another important feature of GOption is that it can automatically generate nicely
46 formatted help output. Unless it is explicitly turned off with
47 g_option_context_set_help_enabled(), GOption will recognize the
48 <option>--help</option>, <option>-?</option>, <option>--help-all</option>
49 and <option>--help-</option><replaceable>groupname</replaceable> options
50 (where <replaceable>groupname</replaceable> is the name of a #GOptionGroup)
51 and write a text similar to the one shown in the following example to stdout.
54 <informalexample><screen>
56 testtreemodel [OPTION...] - test tree model performance
59 -?, --help Show help options
60 --help-all Show all help options
61 --help-gtk Show GTK+ Options
64 -r, --repeats=N Average over N repetitions
65 -m, --max-size=M Test up to 2^M items
66 --display=DISPLAY X display to use
67 -v, --verbose Be verbose
68 -b, --beep Beep when done
69 --rand Randomize the data
70 </screen></informalexample>
73 GOption groups options in #GOptionGroup<!-- -->s, which makes it easy to
74 incorporate options from multiple sources. The intended use for this is
75 to let applications collect option groups from the libraries it uses,
76 add them to their #GOptionContext, and parse all options by a single call
77 to g_option_context_parse(). See gtk_get_option_group() for an example.
81 If an option is declared to be of type string or filename, GOption takes
82 care of converting it to the right encoding; strings are returned in UTF-8,
83 filenames are returned in the GLib filename encoding.
87 Here is a complete example of setting up GOption to parse the example
88 commandline above and produce the example help output.
90 <informalexample><programlisting>
91 static gint repeats = 2;
92 static gint max_size = 8;
93 static gboolean verbose = FALSE;
94 static gboolean beep = FALSE;
95 static gboolean rand = FALSE;
97 static GOptionEntry entries[] =
99 { "repeats", 'r', 0, G_OPTION_ARG_INT, &repeats, "Average over N repetitions", "N" },
100 { "max-size", 'm', 0, G_OPTION_ARG_INT, &max_size, "Test up to 2^M items", "M" },
101 { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL },
102 { "beep", 'b', 0, G_OPTION_ARG_NONE, &beep, "Beep when done", NULL },
103 { "rand", 0, 0, G_OPTION_ARG_NONE, &rand, "Randomize the data", NULL },
108 main (int argc, char *argv[])
110 GError *error = NULL;
112 context = g_option_context_new ("- test tree model performance");
113 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
114 g_option_context_add_group (context, gtk_get_option_group (TRUE));
115 g_option_context_parse (context, &argc, &argv, &error);
120 </programlisting></informalexample>
122 <!-- ##### SECTION See_Also ##### -->
127 <!-- ##### SECTION Stability_Level ##### -->
130 <!-- ##### ENUM GOptionError ##### -->
132 Error codes returned by option parsing.
135 @G_OPTION_ERROR_UNKNOWN_OPTION: An option was not known to the parser.
136 This error will only be reported, if the parser hasn't been instructed
137 to ignore unknown options, see g_option_context_set_ignore_unknown_options().
138 @G_OPTION_ERROR_BAD_VALUE: A value couldn't be parsed.
139 @G_OPTION_ERROR_FAILED: A #GOptionArgFunc callback failed.
141 <!-- ##### MACRO G_OPTION_ERROR ##### -->
143 Error domain for option parsing. Errors in this domain will
144 be from the #GOptionError enumeration. See #GError for information on
150 <!-- ##### USER_FUNCTION GOptionArgFunc ##### -->
152 The type of function to be passed as callback for %G_OPTION_ARG_CALLBACK
156 @option_name: The name of the option being parsed. This will be either a
157 single dash followed by a single letter (for a short name) or two dashes
158 followed by a long option name.
159 @value: The value to be parsed.
160 @data: User data added to the #GOptionGroup containing the option when it
161 was created with g_option_group_new()
162 @error: A return location for errors. The error code %G_OPTION_ERROR_FAILED
163 is intended to be used for errors in #GOptionArgFunc callbacks.
164 @Returns: %TRUE if the option was successfully parsed, %FALSE if an error
168 <!-- ##### STRUCT GOptionContext ##### -->
170 A <structname>GOptionContext</structname> struct defines which options
171 are accepted by the commandline option parser. The struct has only private
172 fields and should not be directly accessed.
176 <!-- ##### FUNCTION g_option_context_new ##### -->
185 <!-- ##### FUNCTION g_option_context_free ##### -->
193 <!-- ##### FUNCTION g_option_context_parse ##### -->
205 <!-- ##### FUNCTION g_option_context_set_help_enabled ##### -->
214 <!-- ##### FUNCTION g_option_context_get_help_enabled ##### -->
223 <!-- ##### FUNCTION g_option_context_set_ignore_unknown_options ##### -->
232 <!-- ##### FUNCTION g_option_context_get_ignore_unknown_options ##### -->
241 <!-- ##### ENUM GOptionArg ##### -->
243 The #GOptionArg enum values determine which type of extra argument the
244 options expect to find. If an option expects an extra argument, it
245 can be specified in several ways; with a short option:
246 <option>-x arg</option>, with a long option: <option>--name arg</option>
247 or combined in a single argument: <option>--name=arg</option>.
250 @G_OPTION_ARG_NONE: No extra argument. This is useful for simple flags.
251 @G_OPTION_ARG_STRING: The option takes a string argument.
252 @G_OPTION_ARG_INT: The option takes an integer argument.
253 @G_OPTION_ARG_CALLBACK: The option provides a callback to parse the
255 @G_OPTION_ARG_FILENAME: The option takes a filename as argument.
256 @G_OPTION_ARG_STRING_ARRAY: The option takes a string argument, multiple
257 uses of the option are collected into an array of strings.
258 @G_OPTION_ARG_FILENAME_ARRAY: The option takes a filename as argument,
259 multiple uses of the option are collected into an array of strings.
261 <!-- ##### ENUM GOptionFlags ##### -->
263 Flags which modify individual options.
266 @G_OPTION_FLAG_HIDDEN: The option doesn't appear in <option>--help</option>
268 @G_OPTION_FLAG_IN_MAIN: The option appears in the main section of the
269 <option>--help</option> output, even if it is defined in a group.
270 @G_OPTION_FLAG_REVERSE: For options of the G_OPTION_ARG_NONE kind, this flag
271 indicates that the sense of the option is reversed.
273 <!-- ##### MACRO G_OPTION_REMAINING ##### -->
275 If a long option in the main group has this name, it is not treated as a
276 regular option. Instead it collects all non-option arguments which would
277 otherwise be left in <literal>argv</literal>. The option must be of type
278 %G_OPTION_ARG_STRING_ARRAY or %G_OPTION_ARG_FILENAME_ARRAY.
282 Using #G_OPTION_REMAINING instead of simply scanning <literal>argv</literal>
283 for leftover arguments has the advantage that GOption takes care of
284 necessary encoding conversions for strings or filenames.
290 <!-- ##### STRUCT GOptionEntry ##### -->
292 A <structname>GOptionEntry</structname> defines a single option.
293 To have an effect, they must be added to a #GOptionGroup with
294 g_option_context_add_main_entries() or g_option_group_add_entries().
297 @long_name: The long name of an option can be used to specify it
298 in a commandline as --<replaceable>long_name</replaceable>. Every
299 option must have a long name. To resolve conflicts if multiple
300 option groups contain the same long name, it is also possible to
301 specify the option as
302 --<replaceable>groupname</replaceable>-<replaceable>long_name</replaceable>.
303 @short_name: If an option has a short name, it can be specified
304 -<replaceable>short_name</replaceable> in a commandline.
305 @flags: Flags from #GOptionFlags.
306 @arg: The type of the option, as a #GOptionArg.
307 @arg_data: If the @arg type is %G_OPTION_ARG_CALLBACK, then @arg_data must
308 point to a #GOptionArgFunc callback function, which will be called to handle
309 the extra argument. Otherwise, @arg_data is a pointer to a location to store
310 the value, the required type of the location depends on the @arg type:
313 <term>%G_OPTION_ARG_NONE</term>
314 <listitem><para>%gboolean</para></listitem>
317 <term>%G_OPTION_ARG_STRING</term>
318 <listitem><para>%gchar*</para></listitem>
321 <term>%G_OPTION_ARG_INT</term>
322 <listitem><para>%gint</para></listitem>
325 <term>%G_OPTION_ARG_FILENAME</term>
326 <listitem><para>%gchar*</para></listitem>
329 <term>%G_OPTION_ARG_STRING_ARRAY</term>
330 <listitem><para>%gchar**</para></listitem>
333 <term>%G_OPTION_ARG_FILENAME_ARRAY</term>
334 <listitem><para>%gchar**</para></listitem>
337 @description: the description for the option in <option>--help</option>
338 output. The @description is translated using the @translate_func of the
339 group, see g_option_group_set_translation_domain().
340 @arg_description: The placeholder to use for the extra argument parsed
341 by the option in <option>--help</option>
342 output. The @arg_description is translated using the @translate_func of the
343 group, see g_option_group_set_translation_domain().
345 <!-- ##### FUNCTION g_option_context_add_main_entries ##### -->
355 <!-- ##### STRUCT GOptionGroup ##### -->
357 A <structname>GOptionGroup</structname> struct defines the options in a single
358 group. The struct has only private fields and should not be directly accessed.
361 All options in a group share the same translation function. Libaries which
362 need to parse commandline options are expected to provide a function for
363 getting a <structname>GOptionGroup</structname> holding their options, which
364 the application can then add to its #GOptionContext.
368 <!-- ##### FUNCTION g_option_context_add_group ##### -->
377 <!-- ##### FUNCTION g_option_context_set_main_group ##### -->
386 <!-- ##### FUNCTION g_option_context_get_main_group ##### -->
395 <!-- ##### FUNCTION g_option_group_new ##### -->
408 <!-- ##### FUNCTION g_option_group_free ##### -->
416 <!-- ##### FUNCTION g_option_group_add_entries ##### -->
425 <!-- ##### USER_FUNCTION GOptionParseFunc ##### -->
427 The type of function that can be called before and after parsing.
430 @context The active #GOptionContext
433 @group: The group to which the function belongs
434 @data: User data added to the #GOptionGroup containing the option when it
435 was created with g_option_group_new()
436 @error: A return location for error details
437 @Returns: %TRUE if the function completed successfully, %FALSE if an error
441 <!-- ##### FUNCTION g_option_group_set_parse_hooks ##### -->
451 <!-- ##### USER_FUNCTION GOptionErrorFunc ##### -->
453 The type of function to be used as callback when a parse error
457 @context The active #GOptionContext
460 @group: The group to which the function belongs
461 @data: User data added to the #GOptionGroup containing the option when it
462 was created with g_option_group_new()
463 @error: The #GError containing details about the parse error
466 <!-- ##### FUNCTION g_option_group_set_error_hook ##### -->
475 <!-- ##### USER_FUNCTION GTranslateFunc ##### -->
477 The type of functions which are used to translate user-visible
478 strings, for <option>--help</option> output.
481 @str: the untranslated string
482 @data: user data specified when installing the function, e.g.
483 in g_option_group_set_translate_func()
484 @Returns: a translation of the string for the current locale.
485 The returned string is owned by GLib and must not be freed.
488 <!-- ##### FUNCTION g_option_group_set_translate_func ##### -->
499 <!-- ##### FUNCTION g_option_group_set_translation_domain ##### -->