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"
141 #define TRANSLATE(group, str) (((group)->translate_func ? (* (group)->translate_func) ((str), (group)->translate_data) : (str)))
143 #define NO_ARG(entry) ((entry)->arg == G_OPTION_ARG_NONE || \
144 ((entry)->arg == G_OPTION_ARG_CALLBACK && \
145 ((entry)->flags & G_OPTION_FLAG_NO_ARG)))
147 #define OPTIONAL_ARG(entry) ((entry)->arg == G_OPTION_ARG_CALLBACK && \
148 (entry)->flags & G_OPTION_FLAG_OPTIONAL_ARG)
180 struct _GOptionContext
184 gchar *parameter_string;
188 GTranslateFunc translate_func;
189 GDestroyNotify translate_notify;
190 gpointer translate_data;
192 guint help_enabled : 1;
193 guint ignore_unknown : 1;
195 GOptionGroup *main_group;
197 /* We keep a list of change so we can revert them */
200 /* We also keep track of all argv elements
201 * that should be NULLed or modified.
203 GList *pending_nulls;
210 gchar *help_description;
212 GDestroyNotify destroy_notify;
215 GTranslateFunc translate_func;
216 GDestroyNotify translate_notify;
217 gpointer translate_data;
219 GOptionEntry *entries;
222 GOptionParseFunc pre_parse_func;
223 GOptionParseFunc post_parse_func;
224 GOptionErrorFunc error_func;
227 static void free_changes_list (GOptionContext *context,
229 static void free_pending_nulls (GOptionContext *context,
230 gboolean perform_nulls);
234 _g_unichar_get_width (gunichar c)
236 if (G_UNLIKELY (g_unichar_iszerowidth (c)))
239 /* we ignore the fact that we should call g_unichar_iswide_cjk() under
240 * some locales (legacy East Asian ones) */
241 if (g_unichar_iswide (c))
248 _g_utf8_strwidth (const gchar *p,
252 const gchar *start = p;
253 g_return_val_if_fail (p != NULL || max == 0, 0);
259 len += _g_unichar_get_width (g_utf8_get_char (p));
260 p = g_utf8_next_char (p);
268 /* this case may not be quite correct */
270 len += _g_unichar_get_width (g_utf8_get_char (p));
271 p = g_utf8_next_char (p);
273 while (p - start < max && *p)
275 len += _g_unichar_get_width (g_utf8_get_char (p));
276 p = g_utf8_next_char (p);
285 g_option_error_quark (void)
287 return g_quark_from_static_string ("g-option-context-error-quark");
291 * g_option_context_new:
292 * @parameter_string: a string which is displayed in
293 * the first line of <option>--help</option> output, after the
295 * <literal><replaceable>programname</replaceable> [OPTION...]</literal>
297 * Creates a new option context.
299 * The @parameter_string can serve multiple purposes. It can be used
300 * to add descriptions for "rest" arguments, which are not parsed by
301 * the #GOptionContext, typically something like "FILES" or
302 * "FILE1 FILE2...". If you are using #G_OPTION_REMAINING for
303 * collecting "rest" arguments, GLib handles this automatically by
304 * using the @arg_description of the corresponding #GOptionEntry in
307 * Another usage is to give a short summary of the program
308 * functionality, like " - frob the strings", which will be displayed
309 * in the same line as the usage. For a longer description of the
310 * program functionality that should be displayed as a paragraph
311 * below the usage line, use g_option_context_set_summary().
313 * Note that the @parameter_string is translated using the
314 * function set with g_option_context_set_translate_func(), so
315 * it should normally be passed untranslated.
317 * Returns: a newly created #GOptionContext, which must be
318 * freed with g_option_context_free() after use.
323 g_option_context_new (const gchar *parameter_string)
326 GOptionContext *context;
328 context = g_new0 (GOptionContext, 1);
330 context->parameter_string = g_strdup (parameter_string);
331 context->help_enabled = TRUE;
332 context->ignore_unknown = FALSE;
338 * g_option_context_free:
339 * @context: a #GOptionContext
341 * Frees context and all the groups which have been
344 * Please note that parsed arguments need to be freed separately (see
349 void g_option_context_free (GOptionContext *context)
351 g_return_if_fail (context != NULL);
353 g_list_foreach (context->groups, (GFunc)g_option_group_free, NULL);
354 g_list_free (context->groups);
356 if (context->main_group)
357 g_option_group_free (context->main_group);
359 free_changes_list (context, FALSE);
360 free_pending_nulls (context, FALSE);
362 g_free (context->parameter_string);
363 g_free (context->summary);
364 g_free (context->description);
366 if (context->translate_notify)
367 (* context->translate_notify) (context->translate_data);
374 * g_option_context_set_help_enabled:
375 * @context: a #GOptionContext
376 * @help_enabled: %TRUE to enable <option>--help</option>, %FALSE to disable it
378 * Enables or disables automatic generation of <option>--help</option>
379 * output. By default, g_option_context_parse() recognizes
380 * <option>--help</option>, <option>-h</option>,
381 * <option>-?</option>, <option>--help-all</option>
382 * and <option>--help-</option><replaceable>groupname</replaceable> and creates
383 * suitable output to stdout.
387 void g_option_context_set_help_enabled (GOptionContext *context,
388 gboolean help_enabled)
391 g_return_if_fail (context != NULL);
393 context->help_enabled = help_enabled;
397 * g_option_context_get_help_enabled:
398 * @context: a #GOptionContext
400 * Returns whether automatic <option>--help</option> generation
401 * is turned on for @context. See g_option_context_set_help_enabled().
403 * Returns: %TRUE if automatic help generation is turned on.
408 g_option_context_get_help_enabled (GOptionContext *context)
410 g_return_val_if_fail (context != NULL, FALSE);
412 return context->help_enabled;
416 * g_option_context_set_ignore_unknown_options:
417 * @context: a #GOptionContext
418 * @ignore_unknown: %TRUE to ignore unknown options, %FALSE to produce
419 * an error when unknown options are met
421 * Sets whether to ignore unknown options or not. If an argument is
422 * ignored, it is left in the @argv array after parsing. By default,
423 * g_option_context_parse() treats unknown options as error.
425 * This setting does not affect non-option arguments (i.e. arguments
426 * which don't start with a dash). But note that GOption cannot reliably
427 * determine whether a non-option belongs to a preceding unknown option.
432 g_option_context_set_ignore_unknown_options (GOptionContext *context,
433 gboolean ignore_unknown)
435 g_return_if_fail (context != NULL);
437 context->ignore_unknown = ignore_unknown;
441 * g_option_context_get_ignore_unknown_options:
442 * @context: a #GOptionContext
444 * Returns whether unknown options are ignored or not. See
445 * g_option_context_set_ignore_unknown_options().
447 * Returns: %TRUE if unknown options are ignored.
452 g_option_context_get_ignore_unknown_options (GOptionContext *context)
454 g_return_val_if_fail (context != NULL, FALSE);
456 return context->ignore_unknown;
460 * g_option_context_add_group:
461 * @context: a #GOptionContext
462 * @group: the group to add
464 * Adds a #GOptionGroup to the @context, so that parsing with @context
465 * will recognize the options in the group. Note that the group will
466 * be freed together with the context when g_option_context_free() is
467 * called, so you must not free the group yourself after adding it
473 g_option_context_add_group (GOptionContext *context,
478 g_return_if_fail (context != NULL);
479 g_return_if_fail (group != NULL);
480 g_return_if_fail (group->name != NULL);
481 g_return_if_fail (group->description != NULL);
482 g_return_if_fail (group->help_description != NULL);
484 for (list = context->groups; list; list = list->next)
486 GOptionGroup *g = (GOptionGroup *)list->data;
488 if ((group->name == NULL && g->name == NULL) ||
489 (group->name && g->name && strcmp (group->name, g->name) == 0))
490 g_warning ("A group named \"%s\" is already part of this GOptionContext",
494 context->groups = g_list_append (context->groups, group);
498 * g_option_context_set_main_group:
499 * @context: a #GOptionContext
500 * @group: the group to set as main group
502 * Sets a #GOptionGroup as main group of the @context.
503 * This has the same effect as calling g_option_context_add_group(),
504 * the only difference is that the options in the main group are
505 * treated differently when generating <option>--help</option> output.
510 g_option_context_set_main_group (GOptionContext *context,
513 g_return_if_fail (context != NULL);
514 g_return_if_fail (group != NULL);
516 if (context->main_group)
518 g_warning ("This GOptionContext already has a main group");
523 context->main_group = group;
527 * g_option_context_get_main_group:
528 * @context: a #GOptionContext
530 * Returns a pointer to the main group of @context.
532 * Return value: the main group of @context, or %NULL if @context doesn't
533 * have a main group. Note that group belongs to @context and should
534 * not be modified or freed.
539 g_option_context_get_main_group (GOptionContext *context)
541 g_return_val_if_fail (context != NULL, NULL);
543 return context->main_group;
547 * g_option_context_add_main_entries:
548 * @context: a #GOptionContext
549 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
550 * @translation_domain: a translation domain to use for translating
551 * the <option>--help</option> output for the options in @entries
552 * with gettext(), or %NULL
554 * A convenience function which creates a main group if it doesn't
555 * exist, adds the @entries to it and sets the translation domain.
560 g_option_context_add_main_entries (GOptionContext *context,
561 const GOptionEntry *entries,
562 const gchar *translation_domain)
564 g_return_if_fail (entries != NULL);
566 if (!context->main_group)
567 context->main_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
569 g_option_group_add_entries (context->main_group, entries);
570 g_option_group_set_translation_domain (context->main_group, translation_domain);
574 calculate_max_length (GOptionGroup *group)
577 gint i, len, max_length;
581 for (i = 0; i < group->n_entries; i++)
583 entry = &group->entries[i];
585 if (entry->flags & G_OPTION_FLAG_HIDDEN)
588 len = _g_utf8_strwidth (entry->long_name, -1);
590 if (entry->short_name)
593 if (!NO_ARG (entry) && entry->arg_description)
594 len += 1 + _g_utf8_strwidth (TRANSLATE (group, entry->arg_description), -1);
596 max_length = MAX (max_length, len);
603 print_entry (GOptionGroup *group,
605 const GOptionEntry *entry,
610 if (entry->flags & G_OPTION_FLAG_HIDDEN)
613 if (entry->long_name[0] == 0)
616 str = g_string_new (NULL);
618 if (entry->short_name)
619 g_string_append_printf (str, " -%c, --%s", entry->short_name, entry->long_name);
621 g_string_append_printf (str, " --%s", entry->long_name);
623 if (entry->arg_description)
624 g_string_append_printf (str, "=%s", TRANSLATE (group, entry->arg_description));
626 g_string_append_printf (string, "%s%*s %s\n", str->str,
627 (int) (max_length + 4 - _g_utf8_strwidth (str->str, -1)), "",
628 entry->description ? TRANSLATE (group, entry->description) : "");
629 g_string_free (str, TRUE);
633 group_has_visible_entries (GOptionContext *context,
635 gboolean main_entries)
637 GOptionFlags reject_filter = G_OPTION_FLAG_HIDDEN;
640 gboolean main_group = group == context->main_group;
643 reject_filter |= G_OPTION_FLAG_IN_MAIN;
645 for (i = 0, l = (group ? group->n_entries : 0); i < l; i++)
647 entry = &group->entries[i];
649 if (main_entries && !main_group && !(entry->flags & G_OPTION_FLAG_IN_MAIN))
651 if (!(entry->flags & reject_filter))
659 group_list_has_visible_entires (GOptionContext *context,
661 gboolean main_entries)
665 if (group_has_visible_entries (context, group_list->data, main_entries))
668 group_list = group_list->next;
675 context_has_h_entry (GOptionContext *context)
680 if (context->main_group)
682 for (i = 0; i < context->main_group->n_entries; i++)
684 if (context->main_group->entries[i].short_name == 'h')
689 for (list = context->groups; list != NULL; list = g_list_next (list))
693 group = (GOptionGroup*)list->data;
694 for (i = 0; i < group->n_entries; i++)
696 if (group->entries[i].short_name == 'h')
704 * g_option_context_get_help:
705 * @context: a #GOptionContext
706 * @main_help: if %TRUE, only include the main group
707 * @group: the #GOptionGroup to create help for, or %NULL
709 * Returns a formatted, translated help text for the given context.
710 * To obtain the text produced by <option>--help</option>, call
711 * <literal>g_option_context_get_help (context, TRUE, NULL)</literal>.
712 * To obtain the text produced by <option>--help-all</option>, call
713 * <literal>g_option_context_get_help (context, FALSE, NULL)</literal>.
714 * To obtain the help text for an option group, call
715 * <literal>g_option_context_get_help (context, FALSE, group)</literal>.
717 * Returns: A newly allocated string containing the help text
722 g_option_context_get_help (GOptionContext *context,
727 gint max_length, len;
730 GHashTable *shadow_map;
732 const gchar *rest_description;
736 string = g_string_sized_new (1024);
738 rest_description = NULL;
739 if (context->main_group)
742 for (i = 0; i < context->main_group->n_entries; i++)
744 entry = &context->main_group->entries[i];
745 if (entry->long_name[0] == 0)
747 rest_description = TRANSLATE (context->main_group, entry->arg_description);
753 g_string_append_printf (string, "%s\n %s %s",
754 _("Usage:"), g_get_prgname(), _("[OPTION...]"));
756 if (rest_description)
758 g_string_append (string, " ");
759 g_string_append (string, rest_description);
762 if (context->parameter_string)
764 g_string_append (string, " ");
765 g_string_append (string, TRANSLATE (context, context->parameter_string));
768 g_string_append (string, "\n\n");
770 if (context->summary)
772 g_string_append (string, TRANSLATE (context, context->summary));
773 g_string_append (string, "\n\n");
776 memset (seen, 0, sizeof (gboolean) * 256);
777 shadow_map = g_hash_table_new (g_str_hash, g_str_equal);
779 if (context->main_group)
781 for (i = 0; i < context->main_group->n_entries; i++)
783 entry = &context->main_group->entries[i];
784 g_hash_table_insert (shadow_map,
785 (gpointer)entry->long_name,
788 if (seen[(guchar)entry->short_name])
789 entry->short_name = 0;
791 seen[(guchar)entry->short_name] = TRUE;
795 list = context->groups;
798 GOptionGroup *g = list->data;
799 for (i = 0; i < g->n_entries; i++)
801 entry = &g->entries[i];
802 if (g_hash_table_lookup (shadow_map, entry->long_name) &&
803 !(entry->flags & G_OPTION_FLAG_NOALIAS))
804 entry->long_name = g_strdup_printf ("%s-%s", g->name, entry->long_name);
806 g_hash_table_insert (shadow_map, (gpointer)entry->long_name, entry);
808 if (seen[(guchar)entry->short_name] &&
809 !(entry->flags & G_OPTION_FLAG_NOALIAS))
810 entry->short_name = 0;
812 seen[(guchar)entry->short_name] = TRUE;
817 g_hash_table_destroy (shadow_map);
819 list = context->groups;
821 max_length = _g_utf8_strwidth ("-?, --help", -1);
825 len = _g_utf8_strwidth ("--help-all", -1);
826 max_length = MAX (max_length, len);
829 if (context->main_group)
831 len = calculate_max_length (context->main_group);
832 max_length = MAX (max_length, len);
837 GOptionGroup *g = list->data;
839 /* First, we check the --help-<groupname> options */
840 len = _g_utf8_strwidth ("--help-", -1) + _g_utf8_strwidth (g->name, -1);
841 max_length = MAX (max_length, len);
843 /* Then we go through the entries */
844 len = calculate_max_length (g);
845 max_length = MAX (max_length, len);
850 /* Add a bit of padding */
855 list = context->groups;
857 token = context_has_h_entry (context) ? '?' : 'h';
859 g_string_append_printf (string, "%s\n -%c, --%-*s %s\n",
860 _("Help Options:"), token, max_length - 4, "help",
861 _("Show help options"));
863 /* We only want --help-all when there are groups */
865 g_string_append_printf (string, " --%-*s %s\n",
866 max_length, "help-all",
867 _("Show all help options"));
871 GOptionGroup *g = list->data;
873 if (group_has_visible_entries (context, g, FALSE))
874 g_string_append_printf (string, " --help-%-*s %s\n",
875 max_length - 5, g->name,
876 TRANSLATE (g, g->help_description));
881 g_string_append (string, "\n");
886 /* Print a certain group */
888 if (group_has_visible_entries (context, group, FALSE))
890 g_string_append (string, TRANSLATE (group, group->description));
891 g_string_append (string, "\n");
892 for (i = 0; i < group->n_entries; i++)
893 print_entry (group, max_length, &group->entries[i], string);
894 g_string_append (string, "\n");
899 /* Print all groups */
901 list = context->groups;
905 GOptionGroup *g = list->data;
907 if (group_has_visible_entries (context, g, FALSE))
909 g_string_append (string, g->description);
910 g_string_append (string, "\n");
911 for (i = 0; i < g->n_entries; i++)
912 if (!(g->entries[i].flags & G_OPTION_FLAG_IN_MAIN))
913 print_entry (g, max_length, &g->entries[i], string);
915 g_string_append (string, "\n");
922 /* Print application options if --help or --help-all has been specified */
923 if ((main_help || !group) &&
924 (group_has_visible_entries (context, context->main_group, TRUE) ||
925 group_list_has_visible_entires (context, context->groups, TRUE)))
927 list = context->groups;
929 g_string_append (string, _("Application Options:"));
930 g_string_append (string, "\n");
931 if (context->main_group)
932 for (i = 0; i < context->main_group->n_entries; i++)
933 print_entry (context->main_group, max_length,
934 &context->main_group->entries[i], string);
938 GOptionGroup *g = list->data;
940 /* Print main entries from other groups */
941 for (i = 0; i < g->n_entries; i++)
942 if (g->entries[i].flags & G_OPTION_FLAG_IN_MAIN)
943 print_entry (g, max_length, &g->entries[i], string);
948 g_string_append (string, "\n");
951 if (context->description)
953 g_string_append (string, TRANSLATE (context, context->description));
954 g_string_append (string, "\n");
957 return g_string_free (string, FALSE);
962 print_help (GOptionContext *context,
968 help = g_option_context_get_help (context, main_help, group);
969 g_print ("%s", help);
976 parse_int (const gchar *arg_name,
985 tmp = strtol (arg, &end, 0);
987 if (*arg == '\0' || *end != '\0')
990 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
991 _("Cannot parse integer value '%s' for %s"),
997 if (*result != tmp || errno == ERANGE)
1000 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1001 _("Integer value '%s' for %s out of range"),
1011 parse_double (const gchar *arg_name,
1020 tmp = g_strtod (arg, &end);
1022 if (*arg == '\0' || *end != '\0')
1025 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1026 _("Cannot parse double value '%s' for %s"),
1030 if (errno == ERANGE)
1033 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1034 _("Double value '%s' for %s out of range"),
1046 parse_int64 (const gchar *arg_name,
1055 tmp = g_ascii_strtoll (arg, &end, 0);
1057 if (*arg == '\0' || *end != '\0')
1060 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1061 _("Cannot parse integer value '%s' for %s"),
1065 if (errno == ERANGE)
1068 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1069 _("Integer value '%s' for %s out of range"),
1081 get_change (GOptionContext *context,
1082 GOptionArg arg_type,
1086 Change *change = NULL;
1088 for (list = context->changes; list != NULL; list = list->next)
1090 change = list->data;
1092 if (change->arg_data == arg_data)
1096 change = g_new0 (Change, 1);
1097 change->arg_type = arg_type;
1098 change->arg_data = arg_data;
1100 context->changes = g_list_prepend (context->changes, change);
1108 add_pending_null (GOptionContext *context,
1114 n = g_new0 (PendingNull, 1);
1118 context->pending_nulls = g_list_prepend (context->pending_nulls, n);
1122 parse_arg (GOptionContext *context,
1123 GOptionGroup *group,
1124 GOptionEntry *entry,
1126 const gchar *option_name,
1132 g_assert (value || OPTIONAL_ARG (entry) || NO_ARG (entry));
1136 case G_OPTION_ARG_NONE:
1138 change = get_change (context, G_OPTION_ARG_NONE,
1141 *(gboolean *)entry->arg_data = !(entry->flags & G_OPTION_FLAG_REVERSE);
1144 case G_OPTION_ARG_STRING:
1148 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1153 change = get_change (context, G_OPTION_ARG_STRING,
1155 g_free (change->allocated.str);
1157 change->prev.str = *(gchar **)entry->arg_data;
1158 change->allocated.str = data;
1160 *(gchar **)entry->arg_data = data;
1163 case G_OPTION_ARG_STRING_ARRAY:
1167 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1172 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
1175 if (change->allocated.array.len == 0)
1177 change->prev.array = *(gchar ***)entry->arg_data;
1178 change->allocated.array.data = g_new (gchar *, 2);
1181 change->allocated.array.data =
1182 g_renew (gchar *, change->allocated.array.data,
1183 change->allocated.array.len + 2);
1185 change->allocated.array.data[change->allocated.array.len] = data;
1186 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
1188 change->allocated.array.len ++;
1190 *(gchar ***)entry->arg_data = change->allocated.array.data;
1195 case G_OPTION_ARG_FILENAME:
1200 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1205 data = g_strdup (value);
1207 change = get_change (context, G_OPTION_ARG_FILENAME,
1209 g_free (change->allocated.str);
1211 change->prev.str = *(gchar **)entry->arg_data;
1212 change->allocated.str = data;
1214 *(gchar **)entry->arg_data = data;
1218 case G_OPTION_ARG_FILENAME_ARRAY:
1223 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1228 data = g_strdup (value);
1230 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
1233 if (change->allocated.array.len == 0)
1235 change->prev.array = *(gchar ***)entry->arg_data;
1236 change->allocated.array.data = g_new (gchar *, 2);
1239 change->allocated.array.data =
1240 g_renew (gchar *, change->allocated.array.data,
1241 change->allocated.array.len + 2);
1243 change->allocated.array.data[change->allocated.array.len] = data;
1244 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
1246 change->allocated.array.len ++;
1248 *(gchar ***)entry->arg_data = change->allocated.array.data;
1253 case G_OPTION_ARG_INT:
1257 if (!parse_int (option_name, value,
1262 change = get_change (context, G_OPTION_ARG_INT,
1264 change->prev.integer = *(gint *)entry->arg_data;
1265 *(gint *)entry->arg_data = data;
1268 case G_OPTION_ARG_CALLBACK:
1273 if (!value && entry->flags & G_OPTION_FLAG_OPTIONAL_ARG)
1275 else if (entry->flags & G_OPTION_FLAG_NO_ARG)
1277 else if (entry->flags & G_OPTION_FLAG_FILENAME)
1280 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1282 data = g_strdup (value);
1286 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1288 if (!(entry->flags & (G_OPTION_FLAG_NO_ARG|G_OPTION_FLAG_OPTIONAL_ARG)) &&
1292 retval = (* (GOptionArgFunc) entry->arg_data) (option_name, data, group->user_data, error);
1294 if (!retval && error != NULL && *error == NULL)
1296 G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
1297 _("Error parsing option %s"), option_name);
1305 case G_OPTION_ARG_DOUBLE:
1309 if (!parse_double (option_name, value,
1316 change = get_change (context, G_OPTION_ARG_DOUBLE,
1318 change->prev.dbl = *(gdouble *)entry->arg_data;
1319 *(gdouble *)entry->arg_data = data;
1322 case G_OPTION_ARG_INT64:
1326 if (!parse_int64 (option_name, value,
1333 change = get_change (context, G_OPTION_ARG_INT64,
1335 change->prev.int64 = *(gint64 *)entry->arg_data;
1336 *(gint64 *)entry->arg_data = data;
1340 g_assert_not_reached ();
1347 parse_short_option (GOptionContext *context,
1348 GOptionGroup *group,
1359 for (j = 0; j < group->n_entries; j++)
1361 if (arg == group->entries[j].short_name)
1364 gchar *value = NULL;
1366 option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
1368 if (NO_ARG (&group->entries[j]))
1375 G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
1376 _("Error parsing option %s"), option_name);
1377 g_free (option_name);
1381 if (idx < *argc - 1)
1383 if (!OPTIONAL_ARG (&group->entries[j]))
1385 value = (*argv)[idx + 1];
1386 add_pending_null (context, &((*argv)[idx + 1]), NULL);
1391 if ((*argv)[idx + 1][0] == '-')
1395 value = (*argv)[idx + 1];
1396 add_pending_null (context, &((*argv)[idx + 1]), NULL);
1401 else if (idx >= *argc - 1 && OPTIONAL_ARG (&group->entries[j]))
1406 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1407 _("Missing argument for %s"), option_name);
1408 g_free (option_name);
1413 if (!parse_arg (context, group, &group->entries[j],
1414 value, option_name, error))
1416 g_free (option_name);
1420 g_free (option_name);
1429 parse_long_option (GOptionContext *context,
1430 GOptionGroup *group,
1441 for (j = 0; j < group->n_entries; j++)
1446 if (aliased && (group->entries[j].flags & G_OPTION_FLAG_NOALIAS))
1449 if (NO_ARG (&group->entries[j]) &&
1450 strcmp (arg, group->entries[j].long_name) == 0)
1455 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1456 retval = parse_arg (context, group, &group->entries[j],
1457 NULL, option_name, error);
1458 g_free(option_name);
1460 add_pending_null (context, &((*argv)[*idx]), NULL);
1467 gint len = strlen (group->entries[j].long_name);
1469 if (strncmp (arg, group->entries[j].long_name, len) == 0 &&
1470 (arg[len] == '=' || arg[len] == 0))
1472 gchar *value = NULL;
1475 add_pending_null (context, &((*argv)[*idx]), NULL);
1476 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1478 if (arg[len] == '=')
1479 value = arg + len + 1;
1480 else if (*idx < *argc - 1)
1482 if (!(group->entries[j].flags & G_OPTION_FLAG_OPTIONAL_ARG))
1484 value = (*argv)[*idx + 1];
1485 add_pending_null (context, &((*argv)[*idx + 1]), NULL);
1490 if ((*argv)[*idx + 1][0] == '-')
1493 retval = parse_arg (context, group, &group->entries[j],
1494 NULL, option_name, error);
1496 g_free (option_name);
1501 value = (*argv)[*idx + 1];
1502 add_pending_null (context, &((*argv)[*idx + 1]), NULL);
1507 else if (*idx >= *argc - 1 &&
1508 group->entries[j].flags & G_OPTION_FLAG_OPTIONAL_ARG)
1511 retval = parse_arg (context, group, &group->entries[j],
1512 NULL, option_name, error);
1514 g_free (option_name);
1520 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1521 _("Missing argument for %s"), option_name);
1522 g_free (option_name);
1526 if (!parse_arg (context, group, &group->entries[j],
1527 value, option_name, error))
1529 g_free (option_name);
1533 g_free (option_name);
1543 parse_remaining_arg (GOptionContext *context,
1544 GOptionGroup *group,
1553 for (j = 0; j < group->n_entries; j++)
1558 if (group->entries[j].long_name[0])
1561 g_return_val_if_fail (group->entries[j].arg == G_OPTION_ARG_CALLBACK ||
1562 group->entries[j].arg == G_OPTION_ARG_STRING_ARRAY ||
1563 group->entries[j].arg == G_OPTION_ARG_FILENAME_ARRAY, FALSE);
1565 add_pending_null (context, &((*argv)[*idx]), NULL);
1567 if (!parse_arg (context, group, &group->entries[j], (*argv)[*idx], "", error))
1578 free_changes_list (GOptionContext *context,
1583 for (list = context->changes; list != NULL; list = list->next)
1585 Change *change = list->data;
1589 switch (change->arg_type)
1591 case G_OPTION_ARG_NONE:
1592 *(gboolean *)change->arg_data = change->prev.bool;
1594 case G_OPTION_ARG_INT:
1595 *(gint *)change->arg_data = change->prev.integer;
1597 case G_OPTION_ARG_STRING:
1598 case G_OPTION_ARG_FILENAME:
1599 g_free (change->allocated.str);
1600 *(gchar **)change->arg_data = change->prev.str;
1602 case G_OPTION_ARG_STRING_ARRAY:
1603 case G_OPTION_ARG_FILENAME_ARRAY:
1604 g_strfreev (change->allocated.array.data);
1605 *(gchar ***)change->arg_data = change->prev.array;
1607 case G_OPTION_ARG_DOUBLE:
1608 *(gdouble *)change->arg_data = change->prev.dbl;
1610 case G_OPTION_ARG_INT64:
1611 *(gint64 *)change->arg_data = change->prev.int64;
1614 g_assert_not_reached ();
1621 g_list_free (context->changes);
1622 context->changes = NULL;
1626 free_pending_nulls (GOptionContext *context,
1627 gboolean perform_nulls)
1631 for (list = context->pending_nulls; list != NULL; list = list->next)
1633 PendingNull *n = list->data;
1639 /* Copy back the short options */
1641 strcpy (*n->ptr + 1, n->value);
1651 g_list_free (context->pending_nulls);
1652 context->pending_nulls = NULL;
1656 * g_option_context_parse:
1657 * @context: a #GOptionContext
1658 * @argc: a pointer to the number of command line arguments
1659 * @argv: a pointer to the array of command line arguments
1660 * @error: a return location for errors
1662 * Parses the command line arguments, recognizing options
1663 * which have been added to @context. A side-effect of
1664 * calling this function is that g_set_prgname() will be
1667 * If the parsing is successful, any parsed arguments are
1668 * removed from the array and @argc and @argv are updated
1669 * accordingly. A '--' option is stripped from @argv
1670 * unless there are unparsed options before and after it,
1671 * or some of the options after it start with '-'. In case
1672 * of an error, @argc and @argv are left unmodified.
1674 * If automatic <option>--help</option> support is enabled
1675 * (see g_option_context_set_help_enabled()), and the
1676 * @argv array contains one of the recognized help options,
1677 * this function will produce help output to stdout and
1678 * call <literal>exit (0)</literal>.
1680 * Note that function depends on the
1681 * <link linkend="setlocale">current locale</link> for
1682 * automatic character set conversion of string and filename
1685 * Return value: %TRUE if the parsing was successful,
1686 * %FALSE if an error occurred
1691 g_option_context_parse (GOptionContext *context,
1699 /* Set program name */
1700 if (!g_get_prgname())
1702 if (argc && argv && *argc)
1706 prgname = g_path_get_basename ((*argv)[0]);
1707 g_set_prgname (prgname);
1711 g_set_prgname ("<unknown>");
1714 /* Call pre-parse hooks */
1715 list = context->groups;
1718 GOptionGroup *group = list->data;
1720 if (group->pre_parse_func)
1722 if (!(* group->pre_parse_func) (context, group,
1723 group->user_data, error))
1730 if (context->main_group && context->main_group->pre_parse_func)
1732 if (!(* context->main_group->pre_parse_func) (context, context->main_group,
1733 context->main_group->user_data, error))
1739 gboolean stop_parsing = FALSE;
1740 gboolean has_unknown = FALSE;
1741 gint separator_pos = 0;
1743 for (i = 1; i < *argc; i++)
1746 gboolean parsed = FALSE;
1748 if ((*argv)[i][0] == '-' && (*argv)[i][1] != '\0' && !stop_parsing)
1750 if ((*argv)[i][1] == '-')
1754 arg = (*argv)[i] + 2;
1756 /* '--' terminates list of arguments */
1760 stop_parsing = TRUE;
1764 /* Handle help options */
1765 if (context->help_enabled)
1767 if (strcmp (arg, "help") == 0)
1768 print_help (context, TRUE, NULL);
1769 else if (strcmp (arg, "help-all") == 0)
1770 print_help (context, FALSE, NULL);
1771 else if (strncmp (arg, "help-", 5) == 0)
1773 list = context->groups;
1777 GOptionGroup *group = list->data;
1779 if (strcmp (arg + 5, group->name) == 0)
1780 print_help (context, FALSE, group);
1787 if (context->main_group &&
1788 !parse_long_option (context, context->main_group, &i, arg,
1789 FALSE, argc, argv, error, &parsed))
1795 /* Try the groups */
1796 list = context->groups;
1799 GOptionGroup *group = list->data;
1801 if (!parse_long_option (context, group, &i, arg,
1802 FALSE, argc, argv, error, &parsed))
1814 /* Now look for --<group>-<option> */
1815 dash = strchr (arg, '-');
1818 /* Try the groups */
1819 list = context->groups;
1822 GOptionGroup *group = list->data;
1824 if (strncmp (group->name, arg, dash - arg) == 0)
1826 if (!parse_long_option (context, group, &i, dash + 1,
1827 TRUE, argc, argv, error, &parsed))
1838 if (context->ignore_unknown)
1842 { /* short option */
1843 gint new_i = i, arg_length;
1844 gboolean *nulled_out = NULL;
1845 gboolean has_h_entry = context_has_h_entry (context);
1846 arg = (*argv)[i] + 1;
1847 arg_length = strlen (arg);
1848 nulled_out = g_newa (gboolean, arg_length);
1849 memset (nulled_out, 0, arg_length * sizeof (gboolean));
1850 for (j = 0; j < arg_length; j++)
1852 if (context->help_enabled && (arg[j] == '?' ||
1853 (arg[j] == 'h' && !has_h_entry)))
1854 print_help (context, TRUE, NULL);
1856 if (context->main_group &&
1857 !parse_short_option (context, context->main_group,
1859 argc, argv, error, &parsed))
1863 /* Try the groups */
1864 list = context->groups;
1867 GOptionGroup *group = list->data;
1868 if (!parse_short_option (context, group, i, &new_i, arg[j],
1869 argc, argv, error, &parsed))
1877 if (context->ignore_unknown && parsed)
1878 nulled_out[j] = TRUE;
1879 else if (context->ignore_unknown)
1883 /* !context->ignore_unknown && parsed */
1885 if (context->ignore_unknown)
1887 gchar *new_arg = NULL;
1889 for (j = 0; j < arg_length; j++)
1894 new_arg = g_malloc (arg_length + 1);
1895 new_arg[arg_index++] = arg[j];
1899 new_arg[arg_index] = '\0';
1900 add_pending_null (context, &((*argv)[i]), new_arg);
1904 add_pending_null (context, &((*argv)[i]), NULL);
1912 if (!parsed && !context->ignore_unknown)
1915 G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
1916 _("Unknown option %s"), (*argv)[i]);
1922 /* Collect remaining args */
1923 if (context->main_group &&
1924 !parse_remaining_arg (context, context->main_group, &i,
1925 argc, argv, error, &parsed))
1928 if (!parsed && (has_unknown || (*argv)[i][0] == '-'))
1933 if (separator_pos > 0)
1934 add_pending_null (context, &((*argv)[separator_pos]), NULL);
1938 /* Call post-parse hooks */
1939 list = context->groups;
1942 GOptionGroup *group = list->data;
1944 if (group->post_parse_func)
1946 if (!(* group->post_parse_func) (context, group,
1947 group->user_data, error))
1954 if (context->main_group && context->main_group->post_parse_func)
1956 if (!(* context->main_group->post_parse_func) (context, context->main_group,
1957 context->main_group->user_data, error))
1963 free_pending_nulls (context, TRUE);
1965 for (i = 1; i < *argc; i++)
1967 for (k = i; k < *argc; k++)
1968 if ((*argv)[k] != NULL)
1974 for (j = i + k; j < *argc; j++)
1976 (*argv)[j-k] = (*argv)[j];
1988 /* Call error hooks */
1989 list = context->groups;
1992 GOptionGroup *group = list->data;
1994 if (group->error_func)
1995 (* group->error_func) (context, group,
1996 group->user_data, error);
2001 if (context->main_group && context->main_group->error_func)
2002 (* context->main_group->error_func) (context, context->main_group,
2003 context->main_group->user_data, error);
2005 free_changes_list (context, TRUE);
2006 free_pending_nulls (context, FALSE);
2012 * g_option_group_new:
2013 * @name: the name for the option group, this is used to provide
2014 * help for the options in this group with <option>--help-</option>@name
2015 * @description: a description for this group to be shown in
2016 * <option>--help</option>. This string is translated using the translation
2017 * domain or translation function of the group
2018 * @help_description: a description for the <option>--help-</option>@name option.
2019 * This string is translated using the translation domain or translation function
2021 * @user_data: user data that will be passed to the pre- and post-parse hooks,
2022 * the error hook and to callbacks of %G_OPTION_ARG_CALLBACK options, or %NULL
2023 * @destroy: a function that will be called to free @user_data, or %NULL
2025 * Creates a new #GOptionGroup.
2027 * Return value: a newly created option group. It should be added
2028 * to a #GOptionContext or freed with g_option_group_free().
2033 g_option_group_new (const gchar *name,
2034 const gchar *description,
2035 const gchar *help_description,
2037 GDestroyNotify destroy)
2040 GOptionGroup *group;
2042 group = g_new0 (GOptionGroup, 1);
2043 group->name = g_strdup (name);
2044 group->description = g_strdup (description);
2045 group->help_description = g_strdup (help_description);
2046 group->user_data = user_data;
2047 group->destroy_notify = destroy;
2054 * g_option_group_free:
2055 * @group: a #GOptionGroup
2057 * Frees a #GOptionGroup. Note that you must <emphasis>not</emphasis>
2058 * free groups which have been added to a #GOptionContext.
2063 g_option_group_free (GOptionGroup *group)
2065 g_return_if_fail (group != NULL);
2067 g_free (group->name);
2068 g_free (group->description);
2069 g_free (group->help_description);
2071 g_free (group->entries);
2073 if (group->destroy_notify)
2074 (* group->destroy_notify) (group->user_data);
2076 if (group->translate_notify)
2077 (* group->translate_notify) (group->translate_data);
2084 * g_option_group_add_entries:
2085 * @group: a #GOptionGroup
2086 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
2088 * Adds the options specified in @entries to @group.
2093 g_option_group_add_entries (GOptionGroup *group,
2094 const GOptionEntry *entries)
2098 g_return_if_fail (entries != NULL);
2100 for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++) ;
2102 group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
2104 memcpy (group->entries + group->n_entries, entries, sizeof (GOptionEntry) * n_entries);
2106 for (i = group->n_entries; i < group->n_entries + n_entries; i++)
2108 gchar c = group->entries[i].short_name;
2112 if (c == '-' || !g_ascii_isprint (c))
2114 g_warning (G_STRLOC": ignoring invalid short option '%c' (%d)", c, c);
2115 group->entries[i].short_name = 0;
2120 group->n_entries += n_entries;
2124 * g_option_group_set_parse_hooks:
2125 * @group: a #GOptionGroup
2126 * @pre_parse_func: a function to call before parsing, or %NULL
2127 * @post_parse_func: a function to call after parsing, or %NULL
2129 * Associates two functions with @group which will be called
2130 * from g_option_context_parse() before the first option is parsed
2131 * and after the last option has been parsed, respectively.
2133 * Note that the user data to be passed to @pre_parse_func and
2134 * @post_parse_func can be specified when constructing the group
2135 * with g_option_group_new().
2140 g_option_group_set_parse_hooks (GOptionGroup *group,
2141 GOptionParseFunc pre_parse_func,
2142 GOptionParseFunc post_parse_func)
2144 g_return_if_fail (group != NULL);
2146 group->pre_parse_func = pre_parse_func;
2147 group->post_parse_func = post_parse_func;
2151 * g_option_group_set_error_hook:
2152 * @group: a #GOptionGroup
2153 * @error_func: a function to call when an error occurs
2155 * Associates a function with @group which will be called
2156 * from g_option_context_parse() when an error occurs.
2158 * Note that the user data to be passed to @error_func can be
2159 * specified when constructing the group with g_option_group_new().
2164 g_option_group_set_error_hook (GOptionGroup *group,
2165 GOptionErrorFunc error_func)
2167 g_return_if_fail (group != NULL);
2169 group->error_func = error_func;
2174 * g_option_group_set_translate_func:
2175 * @group: a #GOptionGroup
2176 * @func: the #GTranslateFunc, or %NULL
2177 * @data: user data to pass to @func, or %NULL
2178 * @destroy_notify: a function which gets called to free @data, or %NULL
2180 * Sets the function which is used to translate user-visible
2181 * strings, for <option>--help</option> output. Different
2182 * groups can use different #GTranslateFunc<!-- -->s. If @func
2183 * is %NULL, strings are not translated.
2185 * If you are using gettext(), you only need to set the translation
2186 * domain, see g_option_group_set_translation_domain().
2191 g_option_group_set_translate_func (GOptionGroup *group,
2192 GTranslateFunc func,
2194 GDestroyNotify destroy_notify)
2196 g_return_if_fail (group != NULL);
2198 if (group->translate_notify)
2199 group->translate_notify (group->translate_data);
2201 group->translate_func = func;
2202 group->translate_data = data;
2203 group->translate_notify = destroy_notify;
2206 static const gchar *
2207 dgettext_swapped (const gchar *msgid,
2208 const gchar *domainname)
2210 return g_dgettext (domainname, msgid);
2214 * g_option_group_set_translation_domain:
2215 * @group: a #GOptionGroup
2216 * @domain: the domain to use
2218 * A convenience function to use gettext() for translating
2219 * user-visible strings.
2224 g_option_group_set_translation_domain (GOptionGroup *group,
2225 const gchar *domain)
2227 g_return_if_fail (group != NULL);
2229 g_option_group_set_translate_func (group,
2230 (GTranslateFunc)dgettext_swapped,
2236 * g_option_context_set_translate_func:
2237 * @context: a #GOptionContext
2238 * @func: the #GTranslateFunc, or %NULL
2239 * @data: user data to pass to @func, or %NULL
2240 * @destroy_notify: a function which gets called to free @data, or %NULL
2242 * Sets the function which is used to translate the contexts
2243 * user-visible strings, for <option>--help</option> output.
2244 * If @func is %NULL, strings are not translated.
2246 * Note that option groups have their own translation functions,
2247 * this function only affects the @parameter_string (see g_option_context_new()),
2248 * the summary (see g_option_context_set_summary()) and the description
2249 * (see g_option_context_set_description()).
2251 * If you are using gettext(), you only need to set the translation
2252 * domain, see g_option_context_set_translation_domain().
2257 g_option_context_set_translate_func (GOptionContext *context,
2258 GTranslateFunc func,
2260 GDestroyNotify destroy_notify)
2262 g_return_if_fail (context != NULL);
2264 if (context->translate_notify)
2265 context->translate_notify (context->translate_data);
2267 context->translate_func = func;
2268 context->translate_data = data;
2269 context->translate_notify = destroy_notify;
2273 * g_option_context_set_translation_domain:
2274 * @context: a #GOptionContext
2275 * @domain: the domain to use
2277 * A convenience function to use gettext() for translating
2278 * user-visible strings.
2283 g_option_context_set_translation_domain (GOptionContext *context,
2284 const gchar *domain)
2286 g_return_if_fail (context != NULL);
2288 g_option_context_set_translate_func (context,
2289 (GTranslateFunc)dgettext_swapped,
2295 * g_option_context_set_summary:
2296 * @context: a #GOptionContext
2297 * @summary: a string to be shown in <option>--help</option> output
2298 * before the list of options, or %NULL
2300 * Adds a string to be displayed in <option>--help</option> output
2301 * before the list of options. This is typically a summary of the
2302 * program functionality.
2304 * Note that the summary is translated (see
2305 * g_option_context_set_translate_func() and
2306 * g_option_context_set_translation_domain()).
2311 g_option_context_set_summary (GOptionContext *context,
2312 const gchar *summary)
2314 g_return_if_fail (context != NULL);
2316 g_free (context->summary);
2317 context->summary = g_strdup (summary);
2322 * g_option_context_get_summary:
2323 * @context: a #GOptionContext
2325 * Returns the summary. See g_option_context_set_summary().
2327 * Returns: the summary
2331 G_CONST_RETURN gchar *
2332 g_option_context_get_summary (GOptionContext *context)
2334 g_return_val_if_fail (context != NULL, NULL);
2336 return context->summary;
2340 * g_option_context_set_description:
2341 * @context: a #GOptionContext
2342 * @description: a string to be shown in <option>--help</option> output
2343 * after the list of options, or %NULL
2345 * Adds a string to be displayed in <option>--help</option> output
2346 * after the list of options. This text often includes a bug reporting
2349 * Note that the summary is translated (see
2350 * g_option_context_set_translate_func()).
2355 g_option_context_set_description (GOptionContext *context,
2356 const gchar *description)
2358 g_return_if_fail (context != NULL);
2360 g_free (context->description);
2361 context->description = g_strdup (description);
2366 * g_option_context_get_description:
2367 * @context: a #GOptionContext
2369 * Returns the description. See g_option_context_set_description().
2371 * Returns: the description
2375 G_CONST_RETURN gchar *
2376 g_option_context_get_description (GOptionContext *context)
2378 g_return_val_if_fail (context != NULL, NULL);
2380 return context->description;