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, which is useful if the extra argument
42 * starts with a dash, which would otherwise cause it to be interpreted
44 * </para></listitem><listitem><para>
45 * Non-option arguments are returned to the application as rest arguments.
46 * </para></listitem><listitem><para>
47 * An argument consisting solely of two dashes turns off further parsing,
48 * any remaining arguments (even those starting with a dash) are returned
49 * to the application as rest arguments.
50 * </para></listitem></itemizedlist>
52 * Another important feature of GOption is that it can automatically generate
53 * nicely formatted help output. Unless it is explicitly turned off with
54 * g_option_context_set_help_enabled(), GOption will recognize the
55 * <option>--help</option>, <option>-?</option>, <option>--help-all</option>
56 * and <option>--help-</option><replaceable>groupname</replaceable> options
57 * (where <replaceable>groupname</replaceable> is the name of a #GOptionGroup)
58 * and write a text similar to the one shown in the following example to stdout.
60 * <informalexample><screen>
62 * testtreemodel [OPTION...] - test tree model performance
65 * -h, --help Show help options
66 * --help-all Show all help options
67 * --help-gtk Show GTK+ Options
69 * Application Options:
70 * -r, --repeats=N Average over N repetitions
71 * -m, --max-size=M Test up to 2^M items
72 * --display=DISPLAY X display to use
73 * -v, --verbose Be verbose
74 * -b, --beep Beep when done
75 * --rand Randomize the data
76 * </screen></informalexample>
78 * GOption groups options in #GOptionGroup<!-- -->s, which makes it easy to
79 * incorporate options from multiple sources. The intended use for this is
80 * to let applications collect option groups from the libraries it uses,
81 * add them to their #GOptionContext, and parse all options by a single call
82 * to g_option_context_parse(). See gtk_get_option_group() for an example.
84 * If an option is declared to be of type string or filename, GOption takes
85 * care of converting it to the right encoding; strings are returned in UTF-8,
86 * filenames are returned in the GLib filename encoding. Note that this only
87 * works if setlocale() has been called before g_option_context_parse().
89 * Here is a complete example of setting up GOption to parse the example
90 * commandline above and produce the example help output.
92 * <informalexample><programlisting>
93 * static gint repeats = 2;
94 * static gint max_size = 8;
95 * static gboolean verbose = FALSE;
96 * static gboolean beep = FALSE;
97 * static gboolean rand = FALSE;
99 * static GOptionEntry entries[] =
101 * { "repeats", 'r', 0, G_OPTION_ARG_INT, &repeats, "Average over N repetitions", "N" },
102 * { "max-size", 'm', 0, G_OPTION_ARG_INT, &max_size, "Test up to 2^M items", "M" },
103 * { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL },
104 * { "beep", 'b', 0, G_OPTION_ARG_NONE, &beep, "Beep when done", NULL },
105 * { "rand", 0, 0, G_OPTION_ARG_NONE, &rand, "Randomize the data", NULL },
110 * main (int argc, char *argv[])
112 * GError *error = NULL;
113 * GOptionContext *context;
115 * context = g_option_context_new ("- test tree model performance");
116 * g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
117 * g_option_context_add_group (context, gtk_get_option_group (TRUE));
118 * if (!g_option_context_parse (context, &argc, &argv, &error))
120 * g_print ("option parsing failed: %s\n", error->message);
127 * </programlisting></informalexample>
134 #include "glibintl.h"
143 #define TRANSLATE(group, str) (((group)->translate_func ? (* (group)->translate_func) ((str), (group)->translate_data) : (str)))
145 #define NO_ARG(entry) ((entry)->arg == G_OPTION_ARG_NONE || \
146 ((entry)->arg == G_OPTION_ARG_CALLBACK && \
147 ((entry)->flags & G_OPTION_FLAG_NO_ARG)))
149 #define OPTIONAL_ARG(entry) ((entry)->arg == G_OPTION_ARG_CALLBACK && \
150 (entry)->flags & G_OPTION_FLAG_OPTIONAL_ARG)
182 struct _GOptionContext
186 gchar *parameter_string;
190 GTranslateFunc translate_func;
191 GDestroyNotify translate_notify;
192 gpointer translate_data;
194 guint help_enabled : 1;
195 guint ignore_unknown : 1;
197 GOptionGroup *main_group;
199 /* We keep a list of change so we can revert them */
202 /* We also keep track of all argv elements
203 * that should be NULLed or modified.
205 GList *pending_nulls;
212 gchar *help_description;
214 GDestroyNotify destroy_notify;
217 GTranslateFunc translate_func;
218 GDestroyNotify translate_notify;
219 gpointer translate_data;
221 GOptionEntry *entries;
224 GOptionParseFunc pre_parse_func;
225 GOptionParseFunc post_parse_func;
226 GOptionErrorFunc error_func;
229 static void free_changes_list (GOptionContext *context,
231 static void free_pending_nulls (GOptionContext *context,
232 gboolean perform_nulls);
236 _g_unichar_get_width (gunichar c)
238 if (G_UNLIKELY (g_unichar_iszerowidth (c)))
241 /* we ignore the fact that we should call g_unichar_iswide_cjk() under
242 * some locales (legacy East Asian ones) */
243 if (g_unichar_iswide (c))
250 _g_utf8_strwidth (const gchar *p,
254 const gchar *start = p;
255 g_return_val_if_fail (p != NULL || max == 0, 0);
261 len += _g_unichar_get_width (g_utf8_get_char (p));
262 p = g_utf8_next_char (p);
270 /* this case may not be quite correct */
272 len += _g_unichar_get_width (g_utf8_get_char (p));
273 p = g_utf8_next_char (p);
275 while (p - start < max && *p)
277 len += _g_unichar_get_width (g_utf8_get_char (p));
278 p = g_utf8_next_char (p);
287 g_option_error_quark (void)
289 return g_quark_from_static_string ("g-option-context-error-quark");
293 * g_option_context_new:
294 * @parameter_string: a string which is displayed in
295 * the first line of <option>--help</option> output, after the
297 * <literal><replaceable>programname</replaceable> [OPTION...]</literal>
299 * Creates a new option context.
301 * The @parameter_string can serve multiple purposes. It can be used
302 * to add descriptions for "rest" arguments, which are not parsed by
303 * the #GOptionContext, typically something like "FILES" or
304 * "FILE1 FILE2...". If you are using #G_OPTION_REMAINING for
305 * collecting "rest" arguments, GLib handles this automatically by
306 * using the @arg_description of the corresponding #GOptionEntry in
309 * Another usage is to give a short summary of the program
310 * functionality, like " - frob the strings", which will be displayed
311 * in the same line as the usage. For a longer description of the
312 * program functionality that should be displayed as a paragraph
313 * below the usage line, use g_option_context_set_summary().
315 * Note that the @parameter_string is translated using the
316 * function set with g_option_context_set_translate_func(), so
317 * it should normally be passed untranslated.
319 * Returns: a newly created #GOptionContext, which must be
320 * freed with g_option_context_free() after use.
325 g_option_context_new (const gchar *parameter_string)
328 GOptionContext *context;
330 context = g_new0 (GOptionContext, 1);
332 context->parameter_string = g_strdup (parameter_string);
333 context->help_enabled = TRUE;
334 context->ignore_unknown = FALSE;
340 * g_option_context_free:
341 * @context: a #GOptionContext
343 * Frees context and all the groups which have been
346 * Please note that parsed arguments need to be freed separately (see
351 void g_option_context_free (GOptionContext *context)
353 g_return_if_fail (context != NULL);
355 g_list_foreach (context->groups, (GFunc)g_option_group_free, NULL);
356 g_list_free (context->groups);
358 if (context->main_group)
359 g_option_group_free (context->main_group);
361 free_changes_list (context, FALSE);
362 free_pending_nulls (context, FALSE);
364 g_free (context->parameter_string);
365 g_free (context->summary);
366 g_free (context->description);
368 if (context->translate_notify)
369 (* context->translate_notify) (context->translate_data);
376 * g_option_context_set_help_enabled:
377 * @context: a #GOptionContext
378 * @help_enabled: %TRUE to enable <option>--help</option>, %FALSE to disable it
380 * Enables or disables automatic generation of <option>--help</option>
381 * output. By default, g_option_context_parse() recognizes
382 * <option>--help</option>, <option>-h</option>,
383 * <option>-?</option>, <option>--help-all</option>
384 * and <option>--help-</option><replaceable>groupname</replaceable> and creates
385 * suitable output to stdout.
389 void g_option_context_set_help_enabled (GOptionContext *context,
390 gboolean help_enabled)
393 g_return_if_fail (context != NULL);
395 context->help_enabled = help_enabled;
399 * g_option_context_get_help_enabled:
400 * @context: a #GOptionContext
402 * Returns whether automatic <option>--help</option> generation
403 * is turned on for @context. See g_option_context_set_help_enabled().
405 * Returns: %TRUE if automatic help generation is turned on.
410 g_option_context_get_help_enabled (GOptionContext *context)
412 g_return_val_if_fail (context != NULL, FALSE);
414 return context->help_enabled;
418 * g_option_context_set_ignore_unknown_options:
419 * @context: a #GOptionContext
420 * @ignore_unknown: %TRUE to ignore unknown options, %FALSE to produce
421 * an error when unknown options are met
423 * Sets whether to ignore unknown options or not. If an argument is
424 * ignored, it is left in the @argv array after parsing. By default,
425 * g_option_context_parse() treats unknown options as error.
427 * This setting does not affect non-option arguments (i.e. arguments
428 * which don't start with a dash). But note that GOption cannot reliably
429 * determine whether a non-option belongs to a preceding unknown option.
434 g_option_context_set_ignore_unknown_options (GOptionContext *context,
435 gboolean ignore_unknown)
437 g_return_if_fail (context != NULL);
439 context->ignore_unknown = ignore_unknown;
443 * g_option_context_get_ignore_unknown_options:
444 * @context: a #GOptionContext
446 * Returns whether unknown options are ignored or not. See
447 * g_option_context_set_ignore_unknown_options().
449 * Returns: %TRUE if unknown options are ignored.
454 g_option_context_get_ignore_unknown_options (GOptionContext *context)
456 g_return_val_if_fail (context != NULL, FALSE);
458 return context->ignore_unknown;
462 * g_option_context_add_group:
463 * @context: a #GOptionContext
464 * @group: the group to add
466 * Adds a #GOptionGroup to the @context, so that parsing with @context
467 * will recognize the options in the group. Note that the group will
468 * be freed together with the context when g_option_context_free() is
469 * called, so you must not free the group yourself after adding it
475 g_option_context_add_group (GOptionContext *context,
480 g_return_if_fail (context != NULL);
481 g_return_if_fail (group != NULL);
482 g_return_if_fail (group->name != NULL);
483 g_return_if_fail (group->description != NULL);
484 g_return_if_fail (group->help_description != NULL);
486 for (list = context->groups; list; list = list->next)
488 GOptionGroup *g = (GOptionGroup *)list->data;
490 if ((group->name == NULL && g->name == NULL) ||
491 (group->name && g->name && strcmp (group->name, g->name) == 0))
492 g_warning ("A group named \"%s\" is already part of this GOptionContext",
496 context->groups = g_list_append (context->groups, group);
500 * g_option_context_set_main_group:
501 * @context: a #GOptionContext
502 * @group: the group to set as main group
504 * Sets a #GOptionGroup as main group of the @context.
505 * This has the same effect as calling g_option_context_add_group(),
506 * the only difference is that the options in the main group are
507 * treated differently when generating <option>--help</option> output.
512 g_option_context_set_main_group (GOptionContext *context,
515 g_return_if_fail (context != NULL);
516 g_return_if_fail (group != NULL);
518 if (context->main_group)
520 g_warning ("This GOptionContext already has a main group");
525 context->main_group = group;
529 * g_option_context_get_main_group:
530 * @context: a #GOptionContext
532 * Returns a pointer to the main group of @context.
534 * Return value: the main group of @context, or %NULL if @context doesn't
535 * have a main group. Note that group belongs to @context and should
536 * not be modified or freed.
541 g_option_context_get_main_group (GOptionContext *context)
543 g_return_val_if_fail (context != NULL, NULL);
545 return context->main_group;
549 * g_option_context_add_main_entries:
550 * @context: a #GOptionContext
551 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
552 * @translation_domain: a translation domain to use for translating
553 * the <option>--help</option> output for the options in @entries
554 * with gettext(), or %NULL
556 * A convenience function which creates a main group if it doesn't
557 * exist, adds the @entries to it and sets the translation domain.
562 g_option_context_add_main_entries (GOptionContext *context,
563 const GOptionEntry *entries,
564 const gchar *translation_domain)
566 g_return_if_fail (entries != NULL);
568 if (!context->main_group)
569 context->main_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
571 g_option_group_add_entries (context->main_group, entries);
572 g_option_group_set_translation_domain (context->main_group, translation_domain);
576 calculate_max_length (GOptionGroup *group)
579 gint i, len, max_length;
583 for (i = 0; i < group->n_entries; i++)
585 entry = &group->entries[i];
587 if (entry->flags & G_OPTION_FLAG_HIDDEN)
590 len = _g_utf8_strwidth (entry->long_name, -1);
592 if (entry->short_name)
595 if (!NO_ARG (entry) && entry->arg_description)
596 len += 1 + _g_utf8_strwidth (TRANSLATE (group, entry->arg_description), -1);
598 max_length = MAX (max_length, len);
605 print_entry (GOptionGroup *group,
607 const GOptionEntry *entry,
612 if (entry->flags & G_OPTION_FLAG_HIDDEN)
615 if (entry->long_name[0] == 0)
618 str = g_string_new (NULL);
620 if (entry->short_name)
621 g_string_append_printf (str, " -%c, --%s", entry->short_name, entry->long_name);
623 g_string_append_printf (str, " --%s", entry->long_name);
625 if (entry->arg_description)
626 g_string_append_printf (str, "=%s", TRANSLATE (group, entry->arg_description));
628 g_string_append_printf (string, "%s%*s %s\n", str->str,
629 (int) (max_length + 4 - _g_utf8_strwidth (str->str, -1)), "",
630 entry->description ? TRANSLATE (group, entry->description) : "");
631 g_string_free (str, TRUE);
635 group_has_visible_entries (GOptionContext *context,
637 gboolean main_entries)
639 GOptionFlags reject_filter = G_OPTION_FLAG_HIDDEN;
642 gboolean main_group = group == context->main_group;
645 reject_filter |= G_OPTION_FLAG_IN_MAIN;
647 for (i = 0, l = (group ? group->n_entries : 0); i < l; i++)
649 entry = &group->entries[i];
651 if (main_entries && !main_group && !(entry->flags & G_OPTION_FLAG_IN_MAIN))
653 if (!(entry->flags & reject_filter))
661 group_list_has_visible_entires (GOptionContext *context,
663 gboolean main_entries)
667 if (group_has_visible_entries (context, group_list->data, main_entries))
670 group_list = group_list->next;
677 context_has_h_entry (GOptionContext *context)
682 if (context->main_group)
684 for (i = 0; i < context->main_group->n_entries; i++)
686 if (context->main_group->entries[i].short_name == 'h')
691 for (list = context->groups; list != NULL; list = g_list_next (list))
695 group = (GOptionGroup*)list->data;
696 for (i = 0; i < group->n_entries; i++)
698 if (group->entries[i].short_name == 'h')
706 * g_option_context_get_help:
707 * @context: a #GOptionContext
708 * @main_help: if %TRUE, only include the main group
709 * @group: the #GOptionGroup to create help for, or %NULL
711 * Returns a formatted, translated help text for the given context.
712 * To obtain the text produced by <option>--help</option>, call
713 * <literal>g_option_context_get_help (context, TRUE, NULL)</literal>.
714 * To obtain the text produced by <option>--help-all</option>, call
715 * <literal>g_option_context_get_help (context, FALSE, NULL)</literal>.
716 * To obtain the help text for an option group, call
717 * <literal>g_option_context_get_help (context, FALSE, group)</literal>.
719 * Returns: A newly allocated string containing the help text
724 g_option_context_get_help (GOptionContext *context,
729 gint max_length, len;
732 GHashTable *shadow_map;
734 const gchar *rest_description;
738 string = g_string_sized_new (1024);
740 rest_description = NULL;
741 if (context->main_group)
744 for (i = 0; i < context->main_group->n_entries; i++)
746 entry = &context->main_group->entries[i];
747 if (entry->long_name[0] == 0)
749 rest_description = TRANSLATE (context->main_group, entry->arg_description);
755 g_string_append_printf (string, "%s\n %s %s",
756 _("Usage:"), g_get_prgname(), _("[OPTION...]"));
758 if (rest_description)
760 g_string_append (string, " ");
761 g_string_append (string, rest_description);
764 if (context->parameter_string)
766 g_string_append (string, " ");
767 g_string_append (string, TRANSLATE (context, context->parameter_string));
770 g_string_append (string, "\n\n");
772 if (context->summary)
774 g_string_append (string, TRANSLATE (context, context->summary));
775 g_string_append (string, "\n\n");
778 memset (seen, 0, sizeof (gboolean) * 256);
779 shadow_map = g_hash_table_new (g_str_hash, g_str_equal);
781 if (context->main_group)
783 for (i = 0; i < context->main_group->n_entries; i++)
785 entry = &context->main_group->entries[i];
786 g_hash_table_insert (shadow_map,
787 (gpointer)entry->long_name,
790 if (seen[(guchar)entry->short_name])
791 entry->short_name = 0;
793 seen[(guchar)entry->short_name] = TRUE;
797 list = context->groups;
800 GOptionGroup *g = list->data;
801 for (i = 0; i < g->n_entries; i++)
803 entry = &g->entries[i];
804 if (g_hash_table_lookup (shadow_map, entry->long_name) &&
805 !(entry->flags & G_OPTION_FLAG_NOALIAS))
806 entry->long_name = g_strdup_printf ("%s-%s", g->name, entry->long_name);
808 g_hash_table_insert (shadow_map, (gpointer)entry->long_name, entry);
810 if (seen[(guchar)entry->short_name] &&
811 !(entry->flags & G_OPTION_FLAG_NOALIAS))
812 entry->short_name = 0;
814 seen[(guchar)entry->short_name] = TRUE;
819 g_hash_table_destroy (shadow_map);
821 list = context->groups;
823 max_length = _g_utf8_strwidth ("-?, --help", -1);
827 len = _g_utf8_strwidth ("--help-all", -1);
828 max_length = MAX (max_length, len);
831 if (context->main_group)
833 len = calculate_max_length (context->main_group);
834 max_length = MAX (max_length, len);
839 GOptionGroup *g = list->data;
841 /* First, we check the --help-<groupname> options */
842 len = _g_utf8_strwidth ("--help-", -1) + _g_utf8_strwidth (g->name, -1);
843 max_length = MAX (max_length, len);
845 /* Then we go through the entries */
846 len = calculate_max_length (g);
847 max_length = MAX (max_length, len);
852 /* Add a bit of padding */
857 list = context->groups;
859 token = context_has_h_entry (context) ? '?' : 'h';
861 g_string_append_printf (string, "%s\n -%c, --%-*s %s\n",
862 _("Help Options:"), token, max_length - 4, "help",
863 _("Show help options"));
865 /* We only want --help-all when there are groups */
867 g_string_append_printf (string, " --%-*s %s\n",
868 max_length, "help-all",
869 _("Show all help options"));
873 GOptionGroup *g = list->data;
875 if (group_has_visible_entries (context, g, FALSE))
876 g_string_append_printf (string, " --help-%-*s %s\n",
877 max_length - 5, g->name,
878 TRANSLATE (g, g->help_description));
883 g_string_append (string, "\n");
888 /* Print a certain group */
890 if (group_has_visible_entries (context, group, FALSE))
892 g_string_append (string, TRANSLATE (group, group->description));
893 g_string_append (string, "\n");
894 for (i = 0; i < group->n_entries; i++)
895 print_entry (group, max_length, &group->entries[i], string);
896 g_string_append (string, "\n");
901 /* Print all groups */
903 list = context->groups;
907 GOptionGroup *g = list->data;
909 if (group_has_visible_entries (context, g, FALSE))
911 g_string_append (string, g->description);
912 g_string_append (string, "\n");
913 for (i = 0; i < g->n_entries; i++)
914 if (!(g->entries[i].flags & G_OPTION_FLAG_IN_MAIN))
915 print_entry (g, max_length, &g->entries[i], string);
917 g_string_append (string, "\n");
924 /* Print application options if --help or --help-all has been specified */
925 if ((main_help || !group) &&
926 (group_has_visible_entries (context, context->main_group, TRUE) ||
927 group_list_has_visible_entires (context, context->groups, TRUE)))
929 list = context->groups;
931 g_string_append (string, _("Application Options:"));
932 g_string_append (string, "\n");
933 if (context->main_group)
934 for (i = 0; i < context->main_group->n_entries; i++)
935 print_entry (context->main_group, max_length,
936 &context->main_group->entries[i], string);
940 GOptionGroup *g = list->data;
942 /* Print main entries from other groups */
943 for (i = 0; i < g->n_entries; i++)
944 if (g->entries[i].flags & G_OPTION_FLAG_IN_MAIN)
945 print_entry (g, max_length, &g->entries[i], string);
950 g_string_append (string, "\n");
953 if (context->description)
955 g_string_append (string, TRANSLATE (context, context->description));
956 g_string_append (string, "\n");
959 return g_string_free (string, FALSE);
964 print_help (GOptionContext *context,
970 help = g_option_context_get_help (context, main_help, group);
971 g_print ("%s", help);
978 parse_int (const gchar *arg_name,
987 tmp = strtol (arg, &end, 0);
989 if (*arg == '\0' || *end != '\0')
992 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
993 _("Cannot parse integer value '%s' for %s"),
999 if (*result != tmp || errno == ERANGE)
1002 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1003 _("Integer value '%s' for %s out of range"),
1013 parse_double (const gchar *arg_name,
1022 tmp = g_strtod (arg, &end);
1024 if (*arg == '\0' || *end != '\0')
1027 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1028 _("Cannot parse double value '%s' for %s"),
1032 if (errno == ERANGE)
1035 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1036 _("Double value '%s' for %s out of range"),
1048 parse_int64 (const gchar *arg_name,
1057 tmp = g_ascii_strtoll (arg, &end, 0);
1059 if (*arg == '\0' || *end != '\0')
1062 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1063 _("Cannot parse integer value '%s' for %s"),
1067 if (errno == ERANGE)
1070 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1071 _("Integer value '%s' for %s out of range"),
1083 get_change (GOptionContext *context,
1084 GOptionArg arg_type,
1088 Change *change = NULL;
1090 for (list = context->changes; list != NULL; list = list->next)
1092 change = list->data;
1094 if (change->arg_data == arg_data)
1098 change = g_new0 (Change, 1);
1099 change->arg_type = arg_type;
1100 change->arg_data = arg_data;
1102 context->changes = g_list_prepend (context->changes, change);
1110 add_pending_null (GOptionContext *context,
1116 n = g_new0 (PendingNull, 1);
1120 context->pending_nulls = g_list_prepend (context->pending_nulls, n);
1124 parse_arg (GOptionContext *context,
1125 GOptionGroup *group,
1126 GOptionEntry *entry,
1128 const gchar *option_name,
1134 g_assert (value || OPTIONAL_ARG (entry) || NO_ARG (entry));
1138 case G_OPTION_ARG_NONE:
1140 change = get_change (context, G_OPTION_ARG_NONE,
1143 *(gboolean *)entry->arg_data = !(entry->flags & G_OPTION_FLAG_REVERSE);
1146 case G_OPTION_ARG_STRING:
1150 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1155 change = get_change (context, G_OPTION_ARG_STRING,
1157 g_free (change->allocated.str);
1159 change->prev.str = *(gchar **)entry->arg_data;
1160 change->allocated.str = data;
1162 *(gchar **)entry->arg_data = data;
1165 case G_OPTION_ARG_STRING_ARRAY:
1169 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1174 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
1177 if (change->allocated.array.len == 0)
1179 change->prev.array = *(gchar ***)entry->arg_data;
1180 change->allocated.array.data = g_new (gchar *, 2);
1183 change->allocated.array.data =
1184 g_renew (gchar *, change->allocated.array.data,
1185 change->allocated.array.len + 2);
1187 change->allocated.array.data[change->allocated.array.len] = data;
1188 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
1190 change->allocated.array.len ++;
1192 *(gchar ***)entry->arg_data = change->allocated.array.data;
1197 case G_OPTION_ARG_FILENAME:
1202 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1207 data = g_strdup (value);
1209 change = get_change (context, G_OPTION_ARG_FILENAME,
1211 g_free (change->allocated.str);
1213 change->prev.str = *(gchar **)entry->arg_data;
1214 change->allocated.str = data;
1216 *(gchar **)entry->arg_data = data;
1220 case G_OPTION_ARG_FILENAME_ARRAY:
1225 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1230 data = g_strdup (value);
1232 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
1235 if (change->allocated.array.len == 0)
1237 change->prev.array = *(gchar ***)entry->arg_data;
1238 change->allocated.array.data = g_new (gchar *, 2);
1241 change->allocated.array.data =
1242 g_renew (gchar *, change->allocated.array.data,
1243 change->allocated.array.len + 2);
1245 change->allocated.array.data[change->allocated.array.len] = data;
1246 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
1248 change->allocated.array.len ++;
1250 *(gchar ***)entry->arg_data = change->allocated.array.data;
1255 case G_OPTION_ARG_INT:
1259 if (!parse_int (option_name, value,
1264 change = get_change (context, G_OPTION_ARG_INT,
1266 change->prev.integer = *(gint *)entry->arg_data;
1267 *(gint *)entry->arg_data = data;
1270 case G_OPTION_ARG_CALLBACK:
1275 if (!value && entry->flags & G_OPTION_FLAG_OPTIONAL_ARG)
1277 else if (entry->flags & G_OPTION_FLAG_NO_ARG)
1279 else if (entry->flags & G_OPTION_FLAG_FILENAME)
1282 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1284 data = g_strdup (value);
1288 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1290 if (!(entry->flags & (G_OPTION_FLAG_NO_ARG|G_OPTION_FLAG_OPTIONAL_ARG)) &&
1294 retval = (* (GOptionArgFunc) entry->arg_data) (option_name, data, group->user_data, error);
1296 if (!retval && error != NULL && *error == NULL)
1298 G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
1299 _("Error parsing option %s"), option_name);
1307 case G_OPTION_ARG_DOUBLE:
1311 if (!parse_double (option_name, value,
1318 change = get_change (context, G_OPTION_ARG_DOUBLE,
1320 change->prev.dbl = *(gdouble *)entry->arg_data;
1321 *(gdouble *)entry->arg_data = data;
1324 case G_OPTION_ARG_INT64:
1328 if (!parse_int64 (option_name, value,
1335 change = get_change (context, G_OPTION_ARG_INT64,
1337 change->prev.int64 = *(gint64 *)entry->arg_data;
1338 *(gint64 *)entry->arg_data = data;
1342 g_assert_not_reached ();
1349 parse_short_option (GOptionContext *context,
1350 GOptionGroup *group,
1361 for (j = 0; j < group->n_entries; j++)
1363 if (arg == group->entries[j].short_name)
1366 gchar *value = NULL;
1368 option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
1370 if (NO_ARG (&group->entries[j]))
1377 G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
1378 _("Error parsing option %s"), option_name);
1379 g_free (option_name);
1383 if (idx < *argc - 1)
1385 if (!OPTIONAL_ARG (&group->entries[j]))
1387 value = (*argv)[idx + 1];
1388 add_pending_null (context, &((*argv)[idx + 1]), NULL);
1393 if ((*argv)[idx + 1][0] == '-')
1397 value = (*argv)[idx + 1];
1398 add_pending_null (context, &((*argv)[idx + 1]), NULL);
1403 else if (idx >= *argc - 1 && OPTIONAL_ARG (&group->entries[j]))
1408 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1409 _("Missing argument for %s"), option_name);
1410 g_free (option_name);
1415 if (!parse_arg (context, group, &group->entries[j],
1416 value, option_name, error))
1418 g_free (option_name);
1422 g_free (option_name);
1431 parse_long_option (GOptionContext *context,
1432 GOptionGroup *group,
1443 for (j = 0; j < group->n_entries; j++)
1448 if (aliased && (group->entries[j].flags & G_OPTION_FLAG_NOALIAS))
1451 if (NO_ARG (&group->entries[j]) &&
1452 strcmp (arg, group->entries[j].long_name) == 0)
1457 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1458 retval = parse_arg (context, group, &group->entries[j],
1459 NULL, option_name, error);
1460 g_free(option_name);
1462 add_pending_null (context, &((*argv)[*idx]), NULL);
1469 gint len = strlen (group->entries[j].long_name);
1471 if (strncmp (arg, group->entries[j].long_name, len) == 0 &&
1472 (arg[len] == '=' || arg[len] == 0))
1474 gchar *value = NULL;
1477 add_pending_null (context, &((*argv)[*idx]), NULL);
1478 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1480 if (arg[len] == '=')
1481 value = arg + len + 1;
1482 else if (*idx < *argc - 1)
1484 if (!(group->entries[j].flags & G_OPTION_FLAG_OPTIONAL_ARG))
1486 value = (*argv)[*idx + 1];
1487 add_pending_null (context, &((*argv)[*idx + 1]), NULL);
1492 if ((*argv)[*idx + 1][0] == '-')
1495 retval = parse_arg (context, group, &group->entries[j],
1496 NULL, option_name, error);
1498 g_free (option_name);
1503 value = (*argv)[*idx + 1];
1504 add_pending_null (context, &((*argv)[*idx + 1]), NULL);
1509 else if (*idx >= *argc - 1 &&
1510 group->entries[j].flags & G_OPTION_FLAG_OPTIONAL_ARG)
1513 retval = parse_arg (context, group, &group->entries[j],
1514 NULL, option_name, error);
1516 g_free (option_name);
1522 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1523 _("Missing argument for %s"), option_name);
1524 g_free (option_name);
1528 if (!parse_arg (context, group, &group->entries[j],
1529 value, option_name, error))
1531 g_free (option_name);
1535 g_free (option_name);
1545 parse_remaining_arg (GOptionContext *context,
1546 GOptionGroup *group,
1555 for (j = 0; j < group->n_entries; j++)
1560 if (group->entries[j].long_name[0])
1563 g_return_val_if_fail (group->entries[j].arg == G_OPTION_ARG_CALLBACK ||
1564 group->entries[j].arg == G_OPTION_ARG_STRING_ARRAY ||
1565 group->entries[j].arg == G_OPTION_ARG_FILENAME_ARRAY, FALSE);
1567 add_pending_null (context, &((*argv)[*idx]), NULL);
1569 if (!parse_arg (context, group, &group->entries[j], (*argv)[*idx], "", error))
1580 free_changes_list (GOptionContext *context,
1585 for (list = context->changes; list != NULL; list = list->next)
1587 Change *change = list->data;
1591 switch (change->arg_type)
1593 case G_OPTION_ARG_NONE:
1594 *(gboolean *)change->arg_data = change->prev.bool;
1596 case G_OPTION_ARG_INT:
1597 *(gint *)change->arg_data = change->prev.integer;
1599 case G_OPTION_ARG_STRING:
1600 case G_OPTION_ARG_FILENAME:
1601 g_free (change->allocated.str);
1602 *(gchar **)change->arg_data = change->prev.str;
1604 case G_OPTION_ARG_STRING_ARRAY:
1605 case G_OPTION_ARG_FILENAME_ARRAY:
1606 g_strfreev (change->allocated.array.data);
1607 *(gchar ***)change->arg_data = change->prev.array;
1609 case G_OPTION_ARG_DOUBLE:
1610 *(gdouble *)change->arg_data = change->prev.dbl;
1612 case G_OPTION_ARG_INT64:
1613 *(gint64 *)change->arg_data = change->prev.int64;
1616 g_assert_not_reached ();
1623 g_list_free (context->changes);
1624 context->changes = NULL;
1628 free_pending_nulls (GOptionContext *context,
1629 gboolean perform_nulls)
1633 for (list = context->pending_nulls; list != NULL; list = list->next)
1635 PendingNull *n = list->data;
1641 /* Copy back the short options */
1643 strcpy (*n->ptr + 1, n->value);
1653 g_list_free (context->pending_nulls);
1654 context->pending_nulls = NULL;
1658 * g_option_context_parse:
1659 * @context: a #GOptionContext
1660 * @argc: a pointer to the number of command line arguments
1661 * @argv: a pointer to the array of command line arguments
1662 * @error: a return location for errors
1664 * Parses the command line arguments, recognizing options
1665 * which have been added to @context. A side-effect of
1666 * calling this function is that g_set_prgname() will be
1669 * If the parsing is successful, any parsed arguments are
1670 * removed from the array and @argc and @argv are updated
1671 * accordingly. A '--' option is stripped from @argv
1672 * unless there are unparsed options before and after it,
1673 * or some of the options after it start with '-'. In case
1674 * of an error, @argc and @argv are left unmodified.
1676 * If automatic <option>--help</option> support is enabled
1677 * (see g_option_context_set_help_enabled()), and the
1678 * @argv array contains one of the recognized help options,
1679 * this function will produce help output to stdout and
1680 * call <literal>exit (0)</literal>.
1682 * Note that function depends on the
1683 * <link linkend="setlocale">current locale</link> for
1684 * automatic character set conversion of string and filename
1687 * Return value: %TRUE if the parsing was successful,
1688 * %FALSE if an error occurred
1693 g_option_context_parse (GOptionContext *context,
1701 /* Set program name */
1702 if (!g_get_prgname())
1704 if (argc && argv && *argc)
1708 prgname = g_path_get_basename ((*argv)[0]);
1709 g_set_prgname (prgname);
1713 g_set_prgname ("<unknown>");
1716 /* Call pre-parse hooks */
1717 list = context->groups;
1720 GOptionGroup *group = list->data;
1722 if (group->pre_parse_func)
1724 if (!(* group->pre_parse_func) (context, group,
1725 group->user_data, error))
1732 if (context->main_group && context->main_group->pre_parse_func)
1734 if (!(* context->main_group->pre_parse_func) (context, context->main_group,
1735 context->main_group->user_data, error))
1741 gboolean stop_parsing = FALSE;
1742 gboolean has_unknown = FALSE;
1743 gint separator_pos = 0;
1745 for (i = 1; i < *argc; i++)
1748 gboolean parsed = FALSE;
1750 if ((*argv)[i][0] == '-' && (*argv)[i][1] != '\0' && !stop_parsing)
1752 if ((*argv)[i][1] == '-')
1756 arg = (*argv)[i] + 2;
1758 /* '--' terminates list of arguments */
1762 stop_parsing = TRUE;
1766 /* Handle help options */
1767 if (context->help_enabled)
1769 if (strcmp (arg, "help") == 0)
1770 print_help (context, TRUE, NULL);
1771 else if (strcmp (arg, "help-all") == 0)
1772 print_help (context, FALSE, NULL);
1773 else if (strncmp (arg, "help-", 5) == 0)
1775 list = context->groups;
1779 GOptionGroup *group = list->data;
1781 if (strcmp (arg + 5, group->name) == 0)
1782 print_help (context, FALSE, group);
1789 if (context->main_group &&
1790 !parse_long_option (context, context->main_group, &i, arg,
1791 FALSE, argc, argv, error, &parsed))
1797 /* Try the groups */
1798 list = context->groups;
1801 GOptionGroup *group = list->data;
1803 if (!parse_long_option (context, group, &i, arg,
1804 FALSE, argc, argv, error, &parsed))
1816 /* Now look for --<group>-<option> */
1817 dash = strchr (arg, '-');
1820 /* Try the groups */
1821 list = context->groups;
1824 GOptionGroup *group = list->data;
1826 if (strncmp (group->name, arg, dash - arg) == 0)
1828 if (!parse_long_option (context, group, &i, dash + 1,
1829 TRUE, argc, argv, error, &parsed))
1840 if (context->ignore_unknown)
1844 { /* short option */
1845 gint new_i = i, arg_length;
1846 gboolean *nulled_out = NULL;
1847 gboolean has_h_entry = context_has_h_entry (context);
1848 arg = (*argv)[i] + 1;
1849 arg_length = strlen (arg);
1850 nulled_out = g_newa (gboolean, arg_length);
1851 memset (nulled_out, 0, arg_length * sizeof (gboolean));
1852 for (j = 0; j < arg_length; j++)
1854 if (context->help_enabled && (arg[j] == '?' ||
1855 (arg[j] == 'h' && !has_h_entry)))
1856 print_help (context, TRUE, NULL);
1858 if (context->main_group &&
1859 !parse_short_option (context, context->main_group,
1861 argc, argv, error, &parsed))
1865 /* Try the groups */
1866 list = context->groups;
1869 GOptionGroup *group = list->data;
1870 if (!parse_short_option (context, group, i, &new_i, arg[j],
1871 argc, argv, error, &parsed))
1879 if (context->ignore_unknown && parsed)
1880 nulled_out[j] = TRUE;
1881 else if (context->ignore_unknown)
1885 /* !context->ignore_unknown && parsed */
1887 if (context->ignore_unknown)
1889 gchar *new_arg = NULL;
1891 for (j = 0; j < arg_length; j++)
1896 new_arg = g_malloc (arg_length + 1);
1897 new_arg[arg_index++] = arg[j];
1901 new_arg[arg_index] = '\0';
1902 add_pending_null (context, &((*argv)[i]), new_arg);
1906 add_pending_null (context, &((*argv)[i]), NULL);
1914 if (!parsed && !context->ignore_unknown)
1917 G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
1918 _("Unknown option %s"), (*argv)[i]);
1924 /* Collect remaining args */
1925 if (context->main_group &&
1926 !parse_remaining_arg (context, context->main_group, &i,
1927 argc, argv, error, &parsed))
1930 if (!parsed && (has_unknown || (*argv)[i][0] == '-'))
1935 if (separator_pos > 0)
1936 add_pending_null (context, &((*argv)[separator_pos]), NULL);
1940 /* Call post-parse hooks */
1941 list = context->groups;
1944 GOptionGroup *group = list->data;
1946 if (group->post_parse_func)
1948 if (!(* group->post_parse_func) (context, group,
1949 group->user_data, error))
1956 if (context->main_group && context->main_group->post_parse_func)
1958 if (!(* context->main_group->post_parse_func) (context, context->main_group,
1959 context->main_group->user_data, error))
1965 free_pending_nulls (context, TRUE);
1967 for (i = 1; i < *argc; i++)
1969 for (k = i; k < *argc; k++)
1970 if ((*argv)[k] != NULL)
1976 for (j = i + k; j < *argc; j++)
1978 (*argv)[j-k] = (*argv)[j];
1990 /* Call error hooks */
1991 list = context->groups;
1994 GOptionGroup *group = list->data;
1996 if (group->error_func)
1997 (* group->error_func) (context, group,
1998 group->user_data, error);
2003 if (context->main_group && context->main_group->error_func)
2004 (* context->main_group->error_func) (context, context->main_group,
2005 context->main_group->user_data, error);
2007 free_changes_list (context, TRUE);
2008 free_pending_nulls (context, FALSE);
2014 * g_option_group_new:
2015 * @name: the name for the option group, this is used to provide
2016 * help for the options in this group with <option>--help-</option>@name
2017 * @description: a description for this group to be shown in
2018 * <option>--help</option>. This string is translated using the translation
2019 * domain or translation function of the group
2020 * @help_description: a description for the <option>--help-</option>@name option.
2021 * This string is translated using the translation domain or translation function
2023 * @user_data: user data that will be passed to the pre- and post-parse hooks,
2024 * the error hook and to callbacks of %G_OPTION_ARG_CALLBACK options, or %NULL
2025 * @destroy: a function that will be called to free @user_data, or %NULL
2027 * Creates a new #GOptionGroup.
2029 * Return value: a newly created option group. It should be added
2030 * to a #GOptionContext or freed with g_option_group_free().
2035 g_option_group_new (const gchar *name,
2036 const gchar *description,
2037 const gchar *help_description,
2039 GDestroyNotify destroy)
2042 GOptionGroup *group;
2044 group = g_new0 (GOptionGroup, 1);
2045 group->name = g_strdup (name);
2046 group->description = g_strdup (description);
2047 group->help_description = g_strdup (help_description);
2048 group->user_data = user_data;
2049 group->destroy_notify = destroy;
2056 * g_option_group_free:
2057 * @group: a #GOptionGroup
2059 * Frees a #GOptionGroup. Note that you must <emphasis>not</emphasis>
2060 * free groups which have been added to a #GOptionContext.
2065 g_option_group_free (GOptionGroup *group)
2067 g_return_if_fail (group != NULL);
2069 g_free (group->name);
2070 g_free (group->description);
2071 g_free (group->help_description);
2073 g_free (group->entries);
2075 if (group->destroy_notify)
2076 (* group->destroy_notify) (group->user_data);
2078 if (group->translate_notify)
2079 (* group->translate_notify) (group->translate_data);
2086 * g_option_group_add_entries:
2087 * @group: a #GOptionGroup
2088 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
2090 * Adds the options specified in @entries to @group.
2095 g_option_group_add_entries (GOptionGroup *group,
2096 const GOptionEntry *entries)
2100 g_return_if_fail (entries != NULL);
2102 for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++) ;
2104 group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
2106 memcpy (group->entries + group->n_entries, entries, sizeof (GOptionEntry) * n_entries);
2108 for (i = group->n_entries; i < group->n_entries + n_entries; i++)
2110 gchar c = group->entries[i].short_name;
2114 if (c == '-' || !g_ascii_isprint (c))
2116 g_warning (G_STRLOC": ignoring invalid short option '%c' (%d)", c, c);
2117 group->entries[i].short_name = 0;
2122 group->n_entries += n_entries;
2126 * g_option_group_set_parse_hooks:
2127 * @group: a #GOptionGroup
2128 * @pre_parse_func: a function to call before parsing, or %NULL
2129 * @post_parse_func: a function to call after parsing, or %NULL
2131 * Associates two functions with @group which will be called
2132 * from g_option_context_parse() before the first option is parsed
2133 * and after the last option has been parsed, respectively.
2135 * Note that the user data to be passed to @pre_parse_func and
2136 * @post_parse_func can be specified when constructing the group
2137 * with g_option_group_new().
2142 g_option_group_set_parse_hooks (GOptionGroup *group,
2143 GOptionParseFunc pre_parse_func,
2144 GOptionParseFunc post_parse_func)
2146 g_return_if_fail (group != NULL);
2148 group->pre_parse_func = pre_parse_func;
2149 group->post_parse_func = post_parse_func;
2153 * g_option_group_set_error_hook:
2154 * @group: a #GOptionGroup
2155 * @error_func: a function to call when an error occurs
2157 * Associates a function with @group which will be called
2158 * from g_option_context_parse() when an error occurs.
2160 * Note that the user data to be passed to @error_func can be
2161 * specified when constructing the group with g_option_group_new().
2166 g_option_group_set_error_hook (GOptionGroup *group,
2167 GOptionErrorFunc error_func)
2169 g_return_if_fail (group != NULL);
2171 group->error_func = error_func;
2176 * g_option_group_set_translate_func:
2177 * @group: a #GOptionGroup
2178 * @func: the #GTranslateFunc, or %NULL
2179 * @data: user data to pass to @func, or %NULL
2180 * @destroy_notify: a function which gets called to free @data, or %NULL
2182 * Sets the function which is used to translate user-visible
2183 * strings, for <option>--help</option> output. Different
2184 * groups can use different #GTranslateFunc<!-- -->s. If @func
2185 * is %NULL, strings are not translated.
2187 * If you are using gettext(), you only need to set the translation
2188 * domain, see g_option_group_set_translation_domain().
2193 g_option_group_set_translate_func (GOptionGroup *group,
2194 GTranslateFunc func,
2196 GDestroyNotify destroy_notify)
2198 g_return_if_fail (group != NULL);
2200 if (group->translate_notify)
2201 group->translate_notify (group->translate_data);
2203 group->translate_func = func;
2204 group->translate_data = data;
2205 group->translate_notify = destroy_notify;
2208 static const gchar *
2209 dgettext_swapped (const gchar *msgid,
2210 const gchar *domainname)
2212 return g_dgettext (domainname, msgid);
2216 * g_option_group_set_translation_domain:
2217 * @group: a #GOptionGroup
2218 * @domain: the domain to use
2220 * A convenience function to use gettext() for translating
2221 * user-visible strings.
2226 g_option_group_set_translation_domain (GOptionGroup *group,
2227 const gchar *domain)
2229 g_return_if_fail (group != NULL);
2231 g_option_group_set_translate_func (group,
2232 (GTranslateFunc)dgettext_swapped,
2238 * g_option_context_set_translate_func:
2239 * @context: a #GOptionContext
2240 * @func: the #GTranslateFunc, or %NULL
2241 * @data: user data to pass to @func, or %NULL
2242 * @destroy_notify: a function which gets called to free @data, or %NULL
2244 * Sets the function which is used to translate the contexts
2245 * user-visible strings, for <option>--help</option> output.
2246 * If @func is %NULL, strings are not translated.
2248 * Note that option groups have their own translation functions,
2249 * this function only affects the @parameter_string (see g_option_context_new()),
2250 * the summary (see g_option_context_set_summary()) and the description
2251 * (see g_option_context_set_description()).
2253 * If you are using gettext(), you only need to set the translation
2254 * domain, see g_option_context_set_translation_domain().
2259 g_option_context_set_translate_func (GOptionContext *context,
2260 GTranslateFunc func,
2262 GDestroyNotify destroy_notify)
2264 g_return_if_fail (context != NULL);
2266 if (context->translate_notify)
2267 context->translate_notify (context->translate_data);
2269 context->translate_func = func;
2270 context->translate_data = data;
2271 context->translate_notify = destroy_notify;
2275 * g_option_context_set_translation_domain:
2276 * @context: a #GOptionContext
2277 * @domain: the domain to use
2279 * A convenience function to use gettext() for translating
2280 * user-visible strings.
2285 g_option_context_set_translation_domain (GOptionContext *context,
2286 const gchar *domain)
2288 g_return_if_fail (context != NULL);
2290 g_option_context_set_translate_func (context,
2291 (GTranslateFunc)dgettext_swapped,
2297 * g_option_context_set_summary:
2298 * @context: a #GOptionContext
2299 * @summary: a string to be shown in <option>--help</option> output
2300 * before the list of options, or %NULL
2302 * Adds a string to be displayed in <option>--help</option> output
2303 * before the list of options. This is typically a summary of the
2304 * program functionality.
2306 * Note that the summary is translated (see
2307 * g_option_context_set_translate_func() and
2308 * g_option_context_set_translation_domain()).
2313 g_option_context_set_summary (GOptionContext *context,
2314 const gchar *summary)
2316 g_return_if_fail (context != NULL);
2318 g_free (context->summary);
2319 context->summary = g_strdup (summary);
2324 * g_option_context_get_summary:
2325 * @context: a #GOptionContext
2327 * Returns the summary. See g_option_context_set_summary().
2329 * Returns: the summary
2333 G_CONST_RETURN gchar *
2334 g_option_context_get_summary (GOptionContext *context)
2336 g_return_val_if_fail (context != NULL, NULL);
2338 return context->summary;
2342 * g_option_context_set_description:
2343 * @context: a #GOptionContext
2344 * @description: a string to be shown in <option>--help</option> output
2345 * after the list of options, or %NULL
2347 * Adds a string to be displayed in <option>--help</option> output
2348 * after the list of options. This text often includes a bug reporting
2351 * Note that the summary is translated (see
2352 * g_option_context_set_translate_func()).
2357 g_option_context_set_description (GOptionContext *context,
2358 const gchar *description)
2360 g_return_if_fail (context != NULL);
2362 g_free (context->description);
2363 context->description = g_strdup (description);
2368 * g_option_context_get_description:
2369 * @context: a #GOptionContext
2371 * Returns the description. See g_option_context_set_description().
2373 * Returns: the description
2377 G_CONST_RETURN gchar *
2378 g_option_context_get_description (GOptionContext *context)
2380 g_return_val_if_fail (context != NULL, NULL);
2382 return context->description;