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.
23 * @Short_description: parses commandline options
24 * @Title: Commandline option parser
26 * The GOption commandline parser is intended to be a simpler replacement for the
27 * popt library. It supports short and long commandline options, as shown in the
30 * <literal>testtreemodel -r 1 --max-size 20 --rand --display=:1.0 -vb -- file1 file2</literal>
32 * The example demonstrates a number of features of the GOption commandline parser
33 * <itemizedlist><listitem><para>
34 * Options can be single letters, prefixed by a single dash. Multiple
35 * short options can be grouped behind a single dash.
36 * </para></listitem><listitem><para>
37 * Long options are prefixed by two consecutive dashes.
38 * </para></listitem><listitem><para>
39 * Options can have an extra argument, which can be a number, a string or a
40 * filename. For long options, the extra argument can be appended with an
41 * equals sign after the option name.
42 * </para></listitem><listitem><para>
43 * Non-option arguments are returned to the application as rest arguments.
44 * </para></listitem><listitem><para>
45 * An argument consisting solely of two dashes turns off further parsing,
46 * any remaining arguments (even those starting with a dash) are returned
47 * to the application as rest arguments.
48 * </para></listitem></itemizedlist>
50 * Another important feature of GOption is that it can automatically generate
51 * nicely formatted help output. Unless it is explicitly turned off with
52 * g_option_context_set_help_enabled(), GOption will recognize the
53 * <option>--help</option>, <option>-?</option>, <option>--help-all</option>
54 * and <option>--help-</option><replaceable>groupname</replaceable> options
55 * (where <replaceable>groupname</replaceable> is the name of a #GOptionGroup)
56 * and write a text similar to the one shown in the following example to stdout.
58 * <informalexample><screen>
60 * testtreemodel [OPTION...] - test tree model performance
63 * -h, --help Show help options
64 * --help-all Show all help options
65 * --help-gtk Show GTK+ Options
67 * Application Options:
68 * -r, --repeats=N Average over N repetitions
69 * -m, --max-size=M Test up to 2^M items
70 * --display=DISPLAY X display to use
71 * -v, --verbose Be verbose
72 * -b, --beep Beep when done
73 * --rand Randomize the data
74 * </screen></informalexample>
76 * GOption groups options in #GOptionGroup<!-- -->s, which makes it easy to
77 * incorporate options from multiple sources. The intended use for this is
78 * to let applications collect option groups from the libraries it uses,
79 * add them to their #GOptionContext, and parse all options by a single call
80 * to g_option_context_parse(). See gtk_get_option_group() for an example.
82 * If an option is declared to be of type string or filename, GOption takes
83 * care of converting it to the right encoding; strings are returned in UTF-8,
84 * filenames are returned in the GLib filename encoding. Note that this only
85 * works if setlocale() has been called before g_option_context_parse().
87 * Here is a complete example of setting up GOption to parse the example
88 * commandline above and produce the example help output.
90 * <informalexample><programlisting>
91 * static gint repeats = 2;
92 * static gint max_size = 8;
93 * static gboolean verbose = FALSE;
94 * static gboolean beep = FALSE;
95 * static gboolean rand = FALSE;
97 * static GOptionEntry entries[] =
99 * { "repeats", 'r', 0, G_OPTION_ARG_INT, &repeats, "Average over N repetitions", "N" },
100 * { "max-size", 'm', 0, G_OPTION_ARG_INT, &max_size, "Test up to 2^M items", "M" },
101 * { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL },
102 * { "beep", 'b', 0, G_OPTION_ARG_NONE, &beep, "Beep when done", NULL },
103 * { "rand", 0, 0, G_OPTION_ARG_NONE, &rand, "Randomize the data", NULL },
108 * main (int argc, char *argv[])
110 * GError *error = NULL;
111 * GOptionContext *context;
113 * context = g_option_context_new ("- test tree model performance");
114 * g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
115 * g_option_context_add_group (context, gtk_get_option_group (TRUE));
116 * if (!g_option_context_parse (context, &argc, &argv, &error))
118 * g_print ("option parsing failed: %s\n", error->message);
125 * </programlisting></informalexample>
132 #include "glibintl.h"
142 #define TRANSLATE(group, str) (((group)->translate_func ? (* (group)->translate_func) ((str), (group)->translate_data) : (str)))
144 #define NO_ARG(entry) ((entry)->arg == G_OPTION_ARG_NONE || \
145 ((entry)->arg == G_OPTION_ARG_CALLBACK && \
146 ((entry)->flags & G_OPTION_FLAG_NO_ARG)))
148 #define OPTIONAL_ARG(entry) ((entry)->arg == G_OPTION_ARG_CALLBACK && \
149 (entry)->flags & G_OPTION_FLAG_OPTIONAL_ARG)
181 struct _GOptionContext
185 gchar *parameter_string;
189 GTranslateFunc translate_func;
190 GDestroyNotify translate_notify;
191 gpointer translate_data;
193 guint help_enabled : 1;
194 guint ignore_unknown : 1;
196 GOptionGroup *main_group;
198 /* We keep a list of change so we can revert them */
201 /* We also keep track of all argv elements
202 * that should be NULLed or modified.
204 GList *pending_nulls;
211 gchar *help_description;
213 GDestroyNotify destroy_notify;
216 GTranslateFunc translate_func;
217 GDestroyNotify translate_notify;
218 gpointer translate_data;
220 GOptionEntry *entries;
223 GOptionParseFunc pre_parse_func;
224 GOptionParseFunc post_parse_func;
225 GOptionErrorFunc error_func;
228 static void free_changes_list (GOptionContext *context,
230 static void free_pending_nulls (GOptionContext *context,
231 gboolean perform_nulls);
235 _g_unichar_get_width (gunichar c)
237 if (G_UNLIKELY (g_unichar_iszerowidth (c)))
240 /* we ignore the fact that we should call g_unichar_iswide_cjk() under
241 * some locales (legacy East Asian ones) */
242 if (g_unichar_iswide (c))
249 _g_utf8_strwidth (const gchar *p,
253 const gchar *start = p;
254 g_return_val_if_fail (p != NULL || max == 0, 0);
260 len += _g_unichar_get_width (g_utf8_get_char (p));
261 p = g_utf8_next_char (p);
269 /* this case may not be quite correct */
271 len += _g_unichar_get_width (g_utf8_get_char (p));
272 p = g_utf8_next_char (p);
274 while (p - start < max && *p)
276 len += _g_unichar_get_width (g_utf8_get_char (p));
277 p = g_utf8_next_char (p);
286 g_option_error_quark (void)
288 return g_quark_from_static_string ("g-option-context-error-quark");
292 * g_option_context_new:
293 * @parameter_string: a string which is displayed in
294 * the first line of <option>--help</option> output, after the
296 * <literal><replaceable>programname</replaceable> [OPTION...]</literal>
298 * Creates a new option context.
300 * The @parameter_string can serve multiple purposes. It can be used
301 * to add descriptions for "rest" arguments, which are not parsed by
302 * the #GOptionContext, typically something like "FILES" or
303 * "FILE1 FILE2...". If you are using #G_OPTION_REMAINING for
304 * collecting "rest" arguments, GLib handles this automatically by
305 * using the @arg_description of the corresponding #GOptionEntry in
308 * Another usage is to give a short summary of the program
309 * functionality, like " - frob the strings", which will be displayed
310 * in the same line as the usage. For a longer description of the
311 * program functionality that should be displayed as a paragraph
312 * below the usage line, use g_option_context_set_summary().
314 * Note that the @parameter_string is translated using the
315 * function set with g_option_context_set_translate_func(), so
316 * it should normally be passed untranslated.
318 * Returns: a newly created #GOptionContext, which must be
319 * freed with g_option_context_free() after use.
324 g_option_context_new (const gchar *parameter_string)
327 GOptionContext *context;
329 context = g_new0 (GOptionContext, 1);
331 context->parameter_string = g_strdup (parameter_string);
332 context->help_enabled = TRUE;
333 context->ignore_unknown = FALSE;
339 * g_option_context_free:
340 * @context: a #GOptionContext
342 * Frees context and all the groups which have been
345 * Please note that parsed arguments need to be freed separately (see
350 void g_option_context_free (GOptionContext *context)
352 g_return_if_fail (context != NULL);
354 g_list_foreach (context->groups, (GFunc)g_option_group_free, NULL);
355 g_list_free (context->groups);
357 if (context->main_group)
358 g_option_group_free (context->main_group);
360 free_changes_list (context, FALSE);
361 free_pending_nulls (context, FALSE);
363 g_free (context->parameter_string);
364 g_free (context->summary);
365 g_free (context->description);
367 if (context->translate_notify)
368 (* context->translate_notify) (context->translate_data);
375 * g_option_context_set_help_enabled:
376 * @context: a #GOptionContext
377 * @help_enabled: %TRUE to enable <option>--help</option>, %FALSE to disable it
379 * Enables or disables automatic generation of <option>--help</option>
380 * output. By default, g_option_context_parse() recognizes
381 * <option>--help</option>, <option>-h</option>,
382 * <option>-?</option>, <option>--help-all</option>
383 * and <option>--help-</option><replaceable>groupname</replaceable> and creates
384 * suitable output to stdout.
388 void g_option_context_set_help_enabled (GOptionContext *context,
389 gboolean help_enabled)
392 g_return_if_fail (context != NULL);
394 context->help_enabled = help_enabled;
398 * g_option_context_get_help_enabled:
399 * @context: a #GOptionContext
401 * Returns whether automatic <option>--help</option> generation
402 * is turned on for @context. See g_option_context_set_help_enabled().
404 * Returns: %TRUE if automatic help generation is turned on.
409 g_option_context_get_help_enabled (GOptionContext *context)
411 g_return_val_if_fail (context != NULL, FALSE);
413 return context->help_enabled;
417 * g_option_context_set_ignore_unknown_options:
418 * @context: a #GOptionContext
419 * @ignore_unknown: %TRUE to ignore unknown options, %FALSE to produce
420 * an error when unknown options are met
422 * Sets whether to ignore unknown options or not. If an argument is
423 * ignored, it is left in the @argv array after parsing. By default,
424 * g_option_context_parse() treats unknown options as error.
426 * This setting does not affect non-option arguments (i.e. arguments
427 * which don't start with a dash). But note that GOption cannot reliably
428 * determine whether a non-option belongs to a preceding unknown option.
433 g_option_context_set_ignore_unknown_options (GOptionContext *context,
434 gboolean ignore_unknown)
436 g_return_if_fail (context != NULL);
438 context->ignore_unknown = ignore_unknown;
442 * g_option_context_get_ignore_unknown_options:
443 * @context: a #GOptionContext
445 * Returns whether unknown options are ignored or not. See
446 * g_option_context_set_ignore_unknown_options().
448 * Returns: %TRUE if unknown options are ignored.
453 g_option_context_get_ignore_unknown_options (GOptionContext *context)
455 g_return_val_if_fail (context != NULL, FALSE);
457 return context->ignore_unknown;
461 * g_option_context_add_group:
462 * @context: a #GOptionContext
463 * @group: the group to add
465 * Adds a #GOptionGroup to the @context, so that parsing with @context
466 * will recognize the options in the group. Note that the group will
467 * be freed together with the context when g_option_context_free() is
468 * called, so you must not free the group yourself after adding it
474 g_option_context_add_group (GOptionContext *context,
479 g_return_if_fail (context != NULL);
480 g_return_if_fail (group != NULL);
481 g_return_if_fail (group->name != NULL);
482 g_return_if_fail (group->description != NULL);
483 g_return_if_fail (group->help_description != NULL);
485 for (list = context->groups; list; list = list->next)
487 GOptionGroup *g = (GOptionGroup *)list->data;
489 if ((group->name == NULL && g->name == NULL) ||
490 (group->name && g->name && strcmp (group->name, g->name) == 0))
491 g_warning ("A group named \"%s\" is already part of this GOptionContext",
495 context->groups = g_list_append (context->groups, group);
499 * g_option_context_set_main_group:
500 * @context: a #GOptionContext
501 * @group: the group to set as main group
503 * Sets a #GOptionGroup as main group of the @context.
504 * This has the same effect as calling g_option_context_add_group(),
505 * the only difference is that the options in the main group are
506 * treated differently when generating <option>--help</option> output.
511 g_option_context_set_main_group (GOptionContext *context,
514 g_return_if_fail (context != NULL);
515 g_return_if_fail (group != NULL);
517 if (context->main_group)
519 g_warning ("This GOptionContext already has a main group");
524 context->main_group = group;
528 * g_option_context_get_main_group:
529 * @context: a #GOptionContext
531 * Returns a pointer to the main group of @context.
533 * Return value: the main group of @context, or %NULL if @context doesn't
534 * have a main group. Note that group belongs to @context and should
535 * not be modified or freed.
540 g_option_context_get_main_group (GOptionContext *context)
542 g_return_val_if_fail (context != NULL, NULL);
544 return context->main_group;
548 * g_option_context_add_main_entries:
549 * @context: a #GOptionContext
550 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
551 * @translation_domain: a translation domain to use for translating
552 * the <option>--help</option> output for the options in @entries
553 * with gettext(), or %NULL
555 * A convenience function which creates a main group if it doesn't
556 * exist, adds the @entries to it and sets the translation domain.
561 g_option_context_add_main_entries (GOptionContext *context,
562 const GOptionEntry *entries,
563 const gchar *translation_domain)
565 g_return_if_fail (entries != NULL);
567 if (!context->main_group)
568 context->main_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
570 g_option_group_add_entries (context->main_group, entries);
571 g_option_group_set_translation_domain (context->main_group, translation_domain);
575 calculate_max_length (GOptionGroup *group)
578 gint i, len, max_length;
582 for (i = 0; i < group->n_entries; i++)
584 entry = &group->entries[i];
586 if (entry->flags & G_OPTION_FLAG_HIDDEN)
589 len = _g_utf8_strwidth (entry->long_name, -1);
591 if (entry->short_name)
594 if (!NO_ARG (entry) && entry->arg_description)
595 len += 1 + _g_utf8_strwidth (TRANSLATE (group, entry->arg_description), -1);
597 max_length = MAX (max_length, len);
604 print_entry (GOptionGroup *group,
606 const GOptionEntry *entry,
611 if (entry->flags & G_OPTION_FLAG_HIDDEN)
614 if (entry->long_name[0] == 0)
617 str = g_string_new (NULL);
619 if (entry->short_name)
620 g_string_append_printf (str, " -%c, --%s", entry->short_name, entry->long_name);
622 g_string_append_printf (str, " --%s", entry->long_name);
624 if (entry->arg_description)
625 g_string_append_printf (str, "=%s", TRANSLATE (group, entry->arg_description));
627 g_string_append_printf (string, "%s%*s %s\n", str->str,
628 (int) (max_length + 4 - _g_utf8_strwidth (str->str, -1)), "",
629 entry->description ? TRANSLATE (group, entry->description) : "");
630 g_string_free (str, TRUE);
634 group_has_visible_entries (GOptionContext *context,
636 gboolean main_entries)
638 GOptionFlags reject_filter = G_OPTION_FLAG_HIDDEN;
641 gboolean main_group = group == context->main_group;
644 reject_filter |= G_OPTION_FLAG_IN_MAIN;
646 for (i = 0, l = (group ? group->n_entries : 0); i < l; i++)
648 entry = &group->entries[i];
650 if (main_entries && !main_group && !(entry->flags & G_OPTION_FLAG_IN_MAIN))
652 if (!(entry->flags & reject_filter))
660 group_list_has_visible_entires (GOptionContext *context,
662 gboolean main_entries)
666 if (group_has_visible_entries (context, group_list->data, main_entries))
669 group_list = group_list->next;
676 context_has_h_entry (GOptionContext *context)
681 if (context->main_group)
683 for (i = 0; i < context->main_group->n_entries; i++)
685 if (context->main_group->entries[i].short_name == 'h')
690 for (list = context->groups; list != NULL; list = g_list_next (list))
694 group = (GOptionGroup*)list->data;
695 for (i = 0; i < group->n_entries; i++)
697 if (group->entries[i].short_name == 'h')
705 * g_option_context_get_help:
706 * @context: a #GOptionContext
707 * @main_help: if %TRUE, only include the main group
708 * @group: the #GOptionGroup to create help for, or %NULL
710 * Returns a formatted, translated help text for the given context.
711 * To obtain the text produced by <option>--help</option>, call
712 * <literal>g_option_context_get_help (context, TRUE, NULL)</literal>.
713 * To obtain the text produced by <option>--help-all</option>, call
714 * <literal>g_option_context_get_help (context, FALSE, NULL)</literal>.
715 * To obtain the help text for an option group, call
716 * <literal>g_option_context_get_help (context, FALSE, group)</literal>.
718 * Returns: A newly allocated string containing the help text
723 g_option_context_get_help (GOptionContext *context,
728 gint max_length, len;
731 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);
780 if (context->main_group)
782 for (i = 0; i < context->main_group->n_entries; i++)
784 entry = &context->main_group->entries[i];
785 g_hash_table_insert (shadow_map,
786 (gpointer)entry->long_name,
789 if (seen[(guchar)entry->short_name])
790 entry->short_name = 0;
792 seen[(guchar)entry->short_name] = TRUE;
796 list = context->groups;
799 GOptionGroup *g = list->data;
800 for (i = 0; i < g->n_entries; i++)
802 entry = &g->entries[i];
803 if (g_hash_table_lookup (shadow_map, entry->long_name) &&
804 !(entry->flags & G_OPTION_FLAG_NOALIAS))
805 entry->long_name = g_strdup_printf ("%s-%s", g->name, entry->long_name);
807 g_hash_table_insert (shadow_map, (gpointer)entry->long_name, entry);
809 if (seen[(guchar)entry->short_name] &&
810 !(entry->flags & G_OPTION_FLAG_NOALIAS))
811 entry->short_name = 0;
813 seen[(guchar)entry->short_name] = TRUE;
818 g_hash_table_destroy (shadow_map);
820 list = context->groups;
822 max_length = _g_utf8_strwidth ("-?, --help", -1);
826 len = _g_utf8_strwidth ("--help-all", -1);
827 max_length = MAX (max_length, len);
830 if (context->main_group)
832 len = calculate_max_length (context->main_group);
833 max_length = MAX (max_length, len);
838 GOptionGroup *g = list->data;
840 /* First, we check the --help-<groupname> options */
841 len = _g_utf8_strwidth ("--help-", -1) + _g_utf8_strwidth (g->name, -1);
842 max_length = MAX (max_length, len);
844 /* Then we go through the entries */
845 len = calculate_max_length (g);
846 max_length = MAX (max_length, len);
851 /* Add a bit of padding */
856 list = context->groups;
858 token = context_has_h_entry (context) ? '?' : 'h';
860 g_string_append_printf (string, "%s\n -%c, --%-*s %s\n",
861 _("Help Options:"), token, max_length - 4, "help",
862 _("Show help options"));
864 /* We only want --help-all when there are groups */
866 g_string_append_printf (string, " --%-*s %s\n",
867 max_length, "help-all",
868 _("Show all help options"));
872 GOptionGroup *g = list->data;
874 if (group_has_visible_entries (context, g, FALSE))
875 g_string_append_printf (string, " --help-%-*s %s\n",
876 max_length - 5, g->name,
877 TRANSLATE (g, g->help_description));
882 g_string_append (string, "\n");
887 /* Print a certain group */
889 if (group_has_visible_entries (context, group, FALSE))
891 g_string_append (string, TRANSLATE (group, group->description));
892 g_string_append (string, "\n");
893 for (i = 0; i < group->n_entries; i++)
894 print_entry (group, max_length, &group->entries[i], string);
895 g_string_append (string, "\n");
900 /* Print all groups */
902 list = context->groups;
906 GOptionGroup *g = list->data;
908 if (group_has_visible_entries (context, g, FALSE))
910 g_string_append (string, g->description);
911 g_string_append (string, "\n");
912 for (i = 0; i < g->n_entries; i++)
913 if (!(g->entries[i].flags & G_OPTION_FLAG_IN_MAIN))
914 print_entry (g, max_length, &g->entries[i], string);
916 g_string_append (string, "\n");
923 /* Print application options if --help or --help-all has been specified */
924 if ((main_help || !group) &&
925 (group_has_visible_entries (context, context->main_group, TRUE) ||
926 group_list_has_visible_entires (context, context->groups, TRUE)))
928 list = context->groups;
930 g_string_append (string, _("Application Options:"));
931 g_string_append (string, "\n");
932 if (context->main_group)
933 for (i = 0; i < context->main_group->n_entries; i++)
934 print_entry (context->main_group, max_length,
935 &context->main_group->entries[i], string);
939 GOptionGroup *g = list->data;
941 /* Print main entries from other groups */
942 for (i = 0; i < g->n_entries; i++)
943 if (g->entries[i].flags & G_OPTION_FLAG_IN_MAIN)
944 print_entry (g, max_length, &g->entries[i], string);
949 g_string_append (string, "\n");
952 if (context->description)
954 g_string_append (string, TRANSLATE (context, context->description));
955 g_string_append (string, "\n");
958 return g_string_free (string, FALSE);
963 print_help (GOptionContext *context,
969 help = g_option_context_get_help (context, main_help, group);
970 g_print ("%s", help);
977 parse_int (const gchar *arg_name,
986 tmp = strtol (arg, &end, 0);
988 if (*arg == '\0' || *end != '\0')
991 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
992 _("Cannot parse integer value '%s' for %s"),
998 if (*result != tmp || errno == ERANGE)
1001 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1002 _("Integer value '%s' for %s out of range"),
1012 parse_double (const gchar *arg_name,
1021 tmp = g_strtod (arg, &end);
1023 if (*arg == '\0' || *end != '\0')
1026 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1027 _("Cannot parse double value '%s' for %s"),
1031 if (errno == ERANGE)
1034 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1035 _("Double value '%s' for %s out of range"),
1047 parse_int64 (const gchar *arg_name,
1056 tmp = g_ascii_strtoll (arg, &end, 0);
1058 if (*arg == '\0' || *end != '\0')
1061 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1062 _("Cannot parse integer value '%s' for %s"),
1066 if (errno == ERANGE)
1069 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1070 _("Integer value '%s' for %s out of range"),
1082 get_change (GOptionContext *context,
1083 GOptionArg arg_type,
1087 Change *change = NULL;
1089 for (list = context->changes; list != NULL; list = list->next)
1091 change = list->data;
1093 if (change->arg_data == arg_data)
1097 change = g_new0 (Change, 1);
1098 change->arg_type = arg_type;
1099 change->arg_data = arg_data;
1101 context->changes = g_list_prepend (context->changes, change);
1109 add_pending_null (GOptionContext *context,
1115 n = g_new0 (PendingNull, 1);
1119 context->pending_nulls = g_list_prepend (context->pending_nulls, n);
1123 parse_arg (GOptionContext *context,
1124 GOptionGroup *group,
1125 GOptionEntry *entry,
1127 const gchar *option_name,
1133 g_assert (value || OPTIONAL_ARG (entry) || NO_ARG (entry));
1137 case G_OPTION_ARG_NONE:
1139 change = get_change (context, G_OPTION_ARG_NONE,
1142 *(gboolean *)entry->arg_data = !(entry->flags & G_OPTION_FLAG_REVERSE);
1145 case G_OPTION_ARG_STRING:
1149 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1154 change = get_change (context, G_OPTION_ARG_STRING,
1156 g_free (change->allocated.str);
1158 change->prev.str = *(gchar **)entry->arg_data;
1159 change->allocated.str = data;
1161 *(gchar **)entry->arg_data = data;
1164 case G_OPTION_ARG_STRING_ARRAY:
1168 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1173 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
1176 if (change->allocated.array.len == 0)
1178 change->prev.array = *(gchar ***)entry->arg_data;
1179 change->allocated.array.data = g_new (gchar *, 2);
1182 change->allocated.array.data =
1183 g_renew (gchar *, change->allocated.array.data,
1184 change->allocated.array.len + 2);
1186 change->allocated.array.data[change->allocated.array.len] = data;
1187 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
1189 change->allocated.array.len ++;
1191 *(gchar ***)entry->arg_data = change->allocated.array.data;
1196 case G_OPTION_ARG_FILENAME:
1201 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1206 data = g_strdup (value);
1208 change = get_change (context, G_OPTION_ARG_FILENAME,
1210 g_free (change->allocated.str);
1212 change->prev.str = *(gchar **)entry->arg_data;
1213 change->allocated.str = data;
1215 *(gchar **)entry->arg_data = data;
1219 case G_OPTION_ARG_FILENAME_ARRAY:
1224 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1229 data = g_strdup (value);
1231 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
1234 if (change->allocated.array.len == 0)
1236 change->prev.array = *(gchar ***)entry->arg_data;
1237 change->allocated.array.data = g_new (gchar *, 2);
1240 change->allocated.array.data =
1241 g_renew (gchar *, change->allocated.array.data,
1242 change->allocated.array.len + 2);
1244 change->allocated.array.data[change->allocated.array.len] = data;
1245 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
1247 change->allocated.array.len ++;
1249 *(gchar ***)entry->arg_data = change->allocated.array.data;
1254 case G_OPTION_ARG_INT:
1258 if (!parse_int (option_name, value,
1263 change = get_change (context, G_OPTION_ARG_INT,
1265 change->prev.integer = *(gint *)entry->arg_data;
1266 *(gint *)entry->arg_data = data;
1269 case G_OPTION_ARG_CALLBACK:
1274 if (!value && entry->flags & G_OPTION_FLAG_OPTIONAL_ARG)
1276 else if (entry->flags & G_OPTION_FLAG_NO_ARG)
1278 else if (entry->flags & G_OPTION_FLAG_FILENAME)
1281 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1283 data = g_strdup (value);
1287 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1289 if (!(entry->flags & (G_OPTION_FLAG_NO_ARG|G_OPTION_FLAG_OPTIONAL_ARG)) &&
1293 retval = (* (GOptionArgFunc) entry->arg_data) (option_name, data, group->user_data, error);
1295 if (!retval && error != NULL && *error == NULL)
1297 G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
1298 _("Error parsing option %s"), option_name);
1306 case G_OPTION_ARG_DOUBLE:
1310 if (!parse_double (option_name, value,
1317 change = get_change (context, G_OPTION_ARG_DOUBLE,
1319 change->prev.dbl = *(gdouble *)entry->arg_data;
1320 *(gdouble *)entry->arg_data = data;
1323 case G_OPTION_ARG_INT64:
1327 if (!parse_int64 (option_name, value,
1334 change = get_change (context, G_OPTION_ARG_INT64,
1336 change->prev.int64 = *(gint64 *)entry->arg_data;
1337 *(gint64 *)entry->arg_data = data;
1341 g_assert_not_reached ();
1348 parse_short_option (GOptionContext *context,
1349 GOptionGroup *group,
1360 for (j = 0; j < group->n_entries; j++)
1362 if (arg == group->entries[j].short_name)
1365 gchar *value = NULL;
1367 option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
1369 if (NO_ARG (&group->entries[j]))
1376 G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
1377 _("Error parsing option %s"), option_name);
1378 g_free (option_name);
1382 if (idx < *argc - 1)
1384 if (!OPTIONAL_ARG (&group->entries[j]))
1386 value = (*argv)[idx + 1];
1387 add_pending_null (context, &((*argv)[idx + 1]), NULL);
1392 if ((*argv)[idx + 1][0] == '-')
1396 value = (*argv)[idx + 1];
1397 add_pending_null (context, &((*argv)[idx + 1]), NULL);
1402 else if (idx >= *argc - 1 && OPTIONAL_ARG (&group->entries[j]))
1407 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1408 _("Missing argument for %s"), option_name);
1409 g_free (option_name);
1414 if (!parse_arg (context, group, &group->entries[j],
1415 value, option_name, error))
1417 g_free (option_name);
1421 g_free (option_name);
1430 parse_long_option (GOptionContext *context,
1431 GOptionGroup *group,
1442 for (j = 0; j < group->n_entries; j++)
1447 if (aliased && (group->entries[j].flags & G_OPTION_FLAG_NOALIAS))
1450 if (NO_ARG (&group->entries[j]) &&
1451 strcmp (arg, group->entries[j].long_name) == 0)
1456 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1457 retval = parse_arg (context, group, &group->entries[j],
1458 NULL, option_name, error);
1459 g_free(option_name);
1461 add_pending_null (context, &((*argv)[*idx]), NULL);
1468 gint len = strlen (group->entries[j].long_name);
1470 if (strncmp (arg, group->entries[j].long_name, len) == 0 &&
1471 (arg[len] == '=' || arg[len] == 0))
1473 gchar *value = NULL;
1476 add_pending_null (context, &((*argv)[*idx]), NULL);
1477 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1479 if (arg[len] == '=')
1480 value = arg + len + 1;
1481 else if (*idx < *argc - 1)
1483 if (!(group->entries[j].flags & G_OPTION_FLAG_OPTIONAL_ARG))
1485 value = (*argv)[*idx + 1];
1486 add_pending_null (context, &((*argv)[*idx + 1]), NULL);
1491 if ((*argv)[*idx + 1][0] == '-')
1494 retval = parse_arg (context, group, &group->entries[j],
1495 NULL, option_name, error);
1497 g_free (option_name);
1502 value = (*argv)[*idx + 1];
1503 add_pending_null (context, &((*argv)[*idx + 1]), NULL);
1508 else if (*idx >= *argc - 1 &&
1509 group->entries[j].flags & G_OPTION_FLAG_OPTIONAL_ARG)
1512 retval = parse_arg (context, group, &group->entries[j],
1513 NULL, option_name, error);
1515 g_free (option_name);
1521 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1522 _("Missing argument for %s"), option_name);
1523 g_free (option_name);
1527 if (!parse_arg (context, group, &group->entries[j],
1528 value, option_name, error))
1530 g_free (option_name);
1534 g_free (option_name);
1544 parse_remaining_arg (GOptionContext *context,
1545 GOptionGroup *group,
1554 for (j = 0; j < group->n_entries; j++)
1559 if (group->entries[j].long_name[0])
1562 g_return_val_if_fail (group->entries[j].arg == G_OPTION_ARG_CALLBACK ||
1563 group->entries[j].arg == G_OPTION_ARG_STRING_ARRAY ||
1564 group->entries[j].arg == G_OPTION_ARG_FILENAME_ARRAY, FALSE);
1566 add_pending_null (context, &((*argv)[*idx]), NULL);
1568 if (!parse_arg (context, group, &group->entries[j], (*argv)[*idx], "", error))
1579 free_changes_list (GOptionContext *context,
1584 for (list = context->changes; list != NULL; list = list->next)
1586 Change *change = list->data;
1590 switch (change->arg_type)
1592 case G_OPTION_ARG_NONE:
1593 *(gboolean *)change->arg_data = change->prev.bool;
1595 case G_OPTION_ARG_INT:
1596 *(gint *)change->arg_data = change->prev.integer;
1598 case G_OPTION_ARG_STRING:
1599 case G_OPTION_ARG_FILENAME:
1600 g_free (change->allocated.str);
1601 *(gchar **)change->arg_data = change->prev.str;
1603 case G_OPTION_ARG_STRING_ARRAY:
1604 case G_OPTION_ARG_FILENAME_ARRAY:
1605 g_strfreev (change->allocated.array.data);
1606 *(gchar ***)change->arg_data = change->prev.array;
1608 case G_OPTION_ARG_DOUBLE:
1609 *(gdouble *)change->arg_data = change->prev.dbl;
1611 case G_OPTION_ARG_INT64:
1612 *(gint64 *)change->arg_data = change->prev.int64;
1615 g_assert_not_reached ();
1622 g_list_free (context->changes);
1623 context->changes = NULL;
1627 free_pending_nulls (GOptionContext *context,
1628 gboolean perform_nulls)
1632 for (list = context->pending_nulls; list != NULL; list = list->next)
1634 PendingNull *n = list->data;
1640 /* Copy back the short options */
1642 strcpy (*n->ptr + 1, n->value);
1652 g_list_free (context->pending_nulls);
1653 context->pending_nulls = NULL;
1657 * g_option_context_parse:
1658 * @context: a #GOptionContext
1659 * @argc: a pointer to the number of command line arguments
1660 * @argv: a pointer to the array of command line arguments
1661 * @error: a return location for errors
1663 * Parses the command line arguments, recognizing options
1664 * which have been added to @context. A side-effect of
1665 * calling this function is that g_set_prgname() will be
1668 * If the parsing is successful, any parsed arguments are
1669 * removed from the array and @argc and @argv are updated
1670 * accordingly. A '--' option is stripped from @argv
1671 * unless there are unparsed options before and after it,
1672 * or some of the options after it start with '-'. In case
1673 * of an error, @argc and @argv are left unmodified.
1675 * If automatic <option>--help</option> support is enabled
1676 * (see g_option_context_set_help_enabled()), and the
1677 * @argv array contains one of the recognized help options,
1678 * this function will produce help output to stdout and
1679 * call <literal>exit (0)</literal>.
1681 * Note that function depends on the
1682 * <link linkend="setlocale">current locale</link> for
1683 * automatic character set conversion of string and filename
1686 * Return value: %TRUE if the parsing was successful,
1687 * %FALSE if an error occurred
1692 g_option_context_parse (GOptionContext *context,
1700 /* Set program name */
1701 if (!g_get_prgname())
1703 if (argc && argv && *argc)
1707 prgname = g_path_get_basename ((*argv)[0]);
1708 g_set_prgname (prgname);
1712 g_set_prgname ("<unknown>");
1715 /* Call pre-parse hooks */
1716 list = context->groups;
1719 GOptionGroup *group = list->data;
1721 if (group->pre_parse_func)
1723 if (!(* group->pre_parse_func) (context, group,
1724 group->user_data, error))
1731 if (context->main_group && context->main_group->pre_parse_func)
1733 if (!(* context->main_group->pre_parse_func) (context, context->main_group,
1734 context->main_group->user_data, error))
1740 gboolean stop_parsing = FALSE;
1741 gboolean has_unknown = FALSE;
1742 gint separator_pos = 0;
1744 for (i = 1; i < *argc; i++)
1747 gboolean parsed = FALSE;
1749 if ((*argv)[i][0] == '-' && (*argv)[i][1] != '\0' && !stop_parsing)
1751 if ((*argv)[i][1] == '-')
1755 arg = (*argv)[i] + 2;
1757 /* '--' terminates list of arguments */
1761 stop_parsing = TRUE;
1765 /* Handle help options */
1766 if (context->help_enabled)
1768 if (strcmp (arg, "help") == 0)
1769 print_help (context, TRUE, NULL);
1770 else if (strcmp (arg, "help-all") == 0)
1771 print_help (context, FALSE, NULL);
1772 else if (strncmp (arg, "help-", 5) == 0)
1774 list = context->groups;
1778 GOptionGroup *group = list->data;
1780 if (strcmp (arg + 5, group->name) == 0)
1781 print_help (context, FALSE, group);
1788 if (context->main_group &&
1789 !parse_long_option (context, context->main_group, &i, arg,
1790 FALSE, argc, argv, error, &parsed))
1796 /* Try the groups */
1797 list = context->groups;
1800 GOptionGroup *group = list->data;
1802 if (!parse_long_option (context, group, &i, arg,
1803 FALSE, argc, argv, error, &parsed))
1815 /* Now look for --<group>-<option> */
1816 dash = strchr (arg, '-');
1819 /* Try the groups */
1820 list = context->groups;
1823 GOptionGroup *group = list->data;
1825 if (strncmp (group->name, arg, dash - arg) == 0)
1827 if (!parse_long_option (context, group, &i, dash + 1,
1828 TRUE, argc, argv, error, &parsed))
1839 if (context->ignore_unknown)
1843 { /* short option */
1844 gint new_i = i, arg_length;
1845 gboolean *nulled_out = NULL;
1846 gboolean has_h_entry = context_has_h_entry (context);
1847 arg = (*argv)[i] + 1;
1848 arg_length = strlen (arg);
1849 nulled_out = g_newa (gboolean, arg_length);
1850 memset (nulled_out, 0, arg_length * sizeof (gboolean));
1851 for (j = 0; j < arg_length; j++)
1853 if (context->help_enabled && (arg[j] == '?' ||
1854 (arg[j] == 'h' && !has_h_entry)))
1855 print_help (context, TRUE, NULL);
1857 if (context->main_group &&
1858 !parse_short_option (context, context->main_group,
1860 argc, argv, error, &parsed))
1864 /* Try the groups */
1865 list = context->groups;
1868 GOptionGroup *group = list->data;
1869 if (!parse_short_option (context, group, i, &new_i, arg[j],
1870 argc, argv, error, &parsed))
1878 if (context->ignore_unknown && parsed)
1879 nulled_out[j] = TRUE;
1880 else if (context->ignore_unknown)
1884 /* !context->ignore_unknown && parsed */
1886 if (context->ignore_unknown)
1888 gchar *new_arg = NULL;
1890 for (j = 0; j < arg_length; j++)
1895 new_arg = g_malloc (arg_length + 1);
1896 new_arg[arg_index++] = arg[j];
1900 new_arg[arg_index] = '\0';
1901 add_pending_null (context, &((*argv)[i]), new_arg);
1905 add_pending_null (context, &((*argv)[i]), NULL);
1913 if (!parsed && !context->ignore_unknown)
1916 G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
1917 _("Unknown option %s"), (*argv)[i]);
1923 /* Collect remaining args */
1924 if (context->main_group &&
1925 !parse_remaining_arg (context, context->main_group, &i,
1926 argc, argv, error, &parsed))
1929 if (!parsed && (has_unknown || (*argv)[i][0] == '-'))
1934 if (separator_pos > 0)
1935 add_pending_null (context, &((*argv)[separator_pos]), NULL);
1939 /* Call post-parse hooks */
1940 list = context->groups;
1943 GOptionGroup *group = list->data;
1945 if (group->post_parse_func)
1947 if (!(* group->post_parse_func) (context, group,
1948 group->user_data, error))
1955 if (context->main_group && context->main_group->post_parse_func)
1957 if (!(* context->main_group->post_parse_func) (context, context->main_group,
1958 context->main_group->user_data, error))
1964 free_pending_nulls (context, TRUE);
1966 for (i = 1; i < *argc; i++)
1968 for (k = i; k < *argc; k++)
1969 if ((*argv)[k] != NULL)
1975 for (j = i + k; j < *argc; j++)
1977 (*argv)[j-k] = (*argv)[j];
1989 /* Call error hooks */
1990 list = context->groups;
1993 GOptionGroup *group = list->data;
1995 if (group->error_func)
1996 (* group->error_func) (context, group,
1997 group->user_data, error);
2002 if (context->main_group && context->main_group->error_func)
2003 (* context->main_group->error_func) (context, context->main_group,
2004 context->main_group->user_data, error);
2006 free_changes_list (context, TRUE);
2007 free_pending_nulls (context, FALSE);
2013 * g_option_group_new:
2014 * @name: the name for the option group, this is used to provide
2015 * help for the options in this group with <option>--help-</option>@name
2016 * @description: a description for this group to be shown in
2017 * <option>--help</option>. This string is translated using the translation
2018 * domain or translation function of the group
2019 * @help_description: a description for the <option>--help-</option>@name option.
2020 * This string is translated using the translation domain or translation function
2022 * @user_data: user data that will be passed to the pre- and post-parse hooks,
2023 * the error hook and to callbacks of %G_OPTION_ARG_CALLBACK options, or %NULL
2024 * @destroy: a function that will be called to free @user_data, or %NULL
2026 * Creates a new #GOptionGroup.
2028 * Return value: a newly created option group. It should be added
2029 * to a #GOptionContext or freed with g_option_group_free().
2034 g_option_group_new (const gchar *name,
2035 const gchar *description,
2036 const gchar *help_description,
2038 GDestroyNotify destroy)
2041 GOptionGroup *group;
2043 group = g_new0 (GOptionGroup, 1);
2044 group->name = g_strdup (name);
2045 group->description = g_strdup (description);
2046 group->help_description = g_strdup (help_description);
2047 group->user_data = user_data;
2048 group->destroy_notify = destroy;
2055 * g_option_group_free:
2056 * @group: a #GOptionGroup
2058 * Frees a #GOptionGroup. Note that you must <emphasis>not</emphasis>
2059 * free groups which have been added to a #GOptionContext.
2064 g_option_group_free (GOptionGroup *group)
2066 g_return_if_fail (group != NULL);
2068 g_free (group->name);
2069 g_free (group->description);
2070 g_free (group->help_description);
2072 g_free (group->entries);
2074 if (group->destroy_notify)
2075 (* group->destroy_notify) (group->user_data);
2077 if (group->translate_notify)
2078 (* group->translate_notify) (group->translate_data);
2085 * g_option_group_add_entries:
2086 * @group: a #GOptionGroup
2087 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
2089 * Adds the options specified in @entries to @group.
2094 g_option_group_add_entries (GOptionGroup *group,
2095 const GOptionEntry *entries)
2099 g_return_if_fail (entries != NULL);
2101 for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++) ;
2103 group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
2105 memcpy (group->entries + group->n_entries, entries, sizeof (GOptionEntry) * n_entries);
2107 for (i = group->n_entries; i < group->n_entries + n_entries; i++)
2109 gchar c = group->entries[i].short_name;
2113 if (c == '-' || !g_ascii_isprint (c))
2115 g_warning (G_STRLOC": ignoring invalid short option '%c' (%d)", c, c);
2116 group->entries[i].short_name = 0;
2121 group->n_entries += n_entries;
2125 * g_option_group_set_parse_hooks:
2126 * @group: a #GOptionGroup
2127 * @pre_parse_func: a function to call before parsing, or %NULL
2128 * @post_parse_func: a function to call after parsing, or %NULL
2130 * Associates two functions with @group which will be called
2131 * from g_option_context_parse() before the first option is parsed
2132 * and after the last option has been parsed, respectively.
2134 * Note that the user data to be passed to @pre_parse_func and
2135 * @post_parse_func can be specified when constructing the group
2136 * with g_option_group_new().
2141 g_option_group_set_parse_hooks (GOptionGroup *group,
2142 GOptionParseFunc pre_parse_func,
2143 GOptionParseFunc post_parse_func)
2145 g_return_if_fail (group != NULL);
2147 group->pre_parse_func = pre_parse_func;
2148 group->post_parse_func = post_parse_func;
2152 * g_option_group_set_error_hook:
2153 * @group: a #GOptionGroup
2154 * @error_func: a function to call when an error occurs
2156 * Associates a function with @group which will be called
2157 * from g_option_context_parse() when an error occurs.
2159 * Note that the user data to be passed to @error_func can be
2160 * specified when constructing the group with g_option_group_new().
2165 g_option_group_set_error_hook (GOptionGroup *group,
2166 GOptionErrorFunc error_func)
2168 g_return_if_fail (group != NULL);
2170 group->error_func = error_func;
2175 * g_option_group_set_translate_func:
2176 * @group: a #GOptionGroup
2177 * @func: the #GTranslateFunc, or %NULL
2178 * @data: user data to pass to @func, or %NULL
2179 * @destroy_notify: a function which gets called to free @data, or %NULL
2181 * Sets the function which is used to translate user-visible
2182 * strings, for <option>--help</option> output. Different
2183 * groups can use different #GTranslateFunc<!-- -->s. If @func
2184 * is %NULL, strings are not translated.
2186 * If you are using gettext(), you only need to set the translation
2187 * domain, see g_option_group_set_translation_domain().
2192 g_option_group_set_translate_func (GOptionGroup *group,
2193 GTranslateFunc func,
2195 GDestroyNotify destroy_notify)
2197 g_return_if_fail (group != NULL);
2199 if (group->translate_notify)
2200 group->translate_notify (group->translate_data);
2202 group->translate_func = func;
2203 group->translate_data = data;
2204 group->translate_notify = destroy_notify;
2207 static const gchar *
2208 dgettext_swapped (const gchar *msgid,
2209 const gchar *domainname)
2211 return g_dgettext (domainname, msgid);
2215 * g_option_group_set_translation_domain:
2216 * @group: a #GOptionGroup
2217 * @domain: the domain to use
2219 * A convenience function to use gettext() for translating
2220 * user-visible strings.
2225 g_option_group_set_translation_domain (GOptionGroup *group,
2226 const gchar *domain)
2228 g_return_if_fail (group != NULL);
2230 g_option_group_set_translate_func (group,
2231 (GTranslateFunc)dgettext_swapped,
2237 * g_option_context_set_translate_func:
2238 * @context: a #GOptionContext
2239 * @func: the #GTranslateFunc, or %NULL
2240 * @data: user data to pass to @func, or %NULL
2241 * @destroy_notify: a function which gets called to free @data, or %NULL
2243 * Sets the function which is used to translate the contexts
2244 * user-visible strings, for <option>--help</option> output.
2245 * If @func is %NULL, strings are not translated.
2247 * Note that option groups have their own translation functions,
2248 * this function only affects the @parameter_string (see g_option_context_new()),
2249 * the summary (see g_option_context_set_summary()) and the description
2250 * (see g_option_context_set_description()).
2252 * If you are using gettext(), you only need to set the translation
2253 * domain, see g_option_context_set_translation_domain().
2258 g_option_context_set_translate_func (GOptionContext *context,
2259 GTranslateFunc func,
2261 GDestroyNotify destroy_notify)
2263 g_return_if_fail (context != NULL);
2265 if (context->translate_notify)
2266 context->translate_notify (context->translate_data);
2268 context->translate_func = func;
2269 context->translate_data = data;
2270 context->translate_notify = destroy_notify;
2274 * g_option_context_set_translation_domain:
2275 * @context: a #GOptionContext
2276 * @domain: the domain to use
2278 * A convenience function to use gettext() for translating
2279 * user-visible strings.
2284 g_option_context_set_translation_domain (GOptionContext *context,
2285 const gchar *domain)
2287 g_return_if_fail (context != NULL);
2289 g_option_context_set_translate_func (context,
2290 (GTranslateFunc)dgettext_swapped,
2296 * g_option_context_set_summary:
2297 * @context: a #GOptionContext
2298 * @summary: a string to be shown in <option>--help</option> output
2299 * before the list of options, or %NULL
2301 * Adds a string to be displayed in <option>--help</option> output
2302 * before the list of options. This is typically a summary of the
2303 * program functionality.
2305 * Note that the summary is translated (see
2306 * g_option_context_set_translate_func() and
2307 * g_option_context_set_translation_domain()).
2312 g_option_context_set_summary (GOptionContext *context,
2313 const gchar *summary)
2315 g_return_if_fail (context != NULL);
2317 g_free (context->summary);
2318 context->summary = g_strdup (summary);
2323 * g_option_context_get_summary:
2324 * @context: a #GOptionContext
2326 * Returns the summary. See g_option_context_set_summary().
2328 * Returns: the summary
2332 G_CONST_RETURN gchar *
2333 g_option_context_get_summary (GOptionContext *context)
2335 g_return_val_if_fail (context != NULL, NULL);
2337 return context->summary;
2341 * g_option_context_set_description:
2342 * @context: a #GOptionContext
2343 * @description: a string to be shown in <option>--help</option> output
2344 * after the list of options, or %NULL
2346 * Adds a string to be displayed in <option>--help</option> output
2347 * after the list of options. This text often includes a bug reporting
2350 * Note that the summary is translated (see
2351 * g_option_context_set_translate_func()).
2356 g_option_context_set_description (GOptionContext *context,
2357 const gchar *description)
2359 g_return_if_fail (context != NULL);
2361 g_free (context->description);
2362 context->description = g_strdup (description);
2367 * g_option_context_get_description:
2368 * @context: a #GOptionContext
2370 * Returns the description. See g_option_context_set_description().
2372 * Returns: the description
2376 G_CONST_RETURN gchar *
2377 g_option_context_get_description (GOptionContext *context)
2379 g_return_val_if_fail (context != NULL, NULL);
2381 return context->description;
2385 #define __G_OPTION_C__
2386 #include "galiasdef.c"