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:
1161 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1166 change = get_change (context, G_OPTION_ARG_STRING,
1168 g_free (change->allocated.str);
1170 change->prev.str = *(gchar **)entry->arg_data;
1171 change->allocated.str = data;
1173 *(gchar **)entry->arg_data = data;
1176 case G_OPTION_ARG_STRING_ARRAY:
1180 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1185 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
1188 if (change->allocated.array.len == 0)
1190 change->prev.array = *(gchar ***)entry->arg_data;
1191 change->allocated.array.data = g_new (gchar *, 2);
1194 change->allocated.array.data =
1195 g_renew (gchar *, change->allocated.array.data,
1196 change->allocated.array.len + 2);
1198 change->allocated.array.data[change->allocated.array.len] = data;
1199 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
1201 change->allocated.array.len ++;
1203 *(gchar ***)entry->arg_data = change->allocated.array.data;
1208 case G_OPTION_ARG_FILENAME:
1213 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1218 data = g_strdup (value);
1220 change = get_change (context, G_OPTION_ARG_FILENAME,
1222 g_free (change->allocated.str);
1224 change->prev.str = *(gchar **)entry->arg_data;
1225 change->allocated.str = data;
1227 *(gchar **)entry->arg_data = data;
1231 case G_OPTION_ARG_FILENAME_ARRAY:
1236 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1241 data = g_strdup (value);
1243 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
1246 if (change->allocated.array.len == 0)
1248 change->prev.array = *(gchar ***)entry->arg_data;
1249 change->allocated.array.data = g_new (gchar *, 2);
1252 change->allocated.array.data =
1253 g_renew (gchar *, change->allocated.array.data,
1254 change->allocated.array.len + 2);
1256 change->allocated.array.data[change->allocated.array.len] = data;
1257 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
1259 change->allocated.array.len ++;
1261 *(gchar ***)entry->arg_data = change->allocated.array.data;
1266 case G_OPTION_ARG_INT:
1270 if (!parse_int (option_name, value,
1275 change = get_change (context, G_OPTION_ARG_INT,
1277 change->prev.integer = *(gint *)entry->arg_data;
1278 *(gint *)entry->arg_data = data;
1281 case G_OPTION_ARG_CALLBACK:
1286 if (!value && entry->flags & G_OPTION_FLAG_OPTIONAL_ARG)
1288 else if (entry->flags & G_OPTION_FLAG_NO_ARG)
1290 else if (entry->flags & G_OPTION_FLAG_FILENAME)
1293 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1295 data = g_strdup (value);
1299 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1301 if (!(entry->flags & (G_OPTION_FLAG_NO_ARG|G_OPTION_FLAG_OPTIONAL_ARG)) &&
1305 retval = (* (GOptionArgFunc) entry->arg_data) (option_name, data, group->user_data, error);
1307 if (!retval && error != NULL && *error == NULL)
1309 G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
1310 _("Error parsing option %s"), option_name);
1318 case G_OPTION_ARG_DOUBLE:
1322 if (!parse_double (option_name, value,
1329 change = get_change (context, G_OPTION_ARG_DOUBLE,
1331 change->prev.dbl = *(gdouble *)entry->arg_data;
1332 *(gdouble *)entry->arg_data = data;
1335 case G_OPTION_ARG_INT64:
1339 if (!parse_int64 (option_name, value,
1346 change = get_change (context, G_OPTION_ARG_INT64,
1348 change->prev.int64 = *(gint64 *)entry->arg_data;
1349 *(gint64 *)entry->arg_data = data;
1353 g_assert_not_reached ();
1360 parse_short_option (GOptionContext *context,
1361 GOptionGroup *group,
1372 for (j = 0; j < group->n_entries; j++)
1374 if (arg == group->entries[j].short_name)
1377 gchar *value = NULL;
1379 option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
1381 if (NO_ARG (&group->entries[j]))
1388 G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
1389 _("Error parsing option %s"), option_name);
1390 g_free (option_name);
1394 if (idx < *argc - 1)
1396 if (!OPTIONAL_ARG (&group->entries[j]))
1398 value = (*argv)[idx + 1];
1399 add_pending_null (context, &((*argv)[idx + 1]), NULL);
1404 if ((*argv)[idx + 1][0] == '-')
1408 value = (*argv)[idx + 1];
1409 add_pending_null (context, &((*argv)[idx + 1]), NULL);
1414 else if (idx >= *argc - 1 && OPTIONAL_ARG (&group->entries[j]))
1419 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1420 _("Missing argument for %s"), option_name);
1421 g_free (option_name);
1426 if (!parse_arg (context, group, &group->entries[j],
1427 value, option_name, error))
1429 g_free (option_name);
1433 g_free (option_name);
1442 parse_long_option (GOptionContext *context,
1443 GOptionGroup *group,
1454 for (j = 0; j < group->n_entries; j++)
1459 if (aliased && (group->entries[j].flags & G_OPTION_FLAG_NOALIAS))
1462 if (NO_ARG (&group->entries[j]) &&
1463 strcmp (arg, group->entries[j].long_name) == 0)
1468 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1469 retval = parse_arg (context, group, &group->entries[j],
1470 NULL, option_name, error);
1471 g_free (option_name);
1473 add_pending_null (context, &((*argv)[*idx]), NULL);
1480 gint len = strlen (group->entries[j].long_name);
1482 if (strncmp (arg, group->entries[j].long_name, len) == 0 &&
1483 (arg[len] == '=' || arg[len] == 0))
1485 gchar *value = NULL;
1488 add_pending_null (context, &((*argv)[*idx]), NULL);
1489 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1491 if (arg[len] == '=')
1492 value = arg + len + 1;
1493 else if (*idx < *argc - 1)
1495 if (!OPTIONAL_ARG (&group->entries[j]))
1497 value = (*argv)[*idx + 1];
1498 add_pending_null (context, &((*argv)[*idx + 1]), NULL);
1503 if ((*argv)[*idx + 1][0] == '-')
1506 retval = parse_arg (context, group, &group->entries[j],
1507 NULL, option_name, error);
1509 g_free (option_name);
1514 value = (*argv)[*idx + 1];
1515 add_pending_null (context, &((*argv)[*idx + 1]), NULL);
1520 else if (*idx >= *argc - 1 && OPTIONAL_ARG (&group->entries[j]))
1523 retval = parse_arg (context, group, &group->entries[j],
1524 NULL, option_name, error);
1526 g_free (option_name);
1532 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1533 _("Missing argument for %s"), option_name);
1534 g_free (option_name);
1538 if (!parse_arg (context, group, &group->entries[j],
1539 value, option_name, error))
1541 g_free (option_name);
1545 g_free (option_name);
1555 parse_remaining_arg (GOptionContext *context,
1556 GOptionGroup *group,
1565 for (j = 0; j < group->n_entries; j++)
1570 if (group->entries[j].long_name[0])
1573 g_return_val_if_fail (group->entries[j].arg == G_OPTION_ARG_CALLBACK ||
1574 group->entries[j].arg == G_OPTION_ARG_STRING_ARRAY ||
1575 group->entries[j].arg == G_OPTION_ARG_FILENAME_ARRAY, FALSE);
1577 add_pending_null (context, &((*argv)[*idx]), NULL);
1579 if (!parse_arg (context, group, &group->entries[j], (*argv)[*idx], "", error))
1590 free_changes_list (GOptionContext *context,
1595 for (list = context->changes; list != NULL; list = list->next)
1597 Change *change = list->data;
1601 switch (change->arg_type)
1603 case G_OPTION_ARG_NONE:
1604 *(gboolean *)change->arg_data = change->prev.bool;
1606 case G_OPTION_ARG_INT:
1607 *(gint *)change->arg_data = change->prev.integer;
1609 case G_OPTION_ARG_STRING:
1610 case G_OPTION_ARG_FILENAME:
1611 g_free (change->allocated.str);
1612 *(gchar **)change->arg_data = change->prev.str;
1614 case G_OPTION_ARG_STRING_ARRAY:
1615 case G_OPTION_ARG_FILENAME_ARRAY:
1616 g_strfreev (change->allocated.array.data);
1617 *(gchar ***)change->arg_data = change->prev.array;
1619 case G_OPTION_ARG_DOUBLE:
1620 *(gdouble *)change->arg_data = change->prev.dbl;
1622 case G_OPTION_ARG_INT64:
1623 *(gint64 *)change->arg_data = change->prev.int64;
1626 g_assert_not_reached ();
1633 g_list_free (context->changes);
1634 context->changes = NULL;
1638 free_pending_nulls (GOptionContext *context,
1639 gboolean perform_nulls)
1643 for (list = context->pending_nulls; list != NULL; list = list->next)
1645 PendingNull *n = list->data;
1649 if (context->strv_mode)
1654 /* Copy back the short options */
1656 strcpy (*n->ptr + 1, n->value);
1666 g_list_free (context->pending_nulls);
1667 context->pending_nulls = NULL;
1670 /* Use a platform-specific mechanism to look up the first argument to
1671 * the current process.
1672 * Note if you implement this for other platforms, also add it to
1673 * tests/option-argv0.c
1676 platform_get_argv0 (void)
1683 if (!g_file_get_contents ("/proc/self/cmdline",
1688 /* Sanity check for a NUL terminator. */
1689 if (!memchr (cmdline, 0, len))
1691 /* We could just return cmdline, but I think it's better
1692 * to hold on to a smaller malloc block; the arguments
1695 base_arg0 = g_path_get_basename (cmdline);
1698 #elif defined __OpenBSD__
1699 char **cmdline = NULL;
1701 gsize len = PATH_MAX;
1703 int mib[] = { CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ARGV };
1705 cmdline = (char **) realloc (cmdline, len);
1707 if (sysctl (mib, G_N_ELEMENTS (mib), cmdline, &len, NULL, 0) == -1)
1713 /* We could just return cmdline, but I think it's better
1714 * to hold on to a smaller malloc block; the arguments
1717 base_arg0 = g_path_get_basename (*cmdline);
1726 * g_option_context_parse:
1727 * @context: a #GOptionContext
1728 * @argc: (inout) (allow-none): a pointer to the number of command line arguments
1729 * @argv: (inout) (array length=argc) (allow-none): a pointer to the array of command line arguments
1730 * @error: a return location for errors
1732 * Parses the command line arguments, recognizing options
1733 * which have been added to @context. A side-effect of
1734 * calling this function is that g_set_prgname() will be
1737 * If the parsing is successful, any parsed arguments are
1738 * removed from the array and @argc and @argv are updated
1739 * accordingly. A '--' option is stripped from @argv
1740 * unless there are unparsed options before and after it,
1741 * or some of the options after it start with '-'. In case
1742 * of an error, @argc and @argv are left unmodified.
1744 * If automatic <option>--help</option> support is enabled
1745 * (see g_option_context_set_help_enabled()), and the
1746 * @argv array contains one of the recognized help options,
1747 * this function will produce help output to stdout and
1748 * call <literal>exit (0)</literal>.
1750 * Note that function depends on the
1751 * <link linkend="setlocale">current locale</link> for
1752 * automatic character set conversion of string and filename
1755 * Return value: %TRUE if the parsing was successful,
1756 * %FALSE if an error occurred
1761 g_option_context_parse (GOptionContext *context,
1769 /* Set program name */
1770 if (!g_get_prgname())
1774 if (argc && argv && *argc)
1775 prgname = g_path_get_basename ((*argv)[0]);
1777 prgname = platform_get_argv0 ();
1780 g_set_prgname (prgname);
1782 g_set_prgname ("<unknown>");
1787 /* Call pre-parse hooks */
1788 list = context->groups;
1791 GOptionGroup *group = list->data;
1793 if (group->pre_parse_func)
1795 if (!(* group->pre_parse_func) (context, group,
1796 group->user_data, error))
1803 if (context->main_group && context->main_group->pre_parse_func)
1805 if (!(* context->main_group->pre_parse_func) (context, context->main_group,
1806 context->main_group->user_data, error))
1812 gboolean stop_parsing = FALSE;
1813 gboolean has_unknown = FALSE;
1814 gint separator_pos = 0;
1816 for (i = 1; i < *argc; i++)
1819 gboolean parsed = FALSE;
1821 if ((*argv)[i][0] == '-' && (*argv)[i][1] != '\0' && !stop_parsing)
1823 if ((*argv)[i][1] == '-')
1827 arg = (*argv)[i] + 2;
1829 /* '--' terminates list of arguments */
1833 stop_parsing = TRUE;
1837 /* Handle help options */
1838 if (context->help_enabled)
1840 if (strcmp (arg, "help") == 0)
1841 print_help (context, TRUE, NULL);
1842 else if (strcmp (arg, "help-all") == 0)
1843 print_help (context, FALSE, NULL);
1844 else if (strncmp (arg, "help-", 5) == 0)
1846 list = context->groups;
1850 GOptionGroup *group = list->data;
1852 if (strcmp (arg + 5, group->name) == 0)
1853 print_help (context, FALSE, group);
1860 if (context->main_group &&
1861 !parse_long_option (context, context->main_group, &i, arg,
1862 FALSE, argc, argv, error, &parsed))
1868 /* Try the groups */
1869 list = context->groups;
1872 GOptionGroup *group = list->data;
1874 if (!parse_long_option (context, group, &i, arg,
1875 FALSE, argc, argv, error, &parsed))
1887 /* Now look for --<group>-<option> */
1888 dash = strchr (arg, '-');
1891 /* Try the groups */
1892 list = context->groups;
1895 GOptionGroup *group = list->data;
1897 if (strncmp (group->name, arg, dash - arg) == 0)
1899 if (!parse_long_option (context, group, &i, dash + 1,
1900 TRUE, argc, argv, error, &parsed))
1911 if (context->ignore_unknown)
1915 { /* short option */
1916 gint new_i = i, arg_length;
1917 gboolean *nulled_out = NULL;
1918 gboolean has_h_entry = context_has_h_entry (context);
1919 arg = (*argv)[i] + 1;
1920 arg_length = strlen (arg);
1921 nulled_out = g_newa (gboolean, arg_length);
1922 memset (nulled_out, 0, arg_length * sizeof (gboolean));
1923 for (j = 0; j < arg_length; j++)
1925 if (context->help_enabled && (arg[j] == '?' ||
1926 (arg[j] == 'h' && !has_h_entry)))
1927 print_help (context, TRUE, NULL);
1929 if (context->main_group &&
1930 !parse_short_option (context, context->main_group,
1932 argc, argv, error, &parsed))
1936 /* Try the groups */
1937 list = context->groups;
1940 GOptionGroup *group = list->data;
1941 if (!parse_short_option (context, group, i, &new_i, arg[j],
1942 argc, argv, error, &parsed))
1950 if (context->ignore_unknown && parsed)
1951 nulled_out[j] = TRUE;
1952 else if (context->ignore_unknown)
1956 /* !context->ignore_unknown && parsed */
1958 if (context->ignore_unknown)
1960 gchar *new_arg = NULL;
1962 for (j = 0; j < arg_length; j++)
1967 new_arg = g_malloc (arg_length + 1);
1968 new_arg[arg_index++] = arg[j];
1972 new_arg[arg_index] = '\0';
1973 add_pending_null (context, &((*argv)[i]), new_arg);
1977 add_pending_null (context, &((*argv)[i]), NULL);
1985 if (!parsed && !context->ignore_unknown)
1988 G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
1989 _("Unknown option %s"), (*argv)[i]);
1995 /* Collect remaining args */
1996 if (context->main_group &&
1997 !parse_remaining_arg (context, context->main_group, &i,
1998 argc, argv, error, &parsed))
2001 if (!parsed && (has_unknown || (*argv)[i][0] == '-'))
2006 if (separator_pos > 0)
2007 add_pending_null (context, &((*argv)[separator_pos]), NULL);
2011 /* Call post-parse hooks */
2012 list = context->groups;
2015 GOptionGroup *group = list->data;
2017 if (group->post_parse_func)
2019 if (!(* group->post_parse_func) (context, group,
2020 group->user_data, error))
2027 if (context->main_group && context->main_group->post_parse_func)
2029 if (!(* context->main_group->post_parse_func) (context, context->main_group,
2030 context->main_group->user_data, error))
2036 free_pending_nulls (context, TRUE);
2038 for (i = 1; i < *argc; i++)
2040 for (k = i; k < *argc; k++)
2041 if ((*argv)[k] != NULL)
2047 for (j = i + k; j < *argc; j++)
2049 (*argv)[j-k] = (*argv)[j];
2061 /* Call error hooks */
2062 list = context->groups;
2065 GOptionGroup *group = list->data;
2067 if (group->error_func)
2068 (* group->error_func) (context, group,
2069 group->user_data, error);
2074 if (context->main_group && context->main_group->error_func)
2075 (* context->main_group->error_func) (context, context->main_group,
2076 context->main_group->user_data, error);
2078 free_changes_list (context, TRUE);
2079 free_pending_nulls (context, FALSE);
2085 * g_option_group_new:
2086 * @name: the name for the option group, this is used to provide
2087 * help for the options in this group with <option>--help-</option>@name
2088 * @description: a description for this group to be shown in
2089 * <option>--help</option>. This string is translated using the translation
2090 * domain or translation function of the group
2091 * @help_description: a description for the <option>--help-</option>@name option.
2092 * This string is translated using the translation domain or translation function
2094 * @user_data: (allow-none): user data that will be passed to the pre- and post-parse hooks,
2095 * the error hook and to callbacks of %G_OPTION_ARG_CALLBACK options, or %NULL
2096 * @destroy: (allow-none): a function that will be called to free @user_data, or %NULL
2098 * Creates a new #GOptionGroup.
2100 * Return value: a newly created option group. It should be added
2101 * to a #GOptionContext or freed with g_option_group_free().
2106 g_option_group_new (const gchar *name,
2107 const gchar *description,
2108 const gchar *help_description,
2110 GDestroyNotify destroy)
2113 GOptionGroup *group;
2115 group = g_new0 (GOptionGroup, 1);
2116 group->name = g_strdup (name);
2117 group->description = g_strdup (description);
2118 group->help_description = g_strdup (help_description);
2119 group->user_data = user_data;
2120 group->destroy_notify = destroy;
2127 * g_option_group_free:
2128 * @group: a #GOptionGroup
2130 * Frees a #GOptionGroup. Note that you must <emphasis>not</emphasis>
2131 * free groups which have been added to a #GOptionContext.
2136 g_option_group_free (GOptionGroup *group)
2138 g_return_if_fail (group != NULL);
2140 g_free (group->name);
2141 g_free (group->description);
2142 g_free (group->help_description);
2144 g_free (group->entries);
2146 if (group->destroy_notify)
2147 (* group->destroy_notify) (group->user_data);
2149 if (group->translate_notify)
2150 (* group->translate_notify) (group->translate_data);
2157 * g_option_group_add_entries:
2158 * @group: a #GOptionGroup
2159 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
2161 * Adds the options specified in @entries to @group.
2166 g_option_group_add_entries (GOptionGroup *group,
2167 const GOptionEntry *entries)
2171 g_return_if_fail (entries != NULL);
2173 for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++) ;
2175 group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
2177 memcpy (group->entries + group->n_entries, entries, sizeof (GOptionEntry) * n_entries);
2179 for (i = group->n_entries; i < group->n_entries + n_entries; i++)
2181 gchar c = group->entries[i].short_name;
2183 if (c == '-' || (c != 0 && !g_ascii_isprint (c)))
2185 g_warning (G_STRLOC ": ignoring invalid short option '%c' (%d) in entry %s:%s",
2186 c, c, group->name, group->entries[i].long_name);
2187 group->entries[i].short_name = '\0';
2190 if (group->entries[i].arg != G_OPTION_ARG_NONE &&
2191 (group->entries[i].flags & G_OPTION_FLAG_REVERSE) != 0)
2193 g_warning (G_STRLOC ": ignoring reverse flag on option of arg-type %d in entry %s:%s",
2194 group->entries[i].arg, group->name, group->entries[i].long_name);
2196 group->entries[i].flags &= ~G_OPTION_FLAG_REVERSE;
2199 if (group->entries[i].arg != G_OPTION_ARG_CALLBACK &&
2200 (group->entries[i].flags & (G_OPTION_FLAG_NO_ARG|G_OPTION_FLAG_OPTIONAL_ARG|G_OPTION_FLAG_FILENAME)) != 0)
2202 g_warning (G_STRLOC ": ignoring no-arg, optional-arg or filename flags (%d) on option of arg-type %d in entry %s:%s",
2203 group->entries[i].flags, group->entries[i].arg, group->name, group->entries[i].long_name);
2205 group->entries[i].flags &= ~(G_OPTION_FLAG_NO_ARG|G_OPTION_FLAG_OPTIONAL_ARG|G_OPTION_FLAG_FILENAME);
2209 group->n_entries += n_entries;
2213 * g_option_group_set_parse_hooks:
2214 * @group: a #GOptionGroup
2215 * @pre_parse_func: (allow-none): a function to call before parsing, or %NULL
2216 * @post_parse_func: (allow-none): a function to call after parsing, or %NULL
2218 * Associates two functions with @group which will be called
2219 * from g_option_context_parse() before the first option is parsed
2220 * and after the last option has been parsed, respectively.
2222 * Note that the user data to be passed to @pre_parse_func and
2223 * @post_parse_func can be specified when constructing the group
2224 * with g_option_group_new().
2229 g_option_group_set_parse_hooks (GOptionGroup *group,
2230 GOptionParseFunc pre_parse_func,
2231 GOptionParseFunc post_parse_func)
2233 g_return_if_fail (group != NULL);
2235 group->pre_parse_func = pre_parse_func;
2236 group->post_parse_func = post_parse_func;
2240 * g_option_group_set_error_hook:
2241 * @group: a #GOptionGroup
2242 * @error_func: a function to call when an error occurs
2244 * Associates a function with @group which will be called
2245 * from g_option_context_parse() when an error occurs.
2247 * Note that the user data to be passed to @error_func can be
2248 * specified when constructing the group with g_option_group_new().
2253 g_option_group_set_error_hook (GOptionGroup *group,
2254 GOptionErrorFunc error_func)
2256 g_return_if_fail (group != NULL);
2258 group->error_func = error_func;
2263 * g_option_group_set_translate_func:
2264 * @group: a #GOptionGroup
2265 * @func: (allow-none): the #GTranslateFunc, or %NULL
2266 * @data: (allow-none): user data to pass to @func, or %NULL
2267 * @destroy_notify: (allow-none): a function which gets called to free @data, or %NULL
2269 * Sets the function which is used to translate user-visible
2270 * strings, for <option>--help</option> output. Different
2271 * groups can use different #GTranslateFunc<!-- -->s. If @func
2272 * is %NULL, strings are not translated.
2274 * If you are using gettext(), you only need to set the translation
2275 * domain, see g_option_group_set_translation_domain().
2280 g_option_group_set_translate_func (GOptionGroup *group,
2281 GTranslateFunc func,
2283 GDestroyNotify destroy_notify)
2285 g_return_if_fail (group != NULL);
2287 if (group->translate_notify)
2288 group->translate_notify (group->translate_data);
2290 group->translate_func = func;
2291 group->translate_data = data;
2292 group->translate_notify = destroy_notify;
2295 static const gchar *
2296 dgettext_swapped (const gchar *msgid,
2297 const gchar *domainname)
2299 return g_dgettext (domainname, msgid);
2303 * g_option_group_set_translation_domain:
2304 * @group: a #GOptionGroup
2305 * @domain: the domain to use
2307 * A convenience function to use gettext() for translating
2308 * user-visible strings.
2313 g_option_group_set_translation_domain (GOptionGroup *group,
2314 const gchar *domain)
2316 g_return_if_fail (group != NULL);
2318 g_option_group_set_translate_func (group,
2319 (GTranslateFunc)dgettext_swapped,
2325 * g_option_context_set_translate_func:
2326 * @context: a #GOptionContext
2327 * @func: (allow-none): the #GTranslateFunc, or %NULL
2328 * @data: (allow-none): user data to pass to @func, or %NULL
2329 * @destroy_notify: (allow-none): a function which gets called to free @data, or %NULL
2331 * Sets the function which is used to translate the contexts
2332 * user-visible strings, for <option>--help</option> output.
2333 * If @func is %NULL, strings are not translated.
2335 * Note that option groups have their own translation functions,
2336 * this function only affects the @parameter_string (see g_option_context_new()),
2337 * the summary (see g_option_context_set_summary()) and the description
2338 * (see g_option_context_set_description()).
2340 * If you are using gettext(), you only need to set the translation
2341 * domain, see g_option_context_set_translation_domain().
2346 g_option_context_set_translate_func (GOptionContext *context,
2347 GTranslateFunc func,
2349 GDestroyNotify destroy_notify)
2351 g_return_if_fail (context != NULL);
2353 if (context->translate_notify)
2354 context->translate_notify (context->translate_data);
2356 context->translate_func = func;
2357 context->translate_data = data;
2358 context->translate_notify = destroy_notify;
2362 * g_option_context_set_translation_domain:
2363 * @context: a #GOptionContext
2364 * @domain: the domain to use
2366 * A convenience function to use gettext() for translating
2367 * user-visible strings.
2372 g_option_context_set_translation_domain (GOptionContext *context,
2373 const gchar *domain)
2375 g_return_if_fail (context != NULL);
2377 g_option_context_set_translate_func (context,
2378 (GTranslateFunc)dgettext_swapped,
2384 * g_option_context_set_summary:
2385 * @context: a #GOptionContext
2386 * @summary: (allow-none): a string to be shown in <option>--help</option> output
2387 * before the list of options, or %NULL
2389 * Adds a string to be displayed in <option>--help</option> output
2390 * before the list of options. This is typically a summary of the
2391 * program functionality.
2393 * Note that the summary is translated (see
2394 * g_option_context_set_translate_func() and
2395 * g_option_context_set_translation_domain()).
2400 g_option_context_set_summary (GOptionContext *context,
2401 const gchar *summary)
2403 g_return_if_fail (context != NULL);
2405 g_free (context->summary);
2406 context->summary = g_strdup (summary);
2411 * g_option_context_get_summary:
2412 * @context: a #GOptionContext
2414 * Returns the summary. See g_option_context_set_summary().
2416 * Returns: the summary
2421 g_option_context_get_summary (GOptionContext *context)
2423 g_return_val_if_fail (context != NULL, NULL);
2425 return context->summary;
2429 * g_option_context_set_description:
2430 * @context: a #GOptionContext
2431 * @description: (allow-none): a string to be shown in <option>--help</option> output
2432 * after the list of options, or %NULL
2434 * Adds a string to be displayed in <option>--help</option> output
2435 * after the list of options. This text often includes a bug reporting
2438 * Note that the summary is translated (see
2439 * g_option_context_set_translate_func()).
2444 g_option_context_set_description (GOptionContext *context,
2445 const gchar *description)
2447 g_return_if_fail (context != NULL);
2449 g_free (context->description);
2450 context->description = g_strdup (description);
2455 * g_option_context_get_description:
2456 * @context: a #GOptionContext
2458 * Returns the description. See g_option_context_set_description().
2460 * Returns: the description
2465 g_option_context_get_description (GOptionContext *context)
2467 g_return_val_if_fail (context != NULL, NULL);
2469 return context->description;
2473 * g_option_context_parse_strv:
2474 * @context: a #GOptionContext
2475 * @arguments: (inout) (array null-terminated=1): a pointer to the command line arguments
2476 * @error: a return location for errors
2478 * Parses the command line arguments.
2480 * This function is similar to g_option_context_parse() except that it
2481 * respects the normal memory rules when dealing with a strv instead of
2482 * assuming that the passed-in array is the argv of the main function.
2484 * In particular, strings that are removed from the arguments list will
2485 * be freed using g_free().
2487 * This function is useful if you are trying to use #GOptionContext with
2490 * Returns: %TRUE if the parsing was successful,
2491 * %FALSE if an error occurred
2496 g_option_context_parse_strv (GOptionContext *context,
2503 context->strv_mode = TRUE;
2504 argc = g_strv_length (*arguments);
2505 success = g_option_context_parse (context, &argc, arguments, error);
2506 context->strv_mode = FALSE;