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 rand = 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, &rand, "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;
208 GOptionGroup *main_group;
210 /* We keep a list of change so we can revert them */
213 /* We also keep track of all argv elements
214 * that should be NULLed or modified.
216 GList *pending_nulls;
223 gchar *help_description;
225 GDestroyNotify destroy_notify;
228 GTranslateFunc translate_func;
229 GDestroyNotify translate_notify;
230 gpointer translate_data;
232 GOptionEntry *entries;
235 GOptionParseFunc pre_parse_func;
236 GOptionParseFunc post_parse_func;
237 GOptionErrorFunc error_func;
240 static void free_changes_list (GOptionContext *context,
242 static void free_pending_nulls (GOptionContext *context,
243 gboolean perform_nulls);
247 _g_unichar_get_width (gunichar c)
249 if (G_UNLIKELY (g_unichar_iszerowidth (c)))
252 /* we ignore the fact that we should call g_unichar_iswide_cjk() under
253 * some locales (legacy East Asian ones) */
254 if (g_unichar_iswide (c))
261 _g_utf8_strwidth (const gchar *p)
264 g_return_val_if_fail (p != NULL, 0);
268 len += _g_unichar_get_width (g_utf8_get_char (p));
269 p = g_utf8_next_char (p);
277 g_option_error_quark (void)
279 return g_quark_from_static_string ("g-option-context-error-quark");
283 * g_option_context_new:
284 * @parameter_string: a string which is displayed in
285 * the first line of <option>--help</option> output, after the
287 * <literal><replaceable>programname</replaceable> [OPTION...]</literal>
289 * Creates a new option context.
291 * The @parameter_string can serve multiple purposes. It can be used
292 * to add descriptions for "rest" arguments, which are not parsed by
293 * the #GOptionContext, typically something like "FILES" or
294 * "FILE1 FILE2...". If you are using #G_OPTION_REMAINING for
295 * collecting "rest" arguments, GLib handles this automatically by
296 * using the @arg_description of the corresponding #GOptionEntry in
299 * Another usage is to give a short summary of the program
300 * functionality, like " - frob the strings", which will be displayed
301 * in the same line as the usage. For a longer description of the
302 * program functionality that should be displayed as a paragraph
303 * below the usage line, use g_option_context_set_summary().
305 * Note that the @parameter_string is translated using the
306 * function set with g_option_context_set_translate_func(), so
307 * it should normally be passed untranslated.
309 * Returns: a newly created #GOptionContext, which must be
310 * freed with g_option_context_free() after use.
315 g_option_context_new (const gchar *parameter_string)
318 GOptionContext *context;
320 context = g_new0 (GOptionContext, 1);
322 context->parameter_string = g_strdup (parameter_string);
323 context->help_enabled = TRUE;
324 context->ignore_unknown = FALSE;
330 * g_option_context_free:
331 * @context: a #GOptionContext
333 * Frees context and all the groups which have been
336 * Please note that parsed arguments need to be freed separately (see
341 void g_option_context_free (GOptionContext *context)
343 g_return_if_fail (context != NULL);
345 g_list_free_full (context->groups, (GDestroyNotify) g_option_group_free);
347 if (context->main_group)
348 g_option_group_free (context->main_group);
350 free_changes_list (context, FALSE);
351 free_pending_nulls (context, FALSE);
353 g_free (context->parameter_string);
354 g_free (context->summary);
355 g_free (context->description);
357 if (context->translate_notify)
358 (* context->translate_notify) (context->translate_data);
365 * g_option_context_set_help_enabled:
366 * @context: a #GOptionContext
367 * @help_enabled: %TRUE to enable <option>--help</option>, %FALSE to disable it
369 * Enables or disables automatic generation of <option>--help</option>
370 * output. By default, g_option_context_parse() recognizes
371 * <option>--help</option>, <option>-h</option>,
372 * <option>-?</option>, <option>--help-all</option>
373 * and <option>--help-</option><replaceable>groupname</replaceable> and creates
374 * suitable output to stdout.
378 void g_option_context_set_help_enabled (GOptionContext *context,
379 gboolean help_enabled)
382 g_return_if_fail (context != NULL);
384 context->help_enabled = help_enabled;
388 * g_option_context_get_help_enabled:
389 * @context: a #GOptionContext
391 * Returns whether automatic <option>--help</option> generation
392 * is turned on for @context. See g_option_context_set_help_enabled().
394 * Returns: %TRUE if automatic help generation is turned on.
399 g_option_context_get_help_enabled (GOptionContext *context)
401 g_return_val_if_fail (context != NULL, FALSE);
403 return context->help_enabled;
407 * g_option_context_set_ignore_unknown_options:
408 * @context: a #GOptionContext
409 * @ignore_unknown: %TRUE to ignore unknown options, %FALSE to produce
410 * an error when unknown options are met
412 * Sets whether to ignore unknown options or not. If an argument is
413 * ignored, it is left in the @argv array after parsing. By default,
414 * g_option_context_parse() treats unknown options as error.
416 * This setting does not affect non-option arguments (i.e. arguments
417 * which don't start with a dash). But note that GOption cannot reliably
418 * determine whether a non-option belongs to a preceding unknown option.
423 g_option_context_set_ignore_unknown_options (GOptionContext *context,
424 gboolean ignore_unknown)
426 g_return_if_fail (context != NULL);
428 context->ignore_unknown = ignore_unknown;
432 * g_option_context_get_ignore_unknown_options:
433 * @context: a #GOptionContext
435 * Returns whether unknown options are ignored or not. See
436 * g_option_context_set_ignore_unknown_options().
438 * Returns: %TRUE if unknown options are ignored.
443 g_option_context_get_ignore_unknown_options (GOptionContext *context)
445 g_return_val_if_fail (context != NULL, FALSE);
447 return context->ignore_unknown;
451 * g_option_context_add_group:
452 * @context: a #GOptionContext
453 * @group: the group to add
455 * Adds a #GOptionGroup to the @context, so that parsing with @context
456 * will recognize the options in the group. Note that the group will
457 * be freed together with the context when g_option_context_free() is
458 * called, so you must not free the group yourself after adding it
464 g_option_context_add_group (GOptionContext *context,
469 g_return_if_fail (context != NULL);
470 g_return_if_fail (group != NULL);
471 g_return_if_fail (group->name != NULL);
472 g_return_if_fail (group->description != NULL);
473 g_return_if_fail (group->help_description != NULL);
475 for (list = context->groups; list; list = list->next)
477 GOptionGroup *g = (GOptionGroup *)list->data;
479 if ((group->name == NULL && g->name == NULL) ||
480 (group->name && g->name && strcmp (group->name, g->name) == 0))
481 g_warning ("A group named \"%s\" is already part of this GOptionContext",
485 context->groups = g_list_append (context->groups, group);
489 * g_option_context_set_main_group:
490 * @context: a #GOptionContext
491 * @group: the group to set as main group
493 * Sets a #GOptionGroup as main group of the @context.
494 * This has the same effect as calling g_option_context_add_group(),
495 * the only difference is that the options in the main group are
496 * treated differently when generating <option>--help</option> output.
501 g_option_context_set_main_group (GOptionContext *context,
504 g_return_if_fail (context != NULL);
505 g_return_if_fail (group != NULL);
507 if (context->main_group)
509 g_warning ("This GOptionContext already has a main group");
514 context->main_group = group;
518 * g_option_context_get_main_group:
519 * @context: a #GOptionContext
521 * Returns a pointer to the main group of @context.
523 * Return value: the main group of @context, or %NULL if @context doesn't
524 * have a main group. Note that group belongs to @context and should
525 * not be modified or freed.
530 g_option_context_get_main_group (GOptionContext *context)
532 g_return_val_if_fail (context != NULL, NULL);
534 return context->main_group;
538 * g_option_context_add_main_entries:
539 * @context: a #GOptionContext
540 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
541 * @translation_domain: (allow-none): a translation domain to use for translating
542 * the <option>--help</option> output for the options in @entries
543 * with gettext(), or %NULL
545 * A convenience function which creates a main group if it doesn't
546 * exist, adds the @entries to it and sets the translation domain.
551 g_option_context_add_main_entries (GOptionContext *context,
552 const GOptionEntry *entries,
553 const gchar *translation_domain)
555 g_return_if_fail (entries != NULL);
557 if (!context->main_group)
558 context->main_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
560 g_option_group_add_entries (context->main_group, entries);
561 g_option_group_set_translation_domain (context->main_group, translation_domain);
565 calculate_max_length (GOptionGroup *group)
568 gint i, len, max_length;
572 for (i = 0; i < group->n_entries; i++)
574 entry = &group->entries[i];
576 if (entry->flags & G_OPTION_FLAG_HIDDEN)
579 len = _g_utf8_strwidth (entry->long_name);
581 if (entry->short_name)
584 if (!NO_ARG (entry) && entry->arg_description)
585 len += 1 + _g_utf8_strwidth (TRANSLATE (group, entry->arg_description));
587 max_length = MAX (max_length, len);
594 print_entry (GOptionGroup *group,
596 const GOptionEntry *entry,
601 if (entry->flags & G_OPTION_FLAG_HIDDEN)
604 if (entry->long_name[0] == 0)
607 str = g_string_new (NULL);
609 if (entry->short_name)
610 g_string_append_printf (str, " -%c, --%s", entry->short_name, entry->long_name);
612 g_string_append_printf (str, " --%s", entry->long_name);
614 if (entry->arg_description)
615 g_string_append_printf (str, "=%s", TRANSLATE (group, entry->arg_description));
617 g_string_append_printf (string, "%s%*s %s\n", str->str,
618 (int) (max_length + 4 - _g_utf8_strwidth (str->str)), "",
619 entry->description ? TRANSLATE (group, entry->description) : "");
620 g_string_free (str, TRUE);
624 group_has_visible_entries (GOptionContext *context,
626 gboolean main_entries)
628 GOptionFlags reject_filter = G_OPTION_FLAG_HIDDEN;
631 gboolean main_group = group == context->main_group;
634 reject_filter |= G_OPTION_FLAG_IN_MAIN;
636 for (i = 0, l = (group ? group->n_entries : 0); i < l; i++)
638 entry = &group->entries[i];
640 if (main_entries && !main_group && !(entry->flags & G_OPTION_FLAG_IN_MAIN))
642 if (entry->long_name[0] == 0) /* ignore rest entry */
644 if (!(entry->flags & reject_filter))
652 group_list_has_visible_entries (GOptionContext *context,
654 gboolean main_entries)
658 if (group_has_visible_entries (context, group_list->data, main_entries))
661 group_list = group_list->next;
668 context_has_h_entry (GOptionContext *context)
673 if (context->main_group)
675 for (i = 0; i < context->main_group->n_entries; i++)
677 if (context->main_group->entries[i].short_name == 'h')
682 for (list = context->groups; list != NULL; list = g_list_next (list))
686 group = (GOptionGroup*)list->data;
687 for (i = 0; i < group->n_entries; i++)
689 if (group->entries[i].short_name == 'h')
697 * g_option_context_get_help:
698 * @context: a #GOptionContext
699 * @main_help: if %TRUE, only include the main group
700 * @group: (allow-none): the #GOptionGroup to create help for, or %NULL
702 * Returns a formatted, translated help text for the given context.
703 * To obtain the text produced by <option>--help</option>, call
704 * <literal>g_option_context_get_help (context, TRUE, NULL)</literal>.
705 * To obtain the text produced by <option>--help-all</option>, call
706 * <literal>g_option_context_get_help (context, FALSE, NULL)</literal>.
707 * To obtain the help text for an option group, call
708 * <literal>g_option_context_get_help (context, FALSE, group)</literal>.
710 * Returns: A newly allocated string containing the help text
715 g_option_context_get_help (GOptionContext *context,
720 gint max_length, len;
723 GHashTable *shadow_map;
725 const gchar *rest_description;
729 string = g_string_sized_new (1024);
731 rest_description = NULL;
732 if (context->main_group)
735 for (i = 0; i < context->main_group->n_entries; i++)
737 entry = &context->main_group->entries[i];
738 if (entry->long_name[0] == 0)
740 rest_description = TRANSLATE (context->main_group, entry->arg_description);
746 g_string_append_printf (string, "%s\n %s %s",
747 _("Usage:"), g_get_prgname(), _("[OPTION...]"));
749 if (rest_description)
751 g_string_append (string, " ");
752 g_string_append (string, rest_description);
755 if (context->parameter_string)
757 g_string_append (string, " ");
758 g_string_append (string, TRANSLATE (context, context->parameter_string));
761 g_string_append (string, "\n\n");
763 if (context->summary)
765 g_string_append (string, TRANSLATE (context, context->summary));
766 g_string_append (string, "\n\n");
769 memset (seen, 0, sizeof (gboolean) * 256);
770 shadow_map = g_hash_table_new (g_str_hash, g_str_equal);
772 if (context->main_group)
774 for (i = 0; i < context->main_group->n_entries; i++)
776 entry = &context->main_group->entries[i];
777 g_hash_table_insert (shadow_map,
778 (gpointer)entry->long_name,
781 if (seen[(guchar)entry->short_name])
782 entry->short_name = 0;
784 seen[(guchar)entry->short_name] = TRUE;
788 list = context->groups;
791 GOptionGroup *g = list->data;
792 for (i = 0; i < g->n_entries; i++)
794 entry = &g->entries[i];
795 if (g_hash_table_lookup (shadow_map, entry->long_name) &&
796 !(entry->flags & G_OPTION_FLAG_NOALIAS))
797 entry->long_name = g_strdup_printf ("%s-%s", g->name, entry->long_name);
799 g_hash_table_insert (shadow_map, (gpointer)entry->long_name, entry);
801 if (seen[(guchar)entry->short_name] &&
802 !(entry->flags & G_OPTION_FLAG_NOALIAS))
803 entry->short_name = 0;
805 seen[(guchar)entry->short_name] = TRUE;
810 g_hash_table_destroy (shadow_map);
812 list = context->groups;
814 max_length = _g_utf8_strwidth ("-?, --help");
818 len = _g_utf8_strwidth ("--help-all");
819 max_length = MAX (max_length, len);
822 if (context->main_group)
824 len = calculate_max_length (context->main_group);
825 max_length = MAX (max_length, len);
830 GOptionGroup *g = list->data;
832 /* First, we check the --help-<groupname> options */
833 len = _g_utf8_strwidth ("--help-") + _g_utf8_strwidth (g->name);
834 max_length = MAX (max_length, len);
836 /* Then we go through the entries */
837 len = calculate_max_length (g);
838 max_length = MAX (max_length, len);
843 /* Add a bit of padding */
848 list = context->groups;
850 token = context_has_h_entry (context) ? '?' : 'h';
852 g_string_append_printf (string, "%s\n -%c, --%-*s %s\n",
853 _("Help Options:"), token, max_length - 4, "help",
854 _("Show help options"));
856 /* We only want --help-all when there are groups */
858 g_string_append_printf (string, " --%-*s %s\n",
859 max_length, "help-all",
860 _("Show all help options"));
864 GOptionGroup *g = list->data;
866 if (group_has_visible_entries (context, g, FALSE))
867 g_string_append_printf (string, " --help-%-*s %s\n",
868 max_length - 5, g->name,
869 TRANSLATE (g, g->help_description));
874 g_string_append (string, "\n");
879 /* Print a certain group */
881 if (group_has_visible_entries (context, group, FALSE))
883 g_string_append (string, TRANSLATE (group, group->description));
884 g_string_append (string, "\n");
885 for (i = 0; i < group->n_entries; i++)
886 print_entry (group, max_length, &group->entries[i], string);
887 g_string_append (string, "\n");
892 /* Print all groups */
894 list = context->groups;
898 GOptionGroup *g = list->data;
900 if (group_has_visible_entries (context, g, FALSE))
902 g_string_append (string, g->description);
903 g_string_append (string, "\n");
904 for (i = 0; i < g->n_entries; i++)
905 if (!(g->entries[i].flags & G_OPTION_FLAG_IN_MAIN))
906 print_entry (g, max_length, &g->entries[i], string);
908 g_string_append (string, "\n");
915 /* Print application options if --help or --help-all has been specified */
916 if ((main_help || !group) &&
917 (group_has_visible_entries (context, context->main_group, TRUE) ||
918 group_list_has_visible_entries (context, context->groups, TRUE)))
920 list = context->groups;
922 g_string_append (string, _("Application Options:"));
923 g_string_append (string, "\n");
924 if (context->main_group)
925 for (i = 0; i < context->main_group->n_entries; i++)
926 print_entry (context->main_group, max_length,
927 &context->main_group->entries[i], string);
931 GOptionGroup *g = list->data;
933 /* Print main entries from other groups */
934 for (i = 0; i < g->n_entries; i++)
935 if (g->entries[i].flags & G_OPTION_FLAG_IN_MAIN)
936 print_entry (g, max_length, &g->entries[i], string);
941 g_string_append (string, "\n");
944 if (context->description)
946 g_string_append (string, TRANSLATE (context, context->description));
947 g_string_append (string, "\n");
950 return g_string_free (string, FALSE);
955 print_help (GOptionContext *context,
961 help = g_option_context_get_help (context, main_help, group);
962 g_print ("%s", help);
969 parse_int (const gchar *arg_name,
978 tmp = strtol (arg, &end, 0);
980 if (*arg == '\0' || *end != '\0')
983 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
984 _("Cannot parse integer value '%s' for %s"),
990 if (*result != tmp || errno == ERANGE)
993 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
994 _("Integer value '%s' for %s out of range"),
1004 parse_double (const gchar *arg_name,
1013 tmp = g_strtod (arg, &end);
1015 if (*arg == '\0' || *end != '\0')
1018 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1019 _("Cannot parse double value '%s' for %s"),
1023 if (errno == ERANGE)
1026 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1027 _("Double value '%s' for %s out of range"),
1039 parse_int64 (const gchar *arg_name,
1048 tmp = g_ascii_strtoll (arg, &end, 0);
1050 if (*arg == '\0' || *end != '\0')
1053 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1054 _("Cannot parse integer value '%s' for %s"),
1058 if (errno == ERANGE)
1061 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1062 _("Integer value '%s' for %s out of range"),
1074 get_change (GOptionContext *context,
1075 GOptionArg arg_type,
1079 Change *change = NULL;
1081 for (list = context->changes; list != NULL; list = list->next)
1083 change = list->data;
1085 if (change->arg_data == arg_data)
1089 change = g_new0 (Change, 1);
1090 change->arg_type = arg_type;
1091 change->arg_data = arg_data;
1093 context->changes = g_list_prepend (context->changes, change);
1101 add_pending_null (GOptionContext *context,
1107 n = g_new0 (PendingNull, 1);
1111 context->pending_nulls = g_list_prepend (context->pending_nulls, n);
1115 parse_arg (GOptionContext *context,
1116 GOptionGroup *group,
1117 GOptionEntry *entry,
1119 const gchar *option_name,
1125 g_assert (value || OPTIONAL_ARG (entry) || NO_ARG (entry));
1129 case G_OPTION_ARG_NONE:
1131 change = get_change (context, G_OPTION_ARG_NONE,
1134 *(gboolean *)entry->arg_data = !(entry->flags & G_OPTION_FLAG_REVERSE);
1137 case G_OPTION_ARG_STRING:
1141 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1146 change = get_change (context, G_OPTION_ARG_STRING,
1148 g_free (change->allocated.str);
1150 change->prev.str = *(gchar **)entry->arg_data;
1151 change->allocated.str = data;
1153 *(gchar **)entry->arg_data = data;
1156 case G_OPTION_ARG_STRING_ARRAY:
1160 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1165 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
1168 if (change->allocated.array.len == 0)
1170 change->prev.array = *(gchar ***)entry->arg_data;
1171 change->allocated.array.data = g_new (gchar *, 2);
1174 change->allocated.array.data =
1175 g_renew (gchar *, change->allocated.array.data,
1176 change->allocated.array.len + 2);
1178 change->allocated.array.data[change->allocated.array.len] = data;
1179 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
1181 change->allocated.array.len ++;
1183 *(gchar ***)entry->arg_data = change->allocated.array.data;
1188 case G_OPTION_ARG_FILENAME:
1193 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1198 data = g_strdup (value);
1200 change = get_change (context, G_OPTION_ARG_FILENAME,
1202 g_free (change->allocated.str);
1204 change->prev.str = *(gchar **)entry->arg_data;
1205 change->allocated.str = data;
1207 *(gchar **)entry->arg_data = data;
1211 case G_OPTION_ARG_FILENAME_ARRAY:
1216 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1221 data = g_strdup (value);
1223 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
1226 if (change->allocated.array.len == 0)
1228 change->prev.array = *(gchar ***)entry->arg_data;
1229 change->allocated.array.data = g_new (gchar *, 2);
1232 change->allocated.array.data =
1233 g_renew (gchar *, change->allocated.array.data,
1234 change->allocated.array.len + 2);
1236 change->allocated.array.data[change->allocated.array.len] = data;
1237 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
1239 change->allocated.array.len ++;
1241 *(gchar ***)entry->arg_data = change->allocated.array.data;
1246 case G_OPTION_ARG_INT:
1250 if (!parse_int (option_name, value,
1255 change = get_change (context, G_OPTION_ARG_INT,
1257 change->prev.integer = *(gint *)entry->arg_data;
1258 *(gint *)entry->arg_data = data;
1261 case G_OPTION_ARG_CALLBACK:
1266 if (!value && entry->flags & G_OPTION_FLAG_OPTIONAL_ARG)
1268 else if (entry->flags & G_OPTION_FLAG_NO_ARG)
1270 else if (entry->flags & G_OPTION_FLAG_FILENAME)
1273 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1275 data = g_strdup (value);
1279 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1281 if (!(entry->flags & (G_OPTION_FLAG_NO_ARG|G_OPTION_FLAG_OPTIONAL_ARG)) &&
1285 retval = (* (GOptionArgFunc) entry->arg_data) (option_name, data, group->user_data, error);
1287 if (!retval && error != NULL && *error == NULL)
1289 G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
1290 _("Error parsing option %s"), option_name);
1298 case G_OPTION_ARG_DOUBLE:
1302 if (!parse_double (option_name, value,
1309 change = get_change (context, G_OPTION_ARG_DOUBLE,
1311 change->prev.dbl = *(gdouble *)entry->arg_data;
1312 *(gdouble *)entry->arg_data = data;
1315 case G_OPTION_ARG_INT64:
1319 if (!parse_int64 (option_name, value,
1326 change = get_change (context, G_OPTION_ARG_INT64,
1328 change->prev.int64 = *(gint64 *)entry->arg_data;
1329 *(gint64 *)entry->arg_data = data;
1333 g_assert_not_reached ();
1340 parse_short_option (GOptionContext *context,
1341 GOptionGroup *group,
1352 for (j = 0; j < group->n_entries; j++)
1354 if (arg == group->entries[j].short_name)
1357 gchar *value = NULL;
1359 option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
1361 if (NO_ARG (&group->entries[j]))
1368 G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
1369 _("Error parsing option %s"), option_name);
1370 g_free (option_name);
1374 if (idx < *argc - 1)
1376 if (!OPTIONAL_ARG (&group->entries[j]))
1378 value = (*argv)[idx + 1];
1379 add_pending_null (context, &((*argv)[idx + 1]), NULL);
1384 if ((*argv)[idx + 1][0] == '-')
1388 value = (*argv)[idx + 1];
1389 add_pending_null (context, &((*argv)[idx + 1]), NULL);
1394 else if (idx >= *argc - 1 && OPTIONAL_ARG (&group->entries[j]))
1399 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1400 _("Missing argument for %s"), option_name);
1401 g_free (option_name);
1406 if (!parse_arg (context, group, &group->entries[j],
1407 value, option_name, error))
1409 g_free (option_name);
1413 g_free (option_name);
1422 parse_long_option (GOptionContext *context,
1423 GOptionGroup *group,
1434 for (j = 0; j < group->n_entries; j++)
1439 if (aliased && (group->entries[j].flags & G_OPTION_FLAG_NOALIAS))
1442 if (NO_ARG (&group->entries[j]) &&
1443 strcmp (arg, group->entries[j].long_name) == 0)
1448 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1449 retval = parse_arg (context, group, &group->entries[j],
1450 NULL, option_name, error);
1451 g_free (option_name);
1453 add_pending_null (context, &((*argv)[*idx]), NULL);
1460 gint len = strlen (group->entries[j].long_name);
1462 if (strncmp (arg, group->entries[j].long_name, len) == 0 &&
1463 (arg[len] == '=' || arg[len] == 0))
1465 gchar *value = NULL;
1468 add_pending_null (context, &((*argv)[*idx]), NULL);
1469 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1471 if (arg[len] == '=')
1472 value = arg + len + 1;
1473 else if (*idx < *argc - 1)
1475 if (!OPTIONAL_ARG (&group->entries[j]))
1477 value = (*argv)[*idx + 1];
1478 add_pending_null (context, &((*argv)[*idx + 1]), NULL);
1483 if ((*argv)[*idx + 1][0] == '-')
1486 retval = parse_arg (context, group, &group->entries[j],
1487 NULL, option_name, error);
1489 g_free (option_name);
1494 value = (*argv)[*idx + 1];
1495 add_pending_null (context, &((*argv)[*idx + 1]), NULL);
1500 else if (*idx >= *argc - 1 && OPTIONAL_ARG (&group->entries[j]))
1503 retval = parse_arg (context, group, &group->entries[j],
1504 NULL, option_name, error);
1506 g_free (option_name);
1512 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1513 _("Missing argument for %s"), option_name);
1514 g_free (option_name);
1518 if (!parse_arg (context, group, &group->entries[j],
1519 value, option_name, error))
1521 g_free (option_name);
1525 g_free (option_name);
1535 parse_remaining_arg (GOptionContext *context,
1536 GOptionGroup *group,
1545 for (j = 0; j < group->n_entries; j++)
1550 if (group->entries[j].long_name[0])
1553 g_return_val_if_fail (group->entries[j].arg == G_OPTION_ARG_CALLBACK ||
1554 group->entries[j].arg == G_OPTION_ARG_STRING_ARRAY ||
1555 group->entries[j].arg == G_OPTION_ARG_FILENAME_ARRAY, FALSE);
1557 add_pending_null (context, &((*argv)[*idx]), NULL);
1559 if (!parse_arg (context, group, &group->entries[j], (*argv)[*idx], "", error))
1570 free_changes_list (GOptionContext *context,
1575 for (list = context->changes; list != NULL; list = list->next)
1577 Change *change = list->data;
1581 switch (change->arg_type)
1583 case G_OPTION_ARG_NONE:
1584 *(gboolean *)change->arg_data = change->prev.bool;
1586 case G_OPTION_ARG_INT:
1587 *(gint *)change->arg_data = change->prev.integer;
1589 case G_OPTION_ARG_STRING:
1590 case G_OPTION_ARG_FILENAME:
1591 g_free (change->allocated.str);
1592 *(gchar **)change->arg_data = change->prev.str;
1594 case G_OPTION_ARG_STRING_ARRAY:
1595 case G_OPTION_ARG_FILENAME_ARRAY:
1596 g_strfreev (change->allocated.array.data);
1597 *(gchar ***)change->arg_data = change->prev.array;
1599 case G_OPTION_ARG_DOUBLE:
1600 *(gdouble *)change->arg_data = change->prev.dbl;
1602 case G_OPTION_ARG_INT64:
1603 *(gint64 *)change->arg_data = change->prev.int64;
1606 g_assert_not_reached ();
1613 g_list_free (context->changes);
1614 context->changes = NULL;
1618 free_pending_nulls (GOptionContext *context,
1619 gboolean perform_nulls)
1623 for (list = context->pending_nulls; list != NULL; list = list->next)
1625 PendingNull *n = list->data;
1631 /* Copy back the short options */
1633 strcpy (*n->ptr + 1, n->value);
1643 g_list_free (context->pending_nulls);
1644 context->pending_nulls = NULL;
1647 /* Use a platform-specific mechanism to look up the first argument to
1648 * the current process.
1649 * Note if you implement this for other platforms, also add it to
1650 * tests/option-argv0.c
1653 platform_get_argv0 (void)
1660 if (!g_file_get_contents ("/proc/self/cmdline",
1665 /* Sanity check for a NUL terminator. */
1666 if (!memchr (cmdline, 0, len))
1668 /* We could just return cmdline, but I think it's better
1669 * to hold on to a smaller malloc block; the arguments
1672 base_arg0 = g_path_get_basename (cmdline);
1675 #elif defined __OpenBSD__
1676 char **cmdline = NULL;
1678 gsize len = PATH_MAX;
1680 int mib[] = { CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ARGV };
1682 cmdline = (char **) realloc (cmdline, len);
1684 if (sysctl (mib, nitems (mib), cmdline, &len, NULL, 0) == -1)
1690 /* We could just return cmdline, but I think it's better
1691 * to hold on to a smaller malloc block; the arguments
1694 base_arg0 = g_path_get_basename (*cmdline);
1703 * g_option_context_parse:
1704 * @context: a #GOptionContext
1705 * @argc: (inout) (allow-none): a pointer to the number of command line arguments
1706 * @argv: (inout) (array length=argc) (allow-none): a pointer to the array of command line arguments
1707 * @error: a return location for errors
1709 * Parses the command line arguments, recognizing options
1710 * which have been added to @context. A side-effect of
1711 * calling this function is that g_set_prgname() will be
1714 * If the parsing is successful, any parsed arguments are
1715 * removed from the array and @argc and @argv are updated
1716 * accordingly. A '--' option is stripped from @argv
1717 * unless there are unparsed options before and after it,
1718 * or some of the options after it start with '-'. In case
1719 * of an error, @argc and @argv are left unmodified.
1721 * If automatic <option>--help</option> support is enabled
1722 * (see g_option_context_set_help_enabled()), and the
1723 * @argv array contains one of the recognized help options,
1724 * this function will produce help output to stdout and
1725 * call <literal>exit (0)</literal>.
1727 * Note that function depends on the
1728 * <link linkend="setlocale">current locale</link> for
1729 * automatic character set conversion of string and filename
1732 * Return value: %TRUE if the parsing was successful,
1733 * %FALSE if an error occurred
1738 g_option_context_parse (GOptionContext *context,
1746 /* Set program name */
1747 if (!g_get_prgname())
1751 if (argc && argv && *argc)
1752 prgname = g_path_get_basename ((*argv)[0]);
1754 prgname = platform_get_argv0 ();
1757 g_set_prgname (prgname);
1759 g_set_prgname ("<unknown>");
1764 /* Call pre-parse hooks */
1765 list = context->groups;
1768 GOptionGroup *group = list->data;
1770 if (group->pre_parse_func)
1772 if (!(* group->pre_parse_func) (context, group,
1773 group->user_data, error))
1780 if (context->main_group && context->main_group->pre_parse_func)
1782 if (!(* context->main_group->pre_parse_func) (context, context->main_group,
1783 context->main_group->user_data, error))
1789 gboolean stop_parsing = FALSE;
1790 gboolean has_unknown = FALSE;
1791 gint separator_pos = 0;
1793 for (i = 1; i < *argc; i++)
1796 gboolean parsed = FALSE;
1798 if ((*argv)[i][0] == '-' && (*argv)[i][1] != '\0' && !stop_parsing)
1800 if ((*argv)[i][1] == '-')
1804 arg = (*argv)[i] + 2;
1806 /* '--' terminates list of arguments */
1810 stop_parsing = TRUE;
1814 /* Handle help options */
1815 if (context->help_enabled)
1817 if (strcmp (arg, "help") == 0)
1818 print_help (context, TRUE, NULL);
1819 else if (strcmp (arg, "help-all") == 0)
1820 print_help (context, FALSE, NULL);
1821 else if (strncmp (arg, "help-", 5) == 0)
1823 list = context->groups;
1827 GOptionGroup *group = list->data;
1829 if (strcmp (arg + 5, group->name) == 0)
1830 print_help (context, FALSE, group);
1837 if (context->main_group &&
1838 !parse_long_option (context, context->main_group, &i, arg,
1839 FALSE, argc, argv, error, &parsed))
1845 /* Try the groups */
1846 list = context->groups;
1849 GOptionGroup *group = list->data;
1851 if (!parse_long_option (context, group, &i, arg,
1852 FALSE, argc, argv, error, &parsed))
1864 /* Now look for --<group>-<option> */
1865 dash = strchr (arg, '-');
1868 /* Try the groups */
1869 list = context->groups;
1872 GOptionGroup *group = list->data;
1874 if (strncmp (group->name, arg, dash - arg) == 0)
1876 if (!parse_long_option (context, group, &i, dash + 1,
1877 TRUE, argc, argv, error, &parsed))
1888 if (context->ignore_unknown)
1892 { /* short option */
1893 gint new_i = i, arg_length;
1894 gboolean *nulled_out = NULL;
1895 gboolean has_h_entry = context_has_h_entry (context);
1896 arg = (*argv)[i] + 1;
1897 arg_length = strlen (arg);
1898 nulled_out = g_newa (gboolean, arg_length);
1899 memset (nulled_out, 0, arg_length * sizeof (gboolean));
1900 for (j = 0; j < arg_length; j++)
1902 if (context->help_enabled && (arg[j] == '?' ||
1903 (arg[j] == 'h' && !has_h_entry)))
1904 print_help (context, TRUE, NULL);
1906 if (context->main_group &&
1907 !parse_short_option (context, context->main_group,
1909 argc, argv, error, &parsed))
1913 /* Try the groups */
1914 list = context->groups;
1917 GOptionGroup *group = list->data;
1918 if (!parse_short_option (context, group, i, &new_i, arg[j],
1919 argc, argv, error, &parsed))
1927 if (context->ignore_unknown && parsed)
1928 nulled_out[j] = TRUE;
1929 else if (context->ignore_unknown)
1933 /* !context->ignore_unknown && parsed */
1935 if (context->ignore_unknown)
1937 gchar *new_arg = NULL;
1939 for (j = 0; j < arg_length; j++)
1944 new_arg = g_malloc (arg_length + 1);
1945 new_arg[arg_index++] = arg[j];
1949 new_arg[arg_index] = '\0';
1950 add_pending_null (context, &((*argv)[i]), new_arg);
1954 add_pending_null (context, &((*argv)[i]), NULL);
1962 if (!parsed && !context->ignore_unknown)
1965 G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
1966 _("Unknown option %s"), (*argv)[i]);
1972 /* Collect remaining args */
1973 if (context->main_group &&
1974 !parse_remaining_arg (context, context->main_group, &i,
1975 argc, argv, error, &parsed))
1978 if (!parsed && (has_unknown || (*argv)[i][0] == '-'))
1983 if (separator_pos > 0)
1984 add_pending_null (context, &((*argv)[separator_pos]), NULL);
1988 /* Call post-parse hooks */
1989 list = context->groups;
1992 GOptionGroup *group = list->data;
1994 if (group->post_parse_func)
1996 if (!(* group->post_parse_func) (context, group,
1997 group->user_data, error))
2004 if (context->main_group && context->main_group->post_parse_func)
2006 if (!(* context->main_group->post_parse_func) (context, context->main_group,
2007 context->main_group->user_data, error))
2013 free_pending_nulls (context, TRUE);
2015 for (i = 1; i < *argc; i++)
2017 for (k = i; k < *argc; k++)
2018 if ((*argv)[k] != NULL)
2024 for (j = i + k; j < *argc; j++)
2026 (*argv)[j-k] = (*argv)[j];
2038 /* Call error hooks */
2039 list = context->groups;
2042 GOptionGroup *group = list->data;
2044 if (group->error_func)
2045 (* group->error_func) (context, group,
2046 group->user_data, error);
2051 if (context->main_group && context->main_group->error_func)
2052 (* context->main_group->error_func) (context, context->main_group,
2053 context->main_group->user_data, error);
2055 free_changes_list (context, TRUE);
2056 free_pending_nulls (context, FALSE);
2062 * g_option_group_new:
2063 * @name: the name for the option group, this is used to provide
2064 * help for the options in this group with <option>--help-</option>@name
2065 * @description: a description for this group to be shown in
2066 * <option>--help</option>. This string is translated using the translation
2067 * domain or translation function of the group
2068 * @help_description: a description for the <option>--help-</option>@name option.
2069 * This string is translated using the translation domain or translation function
2071 * @user_data: (allow-none): user data that will be passed to the pre- and post-parse hooks,
2072 * the error hook and to callbacks of %G_OPTION_ARG_CALLBACK options, or %NULL
2073 * @destroy: (allow-none): a function that will be called to free @user_data, or %NULL
2075 * Creates a new #GOptionGroup.
2077 * Return value: a newly created option group. It should be added
2078 * to a #GOptionContext or freed with g_option_group_free().
2083 g_option_group_new (const gchar *name,
2084 const gchar *description,
2085 const gchar *help_description,
2087 GDestroyNotify destroy)
2090 GOptionGroup *group;
2092 group = g_new0 (GOptionGroup, 1);
2093 group->name = g_strdup (name);
2094 group->description = g_strdup (description);
2095 group->help_description = g_strdup (help_description);
2096 group->user_data = user_data;
2097 group->destroy_notify = destroy;
2104 * g_option_group_free:
2105 * @group: a #GOptionGroup
2107 * Frees a #GOptionGroup. Note that you must <emphasis>not</emphasis>
2108 * free groups which have been added to a #GOptionContext.
2113 g_option_group_free (GOptionGroup *group)
2115 g_return_if_fail (group != NULL);
2117 g_free (group->name);
2118 g_free (group->description);
2119 g_free (group->help_description);
2121 g_free (group->entries);
2123 if (group->destroy_notify)
2124 (* group->destroy_notify) (group->user_data);
2126 if (group->translate_notify)
2127 (* group->translate_notify) (group->translate_data);
2134 * g_option_group_add_entries:
2135 * @group: a #GOptionGroup
2136 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
2138 * Adds the options specified in @entries to @group.
2143 g_option_group_add_entries (GOptionGroup *group,
2144 const GOptionEntry *entries)
2148 g_return_if_fail (entries != NULL);
2150 for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++) ;
2152 group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
2154 memcpy (group->entries + group->n_entries, entries, sizeof (GOptionEntry) * n_entries);
2156 for (i = group->n_entries; i < group->n_entries + n_entries; i++)
2158 gchar c = group->entries[i].short_name;
2160 if (c == '-' || (c != 0 && !g_ascii_isprint (c)))
2162 g_warning (G_STRLOC ": ignoring invalid short option '%c' (%d) in entry %s:%s",
2163 c, c, group->name, group->entries[i].long_name);
2164 group->entries[i].short_name = '\0';
2167 if (group->entries[i].arg != G_OPTION_ARG_NONE &&
2168 (group->entries[i].flags & G_OPTION_FLAG_REVERSE) != 0)
2170 g_warning (G_STRLOC ": ignoring reverse flag on option of arg-type %d in entry %s:%s",
2171 group->entries[i].arg, group->name, group->entries[i].long_name);
2173 group->entries[i].flags &= ~G_OPTION_FLAG_REVERSE;
2176 if (group->entries[i].arg != G_OPTION_ARG_CALLBACK &&
2177 (group->entries[i].flags & (G_OPTION_FLAG_NO_ARG|G_OPTION_FLAG_OPTIONAL_ARG|G_OPTION_FLAG_FILENAME)) != 0)
2179 g_warning (G_STRLOC ": ignoring no-arg, optional-arg or filename flags (%d) on option of arg-type %d in entry %s:%s",
2180 group->entries[i].flags, group->entries[i].arg, group->name, group->entries[i].long_name);
2182 group->entries[i].flags &= ~(G_OPTION_FLAG_NO_ARG|G_OPTION_FLAG_OPTIONAL_ARG|G_OPTION_FLAG_FILENAME);
2186 group->n_entries += n_entries;
2190 * g_option_group_set_parse_hooks:
2191 * @group: a #GOptionGroup
2192 * @pre_parse_func: (allow-none): a function to call before parsing, or %NULL
2193 * @post_parse_func: (allow-none): a function to call after parsing, or %NULL
2195 * Associates two functions with @group which will be called
2196 * from g_option_context_parse() before the first option is parsed
2197 * and after the last option has been parsed, respectively.
2199 * Note that the user data to be passed to @pre_parse_func and
2200 * @post_parse_func can be specified when constructing the group
2201 * with g_option_group_new().
2206 g_option_group_set_parse_hooks (GOptionGroup *group,
2207 GOptionParseFunc pre_parse_func,
2208 GOptionParseFunc post_parse_func)
2210 g_return_if_fail (group != NULL);
2212 group->pre_parse_func = pre_parse_func;
2213 group->post_parse_func = post_parse_func;
2217 * g_option_group_set_error_hook:
2218 * @group: a #GOptionGroup
2219 * @error_func: a function to call when an error occurs
2221 * Associates a function with @group which will be called
2222 * from g_option_context_parse() when an error occurs.
2224 * Note that the user data to be passed to @error_func can be
2225 * specified when constructing the group with g_option_group_new().
2230 g_option_group_set_error_hook (GOptionGroup *group,
2231 GOptionErrorFunc error_func)
2233 g_return_if_fail (group != NULL);
2235 group->error_func = error_func;
2240 * g_option_group_set_translate_func:
2241 * @group: a #GOptionGroup
2242 * @func: (allow-none): the #GTranslateFunc, or %NULL
2243 * @data: (allow-none): user data to pass to @func, or %NULL
2244 * @destroy_notify: (allow-none): a function which gets called to free @data, or %NULL
2246 * Sets the function which is used to translate user-visible
2247 * strings, for <option>--help</option> output. Different
2248 * groups can use different #GTranslateFunc<!-- -->s. If @func
2249 * is %NULL, strings are not translated.
2251 * If you are using gettext(), you only need to set the translation
2252 * domain, see g_option_group_set_translation_domain().
2257 g_option_group_set_translate_func (GOptionGroup *group,
2258 GTranslateFunc func,
2260 GDestroyNotify destroy_notify)
2262 g_return_if_fail (group != NULL);
2264 if (group->translate_notify)
2265 group->translate_notify (group->translate_data);
2267 group->translate_func = func;
2268 group->translate_data = data;
2269 group->translate_notify = destroy_notify;
2272 static const gchar *
2273 dgettext_swapped (const gchar *msgid,
2274 const gchar *domainname)
2276 return g_dgettext (domainname, msgid);
2280 * g_option_group_set_translation_domain:
2281 * @group: a #GOptionGroup
2282 * @domain: the domain to use
2284 * A convenience function to use gettext() for translating
2285 * user-visible strings.
2290 g_option_group_set_translation_domain (GOptionGroup *group,
2291 const gchar *domain)
2293 g_return_if_fail (group != NULL);
2295 g_option_group_set_translate_func (group,
2296 (GTranslateFunc)dgettext_swapped,
2302 * g_option_context_set_translate_func:
2303 * @context: a #GOptionContext
2304 * @func: (allow-none): the #GTranslateFunc, or %NULL
2305 * @data: (allow-none): user data to pass to @func, or %NULL
2306 * @destroy_notify: (allow-none): a function which gets called to free @data, or %NULL
2308 * Sets the function which is used to translate the contexts
2309 * user-visible strings, for <option>--help</option> output.
2310 * If @func is %NULL, strings are not translated.
2312 * Note that option groups have their own translation functions,
2313 * this function only affects the @parameter_string (see g_option_context_new()),
2314 * the summary (see g_option_context_set_summary()) and the description
2315 * (see g_option_context_set_description()).
2317 * If you are using gettext(), you only need to set the translation
2318 * domain, see g_option_context_set_translation_domain().
2323 g_option_context_set_translate_func (GOptionContext *context,
2324 GTranslateFunc func,
2326 GDestroyNotify destroy_notify)
2328 g_return_if_fail (context != NULL);
2330 if (context->translate_notify)
2331 context->translate_notify (context->translate_data);
2333 context->translate_func = func;
2334 context->translate_data = data;
2335 context->translate_notify = destroy_notify;
2339 * g_option_context_set_translation_domain:
2340 * @context: a #GOptionContext
2341 * @domain: the domain to use
2343 * A convenience function to use gettext() for translating
2344 * user-visible strings.
2349 g_option_context_set_translation_domain (GOptionContext *context,
2350 const gchar *domain)
2352 g_return_if_fail (context != NULL);
2354 g_option_context_set_translate_func (context,
2355 (GTranslateFunc)dgettext_swapped,
2361 * g_option_context_set_summary:
2362 * @context: a #GOptionContext
2363 * @summary: (allow-none): a string to be shown in <option>--help</option> output
2364 * before the list of options, or %NULL
2366 * Adds a string to be displayed in <option>--help</option> output
2367 * before the list of options. This is typically a summary of the
2368 * program functionality.
2370 * Note that the summary is translated (see
2371 * g_option_context_set_translate_func() and
2372 * g_option_context_set_translation_domain()).
2377 g_option_context_set_summary (GOptionContext *context,
2378 const gchar *summary)
2380 g_return_if_fail (context != NULL);
2382 g_free (context->summary);
2383 context->summary = g_strdup (summary);
2388 * g_option_context_get_summary:
2389 * @context: a #GOptionContext
2391 * Returns the summary. See g_option_context_set_summary().
2393 * Returns: the summary
2398 g_option_context_get_summary (GOptionContext *context)
2400 g_return_val_if_fail (context != NULL, NULL);
2402 return context->summary;
2406 * g_option_context_set_description:
2407 * @context: a #GOptionContext
2408 * @description: (allow-none): a string to be shown in <option>--help</option> output
2409 * after the list of options, or %NULL
2411 * Adds a string to be displayed in <option>--help</option> output
2412 * after the list of options. This text often includes a bug reporting
2415 * Note that the summary is translated (see
2416 * g_option_context_set_translate_func()).
2421 g_option_context_set_description (GOptionContext *context,
2422 const gchar *description)
2424 g_return_if_fail (context != NULL);
2426 g_free (context->description);
2427 context->description = g_strdup (description);
2432 * g_option_context_get_description:
2433 * @context: a #GOptionContext
2435 * Returns the description. See g_option_context_set_description().
2437 * Returns: the description
2442 g_option_context_get_description (GOptionContext *context)
2444 g_return_val_if_fail (context != NULL, NULL);
2446 return context->description;