1 /* goption.c - Option parser
3 * Copyright (C) 1999, 2003 Red Hat Software
4 * Copyright (C) 2004 Anders Carlsson <andersca@gnome.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
24 * @Short_description: parses commandline options
25 * @Title: Commandline option parser
27 * The GOption commandline parser is intended to be a simpler replacement
28 * for the popt library. It supports short and long commandline options,
29 * as shown in the following example:
31 * <literal>testtreemodel -r 1 --max-size 20 --rand --display=:1.0 -vb -- file1 file2</literal>
33 * The example demonstrates a number of features of the GOption
35 * <itemizedlist><listitem><para>
36 * Options can be single letters, prefixed by a single dash. Multiple
37 * short options can be grouped behind a single dash.
38 * </para></listitem><listitem><para>
39 * Long options are prefixed by two consecutive dashes.
40 * </para></listitem><listitem><para>
41 * Options can have an extra argument, which can be a number, a string or
42 * a filename. For long options, the extra argument can be appended with
43 * an equals sign after the option name, which is useful if the extra
44 * argument starts with a dash, which would otherwise cause it to be
45 * interpreted as another option.
46 * </para></listitem><listitem><para>
47 * Non-option arguments are returned to the application as rest arguments.
48 * </para></listitem><listitem><para>
49 * An argument consisting solely of two dashes turns off further parsing,
50 * any remaining arguments (even those starting with a dash) are returned
51 * to the application as rest arguments.
52 * </para></listitem></itemizedlist>
54 * Another important feature of GOption is that it can automatically
55 * generate nicely formatted help output. Unless it is explicitly turned
56 * off with g_option_context_set_help_enabled(), GOption will recognize
57 * the <option>--help</option>, <option>-?</option>,
58 * <option>--help-all</option> and
59 * <option>--help-</option><replaceable>groupname</replaceable> options
60 * (where <replaceable>groupname</replaceable> is the name of a
61 * #GOptionGroup) and write a text similar to the one shown in the
62 * following example to stdout.
64 * <informalexample><screen>
66 * testtreemodel [OPTION...] - test tree model performance
69 * -h, --help Show help options
70 * --help-all Show all help options
71 * --help-gtk Show GTK+ Options
73 * Application Options:
74 * -r, --repeats=N Average over N repetitions
75 * -m, --max-size=M Test up to 2^M items
76 * --display=DISPLAY X display to use
77 * -v, --verbose Be verbose
78 * -b, --beep Beep when done
79 * --rand Randomize the data
80 * </screen></informalexample>
82 * GOption groups options in #GOptionGroup<!-- -->s, which makes it easy to
83 * incorporate options from multiple sources. The intended use for this is
84 * to let applications collect option groups from the libraries it uses,
85 * add them to their #GOptionContext, and parse all options by a single call
86 * to g_option_context_parse(). See gtk_get_option_group() for an example.
88 * If an option is declared to be of type string or filename, GOption takes
89 * care of converting it to the right encoding; strings are returned in
90 * UTF-8, filenames are returned in the GLib filename encoding. Note that
91 * this only works if setlocale() has been called before
92 * g_option_context_parse().
94 * Here is a complete example of setting up GOption to parse the example
95 * commandline above and produce the example help output.
97 * <informalexample><programlisting>
98 * static gint repeats = 2;
99 * static gint max_size = 8;
100 * static gboolean verbose = FALSE;
101 * static gboolean beep = FALSE;
102 * static gboolean randomize = FALSE;
104 * static GOptionEntry entries[] =
106 * { "repeats", 'r', 0, G_OPTION_ARG_INT, &repeats, "Average over N repetitions", "N" },
107 * { "max-size", 'm', 0, G_OPTION_ARG_INT, &max_size, "Test up to 2^M items", "M" },
108 * { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL },
109 * { "beep", 'b', 0, G_OPTION_ARG_NONE, &beep, "Beep when done", NULL },
110 * { "rand", 0, 0, G_OPTION_ARG_NONE, &randomize, "Randomize the data", NULL },
115 * main (int argc, char *argv[])
117 * GError *error = NULL;
118 * GOptionContext *context;
120 * context = g_option_context_new ("- test tree model performance");
121 * g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
122 * g_option_context_add_group (context, gtk_get_option_group (TRUE));
123 * if (!g_option_context_parse (context, &argc, &argv, &error))
125 * g_print ("option parsing failed: %s\n", error->message);
132 * </programlisting></informalexample>
142 #if defined __OpenBSD__
143 #include <sys/types.h>
145 #include <sys/param.h>
146 #include <sys/sysctl.h>
152 #include "glibintl.h"
154 #define TRANSLATE(group, str) (((group)->translate_func ? (* (group)->translate_func) ((str), (group)->translate_data) : (str)))
156 #define NO_ARG(entry) ((entry)->arg == G_OPTION_ARG_NONE || \
157 ((entry)->arg == G_OPTION_ARG_CALLBACK && \
158 ((entry)->flags & G_OPTION_FLAG_NO_ARG)))
160 #define OPTIONAL_ARG(entry) ((entry)->arg == G_OPTION_ARG_CALLBACK && \
161 (entry)->flags & G_OPTION_FLAG_OPTIONAL_ARG)
193 struct _GOptionContext
197 gchar *parameter_string;
201 GTranslateFunc translate_func;
202 GDestroyNotify translate_notify;
203 gpointer translate_data;
205 guint help_enabled : 1;
206 guint ignore_unknown : 1;
209 GOptionGroup *main_group;
211 /* We keep a list of change so we can revert them */
214 /* We also keep track of all argv elements
215 * that should be NULLed or modified.
217 GList *pending_nulls;
224 gchar *help_description;
226 GDestroyNotify destroy_notify;
229 GTranslateFunc translate_func;
230 GDestroyNotify translate_notify;
231 gpointer translate_data;
233 GOptionEntry *entries;
236 GOptionParseFunc pre_parse_func;
237 GOptionParseFunc post_parse_func;
238 GOptionErrorFunc error_func;
241 static void free_changes_list (GOptionContext *context,
243 static void free_pending_nulls (GOptionContext *context,
244 gboolean perform_nulls);
248 _g_unichar_get_width (gunichar c)
250 if (G_UNLIKELY (g_unichar_iszerowidth (c)))
253 /* we ignore the fact that we should call g_unichar_iswide_cjk() under
254 * some locales (legacy East Asian ones) */
255 if (g_unichar_iswide (c))
262 _g_utf8_strwidth (const gchar *p)
265 g_return_val_if_fail (p != NULL, 0);
269 len += _g_unichar_get_width (g_utf8_get_char (p));
270 p = g_utf8_next_char (p);
276 G_DEFINE_QUARK (g-option-context-error-quark, g_option_error)
279 * g_option_context_new:
280 * @parameter_string: (allow-none): a string which is displayed in
281 * the first line of <option>--help</option> output, after the
283 * <literal><replaceable>programname</replaceable> [OPTION...]</literal>
285 * Creates a new option context.
287 * The @parameter_string can serve multiple purposes. It can be used
288 * to add descriptions for "rest" arguments, which are not parsed by
289 * the #GOptionContext, typically something like "FILES" or
290 * "FILE1 FILE2...". If you are using #G_OPTION_REMAINING for
291 * collecting "rest" arguments, GLib handles this automatically by
292 * using the @arg_description of the corresponding #GOptionEntry in
295 * Another usage is to give a short summary of the program
296 * functionality, like " - frob the strings", which will be displayed
297 * in the same line as the usage. For a longer description of the
298 * program functionality that should be displayed as a paragraph
299 * below the usage line, use g_option_context_set_summary().
301 * Note that the @parameter_string is translated using the
302 * function set with g_option_context_set_translate_func(), so
303 * it should normally be passed untranslated.
305 * Returns: a newly created #GOptionContext, which must be
306 * freed with g_option_context_free() after use.
311 g_option_context_new (const gchar *parameter_string)
314 GOptionContext *context;
316 context = g_new0 (GOptionContext, 1);
318 context->parameter_string = g_strdup (parameter_string);
319 context->help_enabled = TRUE;
320 context->ignore_unknown = FALSE;
326 * g_option_context_free:
327 * @context: a #GOptionContext
329 * Frees context and all the groups which have been
332 * Please note that parsed arguments need to be freed separately (see
337 void g_option_context_free (GOptionContext *context)
339 g_return_if_fail (context != NULL);
341 g_list_free_full (context->groups, (GDestroyNotify) g_option_group_free);
343 if (context->main_group)
344 g_option_group_free (context->main_group);
346 free_changes_list (context, FALSE);
347 free_pending_nulls (context, FALSE);
349 g_free (context->parameter_string);
350 g_free (context->summary);
351 g_free (context->description);
353 if (context->translate_notify)
354 (* context->translate_notify) (context->translate_data);
361 * g_option_context_set_help_enabled:
362 * @context: a #GOptionContext
363 * @help_enabled: %TRUE to enable <option>--help</option>, %FALSE to disable it
365 * Enables or disables automatic generation of <option>--help</option>
366 * output. By default, g_option_context_parse() recognizes
367 * <option>--help</option>, <option>-h</option>,
368 * <option>-?</option>, <option>--help-all</option>
369 * and <option>--help-</option><replaceable>groupname</replaceable> and creates
370 * suitable output to stdout.
374 void g_option_context_set_help_enabled (GOptionContext *context,
375 gboolean help_enabled)
378 g_return_if_fail (context != NULL);
380 context->help_enabled = help_enabled;
384 * g_option_context_get_help_enabled:
385 * @context: a #GOptionContext
387 * Returns whether automatic <option>--help</option> generation
388 * is turned on for @context. See g_option_context_set_help_enabled().
390 * Returns: %TRUE if automatic help generation is turned on.
395 g_option_context_get_help_enabled (GOptionContext *context)
397 g_return_val_if_fail (context != NULL, FALSE);
399 return context->help_enabled;
403 * g_option_context_set_ignore_unknown_options:
404 * @context: a #GOptionContext
405 * @ignore_unknown: %TRUE to ignore unknown options, %FALSE to produce
406 * an error when unknown options are met
408 * Sets whether to ignore unknown options or not. If an argument is
409 * ignored, it is left in the @argv array after parsing. By default,
410 * g_option_context_parse() treats unknown options as error.
412 * This setting does not affect non-option arguments (i.e. arguments
413 * which don't start with a dash). But note that GOption cannot reliably
414 * determine whether a non-option belongs to a preceding unknown option.
419 g_option_context_set_ignore_unknown_options (GOptionContext *context,
420 gboolean ignore_unknown)
422 g_return_if_fail (context != NULL);
424 context->ignore_unknown = ignore_unknown;
428 * g_option_context_get_ignore_unknown_options:
429 * @context: a #GOptionContext
431 * Returns whether unknown options are ignored or not. See
432 * g_option_context_set_ignore_unknown_options().
434 * Returns: %TRUE if unknown options are ignored.
439 g_option_context_get_ignore_unknown_options (GOptionContext *context)
441 g_return_val_if_fail (context != NULL, FALSE);
443 return context->ignore_unknown;
447 * g_option_context_add_group:
448 * @context: a #GOptionContext
449 * @group: the group to add
451 * Adds a #GOptionGroup to the @context, so that parsing with @context
452 * will recognize the options in the group. Note that the group will
453 * be freed together with the context when g_option_context_free() is
454 * called, so you must not free the group yourself after adding it
460 g_option_context_add_group (GOptionContext *context,
465 g_return_if_fail (context != NULL);
466 g_return_if_fail (group != NULL);
467 g_return_if_fail (group->name != NULL);
468 g_return_if_fail (group->description != NULL);
469 g_return_if_fail (group->help_description != NULL);
471 for (list = context->groups; list; list = list->next)
473 GOptionGroup *g = (GOptionGroup *)list->data;
475 if ((group->name == NULL && g->name == NULL) ||
476 (group->name && g->name && strcmp (group->name, g->name) == 0))
477 g_warning ("A group named \"%s\" is already part of this GOptionContext",
481 context->groups = g_list_append (context->groups, group);
485 * g_option_context_set_main_group:
486 * @context: a #GOptionContext
487 * @group: the group to set as main group
489 * Sets a #GOptionGroup as main group of the @context.
490 * This has the same effect as calling g_option_context_add_group(),
491 * the only difference is that the options in the main group are
492 * treated differently when generating <option>--help</option> output.
497 g_option_context_set_main_group (GOptionContext *context,
500 g_return_if_fail (context != NULL);
501 g_return_if_fail (group != NULL);
503 if (context->main_group)
505 g_warning ("This GOptionContext already has a main group");
510 context->main_group = group;
514 * g_option_context_get_main_group:
515 * @context: a #GOptionContext
517 * Returns a pointer to the main group of @context.
519 * Return value: the main group of @context, or %NULL if @context doesn't
520 * have a main group. Note that group belongs to @context and should
521 * not be modified or freed.
526 g_option_context_get_main_group (GOptionContext *context)
528 g_return_val_if_fail (context != NULL, NULL);
530 return context->main_group;
534 * g_option_context_add_main_entries:
535 * @context: a #GOptionContext
536 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
537 * @translation_domain: (allow-none): a translation domain to use for translating
538 * the <option>--help</option> output for the options in @entries
539 * with gettext(), or %NULL
541 * A convenience function which creates a main group if it doesn't
542 * exist, adds the @entries to it and sets the translation domain.
547 g_option_context_add_main_entries (GOptionContext *context,
548 const GOptionEntry *entries,
549 const gchar *translation_domain)
551 g_return_if_fail (entries != NULL);
553 if (!context->main_group)
554 context->main_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
556 g_option_group_add_entries (context->main_group, entries);
557 g_option_group_set_translation_domain (context->main_group, translation_domain);
561 calculate_max_length (GOptionGroup *group,
565 gint i, len, max_length;
566 const gchar *long_name;
570 for (i = 0; i < group->n_entries; i++)
572 entry = &group->entries[i];
574 if (entry->flags & G_OPTION_FLAG_HIDDEN)
577 long_name = g_hash_table_lookup (aliases, &entry->long_name);
579 long_name = entry->long_name;
580 len = _g_utf8_strwidth (long_name);
582 if (entry->short_name)
585 if (!NO_ARG (entry) && entry->arg_description)
586 len += 1 + _g_utf8_strwidth (TRANSLATE (group, entry->arg_description));
588 max_length = MAX (max_length, len);
595 print_entry (GOptionGroup *group,
597 const GOptionEntry *entry,
602 const gchar *long_name;
604 if (entry->flags & G_OPTION_FLAG_HIDDEN)
607 if (entry->long_name[0] == 0)
610 long_name = g_hash_table_lookup (aliases, &entry->long_name);
612 long_name = entry->long_name;
614 str = g_string_new (NULL);
616 if (entry->short_name)
617 g_string_append_printf (str, " -%c, --%s", entry->short_name, long_name);
619 g_string_append_printf (str, " --%s", long_name);
621 if (entry->arg_description)
622 g_string_append_printf (str, "=%s", TRANSLATE (group, entry->arg_description));
624 g_string_append_printf (string, "%s%*s %s\n", str->str,
625 (int) (max_length + 4 - _g_utf8_strwidth (str->str)), "",
626 entry->description ? TRANSLATE (group, entry->description) : "");
627 g_string_free (str, TRUE);
631 group_has_visible_entries (GOptionContext *context,
633 gboolean main_entries)
635 GOptionFlags reject_filter = G_OPTION_FLAG_HIDDEN;
638 gboolean main_group = group == context->main_group;
641 reject_filter |= G_OPTION_FLAG_IN_MAIN;
643 for (i = 0, l = (group ? group->n_entries : 0); i < l; i++)
645 entry = &group->entries[i];
647 if (main_entries && !main_group && !(entry->flags & G_OPTION_FLAG_IN_MAIN))
649 if (entry->long_name[0] == 0) /* ignore rest entry */
651 if (!(entry->flags & reject_filter))
659 group_list_has_visible_entries (GOptionContext *context,
661 gboolean main_entries)
665 if (group_has_visible_entries (context, group_list->data, main_entries))
668 group_list = group_list->next;
675 context_has_h_entry (GOptionContext *context)
680 if (context->main_group)
682 for (i = 0; i < context->main_group->n_entries; i++)
684 if (context->main_group->entries[i].short_name == 'h')
689 for (list = context->groups; list != NULL; list = g_list_next (list))
693 group = (GOptionGroup*)list->data;
694 for (i = 0; i < group->n_entries; i++)
696 if (group->entries[i].short_name == 'h')
704 * g_option_context_get_help:
705 * @context: a #GOptionContext
706 * @main_help: if %TRUE, only include the main group
707 * @group: (allow-none): the #GOptionGroup to create help for, or %NULL
709 * Returns a formatted, translated help text for the given context.
710 * To obtain the text produced by <option>--help</option>, call
711 * <literal>g_option_context_get_help (context, TRUE, NULL)</literal>.
712 * To obtain the text produced by <option>--help-all</option>, call
713 * <literal>g_option_context_get_help (context, FALSE, NULL)</literal>.
714 * To obtain the help text for an option group, call
715 * <literal>g_option_context_get_help (context, FALSE, group)</literal>.
717 * Returns: A newly allocated string containing the help text
722 g_option_context_get_help (GOptionContext *context,
727 gint max_length = 0, len;
730 GHashTable *shadow_map;
733 const gchar *rest_description;
737 string = g_string_sized_new (1024);
739 rest_description = NULL;
740 if (context->main_group)
743 for (i = 0; i < context->main_group->n_entries; i++)
745 entry = &context->main_group->entries[i];
746 if (entry->long_name[0] == 0)
748 rest_description = TRANSLATE (context->main_group, entry->arg_description);
754 g_string_append_printf (string, "%s\n %s %s",
755 _("Usage:"), g_get_prgname(), _("[OPTION...]"));
757 if (rest_description)
759 g_string_append (string, " ");
760 g_string_append (string, rest_description);
763 if (context->parameter_string)
765 g_string_append (string, " ");
766 g_string_append (string, TRANSLATE (context, context->parameter_string));
769 g_string_append (string, "\n\n");
771 if (context->summary)
773 g_string_append (string, TRANSLATE (context, context->summary));
774 g_string_append (string, "\n\n");
777 memset (seen, 0, sizeof (gboolean) * 256);
778 shadow_map = g_hash_table_new (g_str_hash, g_str_equal);
779 aliases = g_hash_table_new_full (NULL, NULL, NULL, g_free);
781 if (context->main_group)
783 for (i = 0; i < context->main_group->n_entries; i++)
785 entry = &context->main_group->entries[i];
786 g_hash_table_insert (shadow_map,
787 (gpointer)entry->long_name,
790 if (seen[(guchar)entry->short_name])
791 entry->short_name = 0;
793 seen[(guchar)entry->short_name] = TRUE;
797 list = context->groups;
800 GOptionGroup *g = list->data;
801 for (i = 0; i < g->n_entries; i++)
803 entry = &g->entries[i];
804 if (g_hash_table_lookup (shadow_map, entry->long_name) &&
805 !(entry->flags & G_OPTION_FLAG_NOALIAS))
807 g_hash_table_insert (aliases, &entry->long_name,
808 g_strdup_printf ("%s-%s", g->name, entry->long_name));
811 g_hash_table_insert (shadow_map, (gpointer)entry->long_name, entry);
813 if (seen[(guchar)entry->short_name] &&
814 !(entry->flags & G_OPTION_FLAG_NOALIAS))
815 entry->short_name = 0;
817 seen[(guchar)entry->short_name] = TRUE;
822 g_hash_table_destroy (shadow_map);
824 list = context->groups;
826 if (context->help_enabled)
828 max_length = _g_utf8_strwidth ("-?, --help");
832 len = _g_utf8_strwidth ("--help-all");
833 max_length = MAX (max_length, len);
837 if (context->main_group)
839 len = calculate_max_length (context->main_group, aliases);
840 max_length = MAX (max_length, len);
845 GOptionGroup *g = list->data;
847 if (context->help_enabled)
849 /* First, we check the --help-<groupname> options */
850 len = _g_utf8_strwidth ("--help-") + _g_utf8_strwidth (g->name);
851 max_length = MAX (max_length, len);
854 /* Then we go through the entries */
855 len = calculate_max_length (g, aliases);
856 max_length = MAX (max_length, len);
861 /* Add a bit of padding */
864 if (!group && context->help_enabled)
866 list = context->groups;
868 token = context_has_h_entry (context) ? '?' : 'h';
870 g_string_append_printf (string, "%s\n -%c, --%-*s %s\n",
871 _("Help Options:"), token, max_length - 4, "help",
872 _("Show help options"));
874 /* We only want --help-all when there are groups */
876 g_string_append_printf (string, " --%-*s %s\n",
877 max_length, "help-all",
878 _("Show all help options"));
882 GOptionGroup *g = list->data;
884 if (group_has_visible_entries (context, g, FALSE))
885 g_string_append_printf (string, " --help-%-*s %s\n",
886 max_length - 5, g->name,
887 TRANSLATE (g, g->help_description));
892 g_string_append (string, "\n");
897 /* Print a certain group */
899 if (group_has_visible_entries (context, group, FALSE))
901 g_string_append (string, TRANSLATE (group, group->description));
902 g_string_append (string, "\n");
903 for (i = 0; i < group->n_entries; i++)
904 print_entry (group, max_length, &group->entries[i], string, aliases);
905 g_string_append (string, "\n");
910 /* Print all groups */
912 list = context->groups;
916 GOptionGroup *g = list->data;
918 if (group_has_visible_entries (context, g, FALSE))
920 g_string_append (string, g->description);
921 g_string_append (string, "\n");
922 for (i = 0; i < g->n_entries; i++)
923 if (!(g->entries[i].flags & G_OPTION_FLAG_IN_MAIN))
924 print_entry (g, max_length, &g->entries[i], string, aliases);
926 g_string_append (string, "\n");
933 /* Print application options if --help or --help-all has been specified */
934 if ((main_help || !group) &&
935 (group_has_visible_entries (context, context->main_group, TRUE) ||
936 group_list_has_visible_entries (context, context->groups, TRUE)))
938 list = context->groups;
940 g_string_append (string, _("Application Options:"));
941 g_string_append (string, "\n");
942 if (context->main_group)
943 for (i = 0; i < context->main_group->n_entries; i++)
944 print_entry (context->main_group, max_length,
945 &context->main_group->entries[i], string, aliases);
949 GOptionGroup *g = list->data;
951 /* Print main entries from other groups */
952 for (i = 0; i < g->n_entries; i++)
953 if (g->entries[i].flags & G_OPTION_FLAG_IN_MAIN)
954 print_entry (g, max_length, &g->entries[i], string, aliases);
959 g_string_append (string, "\n");
962 if (context->description)
964 g_string_append (string, TRANSLATE (context, context->description));
965 g_string_append (string, "\n");
968 g_hash_table_destroy (aliases);
970 return g_string_free (string, FALSE);
975 print_help (GOptionContext *context,
981 help = g_option_context_get_help (context, main_help, group);
982 g_print ("%s", help);
989 parse_int (const gchar *arg_name,
998 tmp = strtol (arg, &end, 0);
1000 if (*arg == '\0' || *end != '\0')
1003 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1004 _("Cannot parse integer value '%s' for %s"),
1010 if (*result != tmp || errno == ERANGE)
1013 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1014 _("Integer value '%s' for %s out of range"),
1024 parse_double (const gchar *arg_name,
1033 tmp = g_strtod (arg, &end);
1035 if (*arg == '\0' || *end != '\0')
1038 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1039 _("Cannot parse double value '%s' for %s"),
1043 if (errno == ERANGE)
1046 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1047 _("Double value '%s' for %s out of range"),
1059 parse_int64 (const gchar *arg_name,
1068 tmp = g_ascii_strtoll (arg, &end, 0);
1070 if (*arg == '\0' || *end != '\0')
1073 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1074 _("Cannot parse integer value '%s' for %s"),
1078 if (errno == ERANGE)
1081 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1082 _("Integer value '%s' for %s out of range"),
1094 get_change (GOptionContext *context,
1095 GOptionArg arg_type,
1099 Change *change = NULL;
1101 for (list = context->changes; list != NULL; list = list->next)
1103 change = list->data;
1105 if (change->arg_data == arg_data)
1109 change = g_new0 (Change, 1);
1110 change->arg_type = arg_type;
1111 change->arg_data = arg_data;
1113 context->changes = g_list_prepend (context->changes, change);
1121 add_pending_null (GOptionContext *context,
1127 n = g_new0 (PendingNull, 1);
1131 context->pending_nulls = g_list_prepend (context->pending_nulls, n);
1135 parse_arg (GOptionContext *context,
1136 GOptionGroup *group,
1137 GOptionEntry *entry,
1139 const gchar *option_name,
1145 g_assert (value || OPTIONAL_ARG (entry) || NO_ARG (entry));
1149 case G_OPTION_ARG_NONE:
1151 change = get_change (context, G_OPTION_ARG_NONE,
1154 *(gboolean *)entry->arg_data = !(entry->flags & G_OPTION_FLAG_REVERSE);
1157 case G_OPTION_ARG_STRING:
1162 if (!context->strv_mode)
1163 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1165 data = g_strdup (value);
1167 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1173 change = get_change (context, G_OPTION_ARG_STRING,
1175 g_free (change->allocated.str);
1177 change->prev.str = *(gchar **)entry->arg_data;
1178 change->allocated.str = data;
1180 *(gchar **)entry->arg_data = data;
1183 case G_OPTION_ARG_STRING_ARRAY:
1188 if (!context->strv_mode)
1189 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1191 data = g_strdup (value);
1193 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1199 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
1202 if (change->allocated.array.len == 0)
1204 change->prev.array = *(gchar ***)entry->arg_data;
1205 change->allocated.array.data = g_new (gchar *, 2);
1208 change->allocated.array.data =
1209 g_renew (gchar *, change->allocated.array.data,
1210 change->allocated.array.len + 2);
1212 change->allocated.array.data[change->allocated.array.len] = data;
1213 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
1215 change->allocated.array.len ++;
1217 *(gchar ***)entry->arg_data = change->allocated.array.data;
1222 case G_OPTION_ARG_FILENAME:
1227 if (!context->strv_mode)
1228 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1230 data = g_strdup (value);
1235 data = g_strdup (value);
1237 change = get_change (context, G_OPTION_ARG_FILENAME,
1239 g_free (change->allocated.str);
1241 change->prev.str = *(gchar **)entry->arg_data;
1242 change->allocated.str = data;
1244 *(gchar **)entry->arg_data = data;
1248 case G_OPTION_ARG_FILENAME_ARRAY:
1253 if (!context->strv_mode)
1254 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1256 data = g_strdup (value);
1261 data = g_strdup (value);
1263 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
1266 if (change->allocated.array.len == 0)
1268 change->prev.array = *(gchar ***)entry->arg_data;
1269 change->allocated.array.data = g_new (gchar *, 2);
1272 change->allocated.array.data =
1273 g_renew (gchar *, change->allocated.array.data,
1274 change->allocated.array.len + 2);
1276 change->allocated.array.data[change->allocated.array.len] = data;
1277 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
1279 change->allocated.array.len ++;
1281 *(gchar ***)entry->arg_data = change->allocated.array.data;
1286 case G_OPTION_ARG_INT:
1290 if (!parse_int (option_name, value,
1295 change = get_change (context, G_OPTION_ARG_INT,
1297 change->prev.integer = *(gint *)entry->arg_data;
1298 *(gint *)entry->arg_data = data;
1301 case G_OPTION_ARG_CALLBACK:
1306 if (!value && entry->flags & G_OPTION_FLAG_OPTIONAL_ARG)
1308 else if (entry->flags & G_OPTION_FLAG_NO_ARG)
1310 else if (entry->flags & G_OPTION_FLAG_FILENAME)
1313 if (!context->strv_mode)
1314 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1316 data = g_strdup (value);
1318 data = g_strdup (value);
1322 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1324 if (!(entry->flags & (G_OPTION_FLAG_NO_ARG|G_OPTION_FLAG_OPTIONAL_ARG)) &&
1328 retval = (* (GOptionArgFunc) entry->arg_data) (option_name, data, group->user_data, error);
1330 if (!retval && error != NULL && *error == NULL)
1332 G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
1333 _("Error parsing option %s"), option_name);
1341 case G_OPTION_ARG_DOUBLE:
1345 if (!parse_double (option_name, value,
1352 change = get_change (context, G_OPTION_ARG_DOUBLE,
1354 change->prev.dbl = *(gdouble *)entry->arg_data;
1355 *(gdouble *)entry->arg_data = data;
1358 case G_OPTION_ARG_INT64:
1362 if (!parse_int64 (option_name, value,
1369 change = get_change (context, G_OPTION_ARG_INT64,
1371 change->prev.int64 = *(gint64 *)entry->arg_data;
1372 *(gint64 *)entry->arg_data = data;
1376 g_assert_not_reached ();
1383 parse_short_option (GOptionContext *context,
1384 GOptionGroup *group,
1395 for (j = 0; j < group->n_entries; j++)
1397 if (arg == group->entries[j].short_name)
1400 gchar *value = NULL;
1402 option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
1404 if (NO_ARG (&group->entries[j]))
1411 G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
1412 _("Error parsing option %s"), option_name);
1413 g_free (option_name);
1417 if (idx < *argc - 1)
1419 if (!OPTIONAL_ARG (&group->entries[j]))
1421 value = (*argv)[idx + 1];
1422 add_pending_null (context, &((*argv)[idx + 1]), NULL);
1427 if ((*argv)[idx + 1][0] == '-')
1431 value = (*argv)[idx + 1];
1432 add_pending_null (context, &((*argv)[idx + 1]), NULL);
1437 else if (idx >= *argc - 1 && OPTIONAL_ARG (&group->entries[j]))
1442 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1443 _("Missing argument for %s"), option_name);
1444 g_free (option_name);
1449 if (!parse_arg (context, group, &group->entries[j],
1450 value, option_name, error))
1452 g_free (option_name);
1456 g_free (option_name);
1465 parse_long_option (GOptionContext *context,
1466 GOptionGroup *group,
1477 for (j = 0; j < group->n_entries; j++)
1482 if (aliased && (group->entries[j].flags & G_OPTION_FLAG_NOALIAS))
1485 if (NO_ARG (&group->entries[j]) &&
1486 strcmp (arg, group->entries[j].long_name) == 0)
1491 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1492 retval = parse_arg (context, group, &group->entries[j],
1493 NULL, option_name, error);
1494 g_free (option_name);
1496 add_pending_null (context, &((*argv)[*idx]), NULL);
1503 gint len = strlen (group->entries[j].long_name);
1505 if (strncmp (arg, group->entries[j].long_name, len) == 0 &&
1506 (arg[len] == '=' || arg[len] == 0))
1508 gchar *value = NULL;
1511 add_pending_null (context, &((*argv)[*idx]), NULL);
1512 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1514 if (arg[len] == '=')
1515 value = arg + len + 1;
1516 else if (*idx < *argc - 1)
1518 if (!OPTIONAL_ARG (&group->entries[j]))
1520 value = (*argv)[*idx + 1];
1521 add_pending_null (context, &((*argv)[*idx + 1]), NULL);
1526 if ((*argv)[*idx + 1][0] == '-')
1529 retval = parse_arg (context, group, &group->entries[j],
1530 NULL, option_name, error);
1532 g_free (option_name);
1537 value = (*argv)[*idx + 1];
1538 add_pending_null (context, &((*argv)[*idx + 1]), NULL);
1543 else if (*idx >= *argc - 1 && OPTIONAL_ARG (&group->entries[j]))
1546 retval = parse_arg (context, group, &group->entries[j],
1547 NULL, option_name, error);
1549 g_free (option_name);
1555 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1556 _("Missing argument for %s"), option_name);
1557 g_free (option_name);
1561 if (!parse_arg (context, group, &group->entries[j],
1562 value, option_name, error))
1564 g_free (option_name);
1568 g_free (option_name);
1578 parse_remaining_arg (GOptionContext *context,
1579 GOptionGroup *group,
1588 for (j = 0; j < group->n_entries; j++)
1593 if (group->entries[j].long_name[0])
1596 g_return_val_if_fail (group->entries[j].arg == G_OPTION_ARG_CALLBACK ||
1597 group->entries[j].arg == G_OPTION_ARG_STRING_ARRAY ||
1598 group->entries[j].arg == G_OPTION_ARG_FILENAME_ARRAY, FALSE);
1600 add_pending_null (context, &((*argv)[*idx]), NULL);
1602 if (!parse_arg (context, group, &group->entries[j], (*argv)[*idx], "", error))
1613 free_changes_list (GOptionContext *context,
1618 for (list = context->changes; list != NULL; list = list->next)
1620 Change *change = list->data;
1624 switch (change->arg_type)
1626 case G_OPTION_ARG_NONE:
1627 *(gboolean *)change->arg_data = change->prev.bool;
1629 case G_OPTION_ARG_INT:
1630 *(gint *)change->arg_data = change->prev.integer;
1632 case G_OPTION_ARG_STRING:
1633 case G_OPTION_ARG_FILENAME:
1634 g_free (change->allocated.str);
1635 *(gchar **)change->arg_data = change->prev.str;
1637 case G_OPTION_ARG_STRING_ARRAY:
1638 case G_OPTION_ARG_FILENAME_ARRAY:
1639 g_strfreev (change->allocated.array.data);
1640 *(gchar ***)change->arg_data = change->prev.array;
1642 case G_OPTION_ARG_DOUBLE:
1643 *(gdouble *)change->arg_data = change->prev.dbl;
1645 case G_OPTION_ARG_INT64:
1646 *(gint64 *)change->arg_data = change->prev.int64;
1649 g_assert_not_reached ();
1656 g_list_free (context->changes);
1657 context->changes = NULL;
1661 free_pending_nulls (GOptionContext *context,
1662 gboolean perform_nulls)
1666 for (list = context->pending_nulls; list != NULL; list = list->next)
1668 PendingNull *n = list->data;
1672 if (context->strv_mode)
1677 /* Copy back the short options */
1679 strcpy (*n->ptr + 1, n->value);
1689 g_list_free (context->pending_nulls);
1690 context->pending_nulls = NULL;
1693 /* Use a platform-specific mechanism to look up the first argument to
1694 * the current process.
1695 * Note if you implement this for other platforms, also add it to
1696 * tests/option-argv0.c
1699 platform_get_argv0 (void)
1706 if (!g_file_get_contents ("/proc/self/cmdline",
1711 /* Sanity check for a NUL terminator. */
1712 if (!memchr (cmdline, 0, len))
1714 /* We could just return cmdline, but I think it's better
1715 * to hold on to a smaller malloc block; the arguments
1718 base_arg0 = g_path_get_basename (cmdline);
1721 #elif defined __OpenBSD__
1722 char **cmdline = NULL;
1724 gsize len = PATH_MAX;
1726 int mib[] = { CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ARGV };
1728 cmdline = (char **) realloc (cmdline, len);
1730 if (sysctl (mib, G_N_ELEMENTS (mib), cmdline, &len, NULL, 0) == -1)
1736 /* We could just return cmdline, but I think it's better
1737 * to hold on to a smaller malloc block; the arguments
1740 base_arg0 = g_path_get_basename (*cmdline);
1749 * g_option_context_parse:
1750 * @context: a #GOptionContext
1751 * @argc: (inout) (allow-none): a pointer to the number of command line arguments
1752 * @argv: (inout) (array length=argc) (allow-none): a pointer to the array of command line arguments
1753 * @error: a return location for errors
1755 * Parses the command line arguments, recognizing options
1756 * which have been added to @context. A side-effect of
1757 * calling this function is that g_set_prgname() will be
1760 * If the parsing is successful, any parsed arguments are
1761 * removed from the array and @argc and @argv are updated
1762 * accordingly. A '--' option is stripped from @argv
1763 * unless there are unparsed options before and after it,
1764 * or some of the options after it start with '-'. In case
1765 * of an error, @argc and @argv are left unmodified.
1767 * If automatic <option>--help</option> support is enabled
1768 * (see g_option_context_set_help_enabled()), and the
1769 * @argv array contains one of the recognized help options,
1770 * this function will produce help output to stdout and
1771 * call <literal>exit (0)</literal>.
1773 * Note that function depends on the
1774 * <link linkend="setlocale">current locale</link> for
1775 * automatic character set conversion of string and filename
1778 * Return value: %TRUE if the parsing was successful,
1779 * %FALSE if an error occurred
1784 g_option_context_parse (GOptionContext *context,
1792 /* Set program name */
1793 if (!g_get_prgname())
1797 if (argc && argv && *argc)
1798 prgname = g_path_get_basename ((*argv)[0]);
1800 prgname = platform_get_argv0 ();
1803 g_set_prgname (prgname);
1805 g_set_prgname ("<unknown>");
1810 /* Call pre-parse hooks */
1811 list = context->groups;
1814 GOptionGroup *group = list->data;
1816 if (group->pre_parse_func)
1818 if (!(* group->pre_parse_func) (context, group,
1819 group->user_data, error))
1826 if (context->main_group && context->main_group->pre_parse_func)
1828 if (!(* context->main_group->pre_parse_func) (context, context->main_group,
1829 context->main_group->user_data, error))
1835 gboolean stop_parsing = FALSE;
1836 gboolean has_unknown = FALSE;
1837 gint separator_pos = 0;
1839 for (i = 1; i < *argc; i++)
1842 gboolean parsed = FALSE;
1844 if ((*argv)[i][0] == '-' && (*argv)[i][1] != '\0' && !stop_parsing)
1846 if ((*argv)[i][1] == '-')
1850 arg = (*argv)[i] + 2;
1852 /* '--' terminates list of arguments */
1856 stop_parsing = TRUE;
1860 /* Handle help options */
1861 if (context->help_enabled)
1863 if (strcmp (arg, "help") == 0)
1864 print_help (context, TRUE, NULL);
1865 else if (strcmp (arg, "help-all") == 0)
1866 print_help (context, FALSE, NULL);
1867 else if (strncmp (arg, "help-", 5) == 0)
1869 list = context->groups;
1873 GOptionGroup *group = list->data;
1875 if (strcmp (arg + 5, group->name) == 0)
1876 print_help (context, FALSE, group);
1883 if (context->main_group &&
1884 !parse_long_option (context, context->main_group, &i, arg,
1885 FALSE, argc, argv, error, &parsed))
1891 /* Try the groups */
1892 list = context->groups;
1895 GOptionGroup *group = list->data;
1897 if (!parse_long_option (context, group, &i, arg,
1898 FALSE, argc, argv, error, &parsed))
1910 /* Now look for --<group>-<option> */
1911 dash = strchr (arg, '-');
1914 /* Try the groups */
1915 list = context->groups;
1918 GOptionGroup *group = list->data;
1920 if (strncmp (group->name, arg, dash - arg) == 0)
1922 if (!parse_long_option (context, group, &i, dash + 1,
1923 TRUE, argc, argv, error, &parsed))
1934 if (context->ignore_unknown)
1938 { /* short option */
1939 gint new_i = i, arg_length;
1940 gboolean *nulled_out = NULL;
1941 gboolean has_h_entry = context_has_h_entry (context);
1942 arg = (*argv)[i] + 1;
1943 arg_length = strlen (arg);
1944 nulled_out = g_newa (gboolean, arg_length);
1945 memset (nulled_out, 0, arg_length * sizeof (gboolean));
1946 for (j = 0; j < arg_length; j++)
1948 if (context->help_enabled && (arg[j] == '?' ||
1949 (arg[j] == 'h' && !has_h_entry)))
1950 print_help (context, TRUE, NULL);
1952 if (context->main_group &&
1953 !parse_short_option (context, context->main_group,
1955 argc, argv, error, &parsed))
1959 /* Try the groups */
1960 list = context->groups;
1963 GOptionGroup *group = list->data;
1964 if (!parse_short_option (context, group, i, &new_i, arg[j],
1965 argc, argv, error, &parsed))
1973 if (context->ignore_unknown && parsed)
1974 nulled_out[j] = TRUE;
1975 else if (context->ignore_unknown)
1979 /* !context->ignore_unknown && parsed */
1981 if (context->ignore_unknown)
1983 gchar *new_arg = NULL;
1985 for (j = 0; j < arg_length; j++)
1990 new_arg = g_malloc (arg_length + 1);
1991 new_arg[arg_index++] = arg[j];
1995 new_arg[arg_index] = '\0';
1996 add_pending_null (context, &((*argv)[i]), new_arg);
2000 add_pending_null (context, &((*argv)[i]), NULL);
2008 if (!parsed && !context->ignore_unknown)
2011 G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
2012 _("Unknown option %s"), (*argv)[i]);
2018 /* Collect remaining args */
2019 if (context->main_group &&
2020 !parse_remaining_arg (context, context->main_group, &i,
2021 argc, argv, error, &parsed))
2024 if (!parsed && (has_unknown || (*argv)[i][0] == '-'))
2029 if (separator_pos > 0)
2030 add_pending_null (context, &((*argv)[separator_pos]), NULL);
2034 /* Call post-parse hooks */
2035 list = context->groups;
2038 GOptionGroup *group = list->data;
2040 if (group->post_parse_func)
2042 if (!(* group->post_parse_func) (context, group,
2043 group->user_data, error))
2050 if (context->main_group && context->main_group->post_parse_func)
2052 if (!(* context->main_group->post_parse_func) (context, context->main_group,
2053 context->main_group->user_data, error))
2059 free_pending_nulls (context, TRUE);
2061 for (i = 1; i < *argc; i++)
2063 for (k = i; k < *argc; k++)
2064 if ((*argv)[k] != NULL)
2070 for (j = i + k; j < *argc; j++)
2072 (*argv)[j-k] = (*argv)[j];
2084 /* Call error hooks */
2085 list = context->groups;
2088 GOptionGroup *group = list->data;
2090 if (group->error_func)
2091 (* group->error_func) (context, group,
2092 group->user_data, error);
2097 if (context->main_group && context->main_group->error_func)
2098 (* context->main_group->error_func) (context, context->main_group,
2099 context->main_group->user_data, error);
2101 free_changes_list (context, TRUE);
2102 free_pending_nulls (context, FALSE);
2108 * g_option_group_new:
2109 * @name: the name for the option group, this is used to provide
2110 * help for the options in this group with <option>--help-</option>@name
2111 * @description: a description for this group to be shown in
2112 * <option>--help</option>. This string is translated using the translation
2113 * domain or translation function of the group
2114 * @help_description: a description for the <option>--help-</option>@name option.
2115 * This string is translated using the translation domain or translation function
2117 * @user_data: (allow-none): user data that will be passed to the pre- and post-parse hooks,
2118 * the error hook and to callbacks of %G_OPTION_ARG_CALLBACK options, or %NULL
2119 * @destroy: (allow-none): a function that will be called to free @user_data, or %NULL
2121 * Creates a new #GOptionGroup.
2123 * Return value: a newly created option group. It should be added
2124 * to a #GOptionContext or freed with g_option_group_free().
2129 g_option_group_new (const gchar *name,
2130 const gchar *description,
2131 const gchar *help_description,
2133 GDestroyNotify destroy)
2136 GOptionGroup *group;
2138 group = g_new0 (GOptionGroup, 1);
2139 group->name = g_strdup (name);
2140 group->description = g_strdup (description);
2141 group->help_description = g_strdup (help_description);
2142 group->user_data = user_data;
2143 group->destroy_notify = destroy;
2150 * g_option_group_free:
2151 * @group: a #GOptionGroup
2153 * Frees a #GOptionGroup. Note that you must <emphasis>not</emphasis>
2154 * free groups which have been added to a #GOptionContext.
2159 g_option_group_free (GOptionGroup *group)
2161 g_return_if_fail (group != NULL);
2163 g_free (group->name);
2164 g_free (group->description);
2165 g_free (group->help_description);
2167 g_free (group->entries);
2169 if (group->destroy_notify)
2170 (* group->destroy_notify) (group->user_data);
2172 if (group->translate_notify)
2173 (* group->translate_notify) (group->translate_data);
2180 * g_option_group_add_entries:
2181 * @group: a #GOptionGroup
2182 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
2184 * Adds the options specified in @entries to @group.
2189 g_option_group_add_entries (GOptionGroup *group,
2190 const GOptionEntry *entries)
2194 g_return_if_fail (entries != NULL);
2196 for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++) ;
2198 group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
2200 memcpy (group->entries + group->n_entries, entries, sizeof (GOptionEntry) * n_entries);
2202 for (i = group->n_entries; i < group->n_entries + n_entries; i++)
2204 gchar c = group->entries[i].short_name;
2206 if (c == '-' || (c != 0 && !g_ascii_isprint (c)))
2208 g_warning (G_STRLOC ": ignoring invalid short option '%c' (%d) in entry %s:%s",
2209 c, c, group->name, group->entries[i].long_name);
2210 group->entries[i].short_name = '\0';
2213 if (group->entries[i].arg != G_OPTION_ARG_NONE &&
2214 (group->entries[i].flags & G_OPTION_FLAG_REVERSE) != 0)
2216 g_warning (G_STRLOC ": ignoring reverse flag on option of arg-type %d in entry %s:%s",
2217 group->entries[i].arg, group->name, group->entries[i].long_name);
2219 group->entries[i].flags &= ~G_OPTION_FLAG_REVERSE;
2222 if (group->entries[i].arg != G_OPTION_ARG_CALLBACK &&
2223 (group->entries[i].flags & (G_OPTION_FLAG_NO_ARG|G_OPTION_FLAG_OPTIONAL_ARG|G_OPTION_FLAG_FILENAME)) != 0)
2225 g_warning (G_STRLOC ": ignoring no-arg, optional-arg or filename flags (%d) on option of arg-type %d in entry %s:%s",
2226 group->entries[i].flags, group->entries[i].arg, group->name, group->entries[i].long_name);
2228 group->entries[i].flags &= ~(G_OPTION_FLAG_NO_ARG|G_OPTION_FLAG_OPTIONAL_ARG|G_OPTION_FLAG_FILENAME);
2232 group->n_entries += n_entries;
2236 * g_option_group_set_parse_hooks:
2237 * @group: a #GOptionGroup
2238 * @pre_parse_func: (allow-none): a function to call before parsing, or %NULL
2239 * @post_parse_func: (allow-none): a function to call after parsing, or %NULL
2241 * Associates two functions with @group which will be called
2242 * from g_option_context_parse() before the first option is parsed
2243 * and after the last option has been parsed, respectively.
2245 * Note that the user data to be passed to @pre_parse_func and
2246 * @post_parse_func can be specified when constructing the group
2247 * with g_option_group_new().
2252 g_option_group_set_parse_hooks (GOptionGroup *group,
2253 GOptionParseFunc pre_parse_func,
2254 GOptionParseFunc post_parse_func)
2256 g_return_if_fail (group != NULL);
2258 group->pre_parse_func = pre_parse_func;
2259 group->post_parse_func = post_parse_func;
2263 * g_option_group_set_error_hook:
2264 * @group: a #GOptionGroup
2265 * @error_func: a function to call when an error occurs
2267 * Associates a function with @group which will be called
2268 * from g_option_context_parse() when an error occurs.
2270 * Note that the user data to be passed to @error_func can be
2271 * specified when constructing the group with g_option_group_new().
2276 g_option_group_set_error_hook (GOptionGroup *group,
2277 GOptionErrorFunc error_func)
2279 g_return_if_fail (group != NULL);
2281 group->error_func = error_func;
2286 * g_option_group_set_translate_func:
2287 * @group: a #GOptionGroup
2288 * @func: (allow-none): the #GTranslateFunc, or %NULL
2289 * @data: (allow-none): user data to pass to @func, or %NULL
2290 * @destroy_notify: (allow-none): a function which gets called to free @data, or %NULL
2292 * Sets the function which is used to translate user-visible
2293 * strings, for <option>--help</option> output. Different
2294 * groups can use different #GTranslateFunc<!-- -->s. If @func
2295 * is %NULL, strings are not translated.
2297 * If you are using gettext(), you only need to set the translation
2298 * domain, see g_option_group_set_translation_domain().
2303 g_option_group_set_translate_func (GOptionGroup *group,
2304 GTranslateFunc func,
2306 GDestroyNotify destroy_notify)
2308 g_return_if_fail (group != NULL);
2310 if (group->translate_notify)
2311 group->translate_notify (group->translate_data);
2313 group->translate_func = func;
2314 group->translate_data = data;
2315 group->translate_notify = destroy_notify;
2318 static const gchar *
2319 dgettext_swapped (const gchar *msgid,
2320 const gchar *domainname)
2322 return g_dgettext (domainname, msgid);
2326 * g_option_group_set_translation_domain:
2327 * @group: a #GOptionGroup
2328 * @domain: the domain to use
2330 * A convenience function to use gettext() for translating
2331 * user-visible strings.
2336 g_option_group_set_translation_domain (GOptionGroup *group,
2337 const gchar *domain)
2339 g_return_if_fail (group != NULL);
2341 g_option_group_set_translate_func (group,
2342 (GTranslateFunc)dgettext_swapped,
2348 * g_option_context_set_translate_func:
2349 * @context: a #GOptionContext
2350 * @func: (allow-none): the #GTranslateFunc, or %NULL
2351 * @data: (allow-none): user data to pass to @func, or %NULL
2352 * @destroy_notify: (allow-none): a function which gets called to free @data, or %NULL
2354 * Sets the function which is used to translate the contexts
2355 * user-visible strings, for <option>--help</option> output.
2356 * If @func is %NULL, strings are not translated.
2358 * Note that option groups have their own translation functions,
2359 * this function only affects the @parameter_string (see g_option_context_new()),
2360 * the summary (see g_option_context_set_summary()) and the description
2361 * (see g_option_context_set_description()).
2363 * If you are using gettext(), you only need to set the translation
2364 * domain, see g_option_context_set_translation_domain().
2369 g_option_context_set_translate_func (GOptionContext *context,
2370 GTranslateFunc func,
2372 GDestroyNotify destroy_notify)
2374 g_return_if_fail (context != NULL);
2376 if (context->translate_notify)
2377 context->translate_notify (context->translate_data);
2379 context->translate_func = func;
2380 context->translate_data = data;
2381 context->translate_notify = destroy_notify;
2385 * g_option_context_set_translation_domain:
2386 * @context: a #GOptionContext
2387 * @domain: the domain to use
2389 * A convenience function to use gettext() for translating
2390 * user-visible strings.
2395 g_option_context_set_translation_domain (GOptionContext *context,
2396 const gchar *domain)
2398 g_return_if_fail (context != NULL);
2400 g_option_context_set_translate_func (context,
2401 (GTranslateFunc)dgettext_swapped,
2407 * g_option_context_set_summary:
2408 * @context: a #GOptionContext
2409 * @summary: (allow-none): a string to be shown in <option>--help</option> output
2410 * before the list of options, or %NULL
2412 * Adds a string to be displayed in <option>--help</option> output
2413 * before the list of options. This is typically a summary of the
2414 * program functionality.
2416 * Note that the summary is translated (see
2417 * g_option_context_set_translate_func() and
2418 * g_option_context_set_translation_domain()).
2423 g_option_context_set_summary (GOptionContext *context,
2424 const gchar *summary)
2426 g_return_if_fail (context != NULL);
2428 g_free (context->summary);
2429 context->summary = g_strdup (summary);
2434 * g_option_context_get_summary:
2435 * @context: a #GOptionContext
2437 * Returns the summary. See g_option_context_set_summary().
2439 * Returns: the summary
2444 g_option_context_get_summary (GOptionContext *context)
2446 g_return_val_if_fail (context != NULL, NULL);
2448 return context->summary;
2452 * g_option_context_set_description:
2453 * @context: a #GOptionContext
2454 * @description: (allow-none): a string to be shown in <option>--help</option> output
2455 * after the list of options, or %NULL
2457 * Adds a string to be displayed in <option>--help</option> output
2458 * after the list of options. This text often includes a bug reporting
2461 * Note that the summary is translated (see
2462 * g_option_context_set_translate_func()).
2467 g_option_context_set_description (GOptionContext *context,
2468 const gchar *description)
2470 g_return_if_fail (context != NULL);
2472 g_free (context->description);
2473 context->description = g_strdup (description);
2478 * g_option_context_get_description:
2479 * @context: a #GOptionContext
2481 * Returns the description. See g_option_context_set_description().
2483 * Returns: the description
2488 g_option_context_get_description (GOptionContext *context)
2490 g_return_val_if_fail (context != NULL, NULL);
2492 return context->description;
2496 * g_option_context_parse_strv:
2497 * @context: a #GOptionContext
2498 * @arguments: (inout) (array null-terminated=1): a pointer to the
2499 * command line arguments (which must be in UTF-8 on Windows)
2500 * @error: a return location for errors
2502 * Parses the command line arguments.
2504 * This function is similar to g_option_context_parse() except that it
2505 * respects the normal memory rules when dealing with a strv instead of
2506 * assuming that the passed-in array is the argv of the main function.
2508 * In particular, strings that are removed from the arguments list will
2509 * be freed using g_free().
2511 * On Windows, the strings are expected to be in UTF-8. This is in
2512 * contrast to g_option_context_parse() which expects them to be in the
2513 * system codepage, which is how they are passed as @argv to main().
2514 * See g_win32_get_command_line() for a solution.
2516 * This function is useful if you are trying to use #GOptionContext with
2519 * Returns: %TRUE if the parsing was successful,
2520 * %FALSE if an error occurred
2525 g_option_context_parse_strv (GOptionContext *context,
2532 context->strv_mode = TRUE;
2533 argc = g_strv_length (*arguments);
2534 success = g_option_context_parse (context, &argc, arguments, error);
2535 context->strv_mode = FALSE;