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 * -?, --help Show help options
64 * --help-all Show all help options
65 * --help-gtk Show GTK+ Options
67 * Application Options:
68 * -r, --repeats=N Average over N repetitions
69 * -m, --max-size=M Test up to 2^M items
70 * --display=DISPLAY X display to use
71 * -v, --verbose Be verbose
72 * -b, --beep Beep when done
73 * --rand Randomize the data
74 * </screen></informalexample>
76 * GOption groups options in #GOptionGroup<!-- -->s, which makes it easy to
77 * incorporate options from multiple sources. The intended use for this is
78 * to let applications collect option groups from the libraries it uses,
79 * add them to their #GOptionContext, and parse all options by a single call
80 * to g_option_context_parse(). See gtk_get_option_group() for an example.
82 * If an option is declared to be of type string or filename, GOption takes
83 * care of converting it to the right encoding; strings are returned in UTF-8,
84 * filenames are returned in the GLib filename encoding. Note that this only
85 * works if setlocale() has been called before g_option_context_parse().
87 * Here is a complete example of setting up GOption to parse the example
88 * commandline above and produce the example help output.
90 * <informalexample><programlisting>
91 * static gint repeats = 2;
92 * static gint max_size = 8;
93 * static gboolean verbose = FALSE;
94 * static gboolean beep = FALSE;
95 * static gboolean rand = FALSE;
97 * static GOptionEntry entries[] =
99 * { "repeats", 'r', 0, G_OPTION_ARG_INT, &repeats, "Average over N repetitions", "N" },
100 * { "max-size", 'm', 0, G_OPTION_ARG_INT, &max_size, "Test up to 2^M items", "M" },
101 * { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL },
102 * { "beep", 'b', 0, G_OPTION_ARG_NONE, &beep, "Beep when done", NULL },
103 * { "rand", 0, 0, G_OPTION_ARG_NONE, &rand, "Randomize the data", NULL },
108 * main (int argc, char *argv[])
110 * GError *error = NULL;
111 * GOptionContext *context;
113 * context = g_option_context_new ("- test tree model performance");
114 * g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
115 * g_option_context_add_group (context, gtk_get_option_group (TRUE));
116 * if (!g_option_context_parse (context, &argc, &argv, &error))
118 * g_print ("option parsing failed: %s\n", error->message);
125 * </programlisting></informalexample>
132 #include "glibintl.h"
142 #define TRANSLATE(group, str) (((group)->translate_func ? (* (group)->translate_func) ((str), (group)->translate_data) : (str)))
144 #define NO_ARG(entry) ((entry)->arg == G_OPTION_ARG_NONE || \
145 ((entry)->arg == G_OPTION_ARG_CALLBACK && \
146 ((entry)->flags & G_OPTION_FLAG_NO_ARG)))
148 #define OPTIONAL_ARG(entry) ((entry)->arg == G_OPTION_ARG_CALLBACK && \
149 (entry)->flags & G_OPTION_FLAG_OPTIONAL_ARG)
181 struct _GOptionContext
185 gchar *parameter_string;
189 GTranslateFunc translate_func;
190 GDestroyNotify translate_notify;
191 gpointer translate_data;
193 guint help_enabled : 1;
194 guint ignore_unknown : 1;
196 GOptionGroup *main_group;
198 /* We keep a list of change so we can revert them */
201 /* We also keep track of all argv elements
202 * that should be NULLed or modified.
204 GList *pending_nulls;
211 gchar *help_description;
213 GDestroyNotify destroy_notify;
216 GTranslateFunc translate_func;
217 GDestroyNotify translate_notify;
218 gpointer translate_data;
220 GOptionEntry *entries;
223 GOptionParseFunc pre_parse_func;
224 GOptionParseFunc post_parse_func;
225 GOptionErrorFunc error_func;
228 static void free_changes_list (GOptionContext *context,
230 static void free_pending_nulls (GOptionContext *context,
231 gboolean perform_nulls);
235 _g_unichar_get_width (gunichar c)
237 if (G_UNLIKELY (g_unichar_iszerowidth (c)))
240 /* we ignore the fact that we should call g_unichar_iswide_cjk() under
241 * some locales (legacy East Asian ones) */
242 if (g_unichar_iswide (c))
249 _g_utf8_strwidth (const gchar *p,
253 const gchar *start = p;
254 g_return_val_if_fail (p != NULL || max == 0, 0);
260 len += _g_unichar_get_width (g_utf8_get_char (p));
261 p = g_utf8_next_char (p);
269 /* this case may not be quite correct */
271 len += _g_unichar_get_width (g_utf8_get_char (p));
272 p = g_utf8_next_char (p);
274 while (p - start < max && *p)
276 len += _g_unichar_get_width (g_utf8_get_char (p));
277 p = g_utf8_next_char (p);
286 g_option_error_quark (void)
288 return g_quark_from_static_string ("g-option-context-error-quark");
292 * g_option_context_new:
293 * @parameter_string: a string which is displayed in
294 * the first line of <option>--help</option> output, after the
296 * <literal><replaceable>programname</replaceable> [OPTION...]</literal>
298 * Creates a new option context.
300 * The @parameter_string can serve multiple purposes. It can be used
301 * to add descriptions for "rest" arguments, which are not parsed by
302 * the #GOptionContext, typically something like "FILES" or
303 * "FILE1 FILE2...". If you are using #G_OPTION_REMAINING for
304 * collecting "rest" arguments, GLib handles this automatically by
305 * using the @arg_description of the corresponding #GOptionEntry in
308 * Another usage is to give a short summary of the program
309 * functionality, like " - frob the strings", which will be displayed
310 * in the same line as the usage. For a longer description of the
311 * program functionality that should be displayed as a paragraph
312 * below the usage line, use g_option_context_set_summary().
314 * Note that the @parameter_string is translated using the
315 * function set with g_option_context_set_translate_func(), so
316 * it should normally be passed untranslated.
318 * Returns: a newly created #GOptionContext, which must be
319 * freed with g_option_context_free() after use.
324 g_option_context_new (const gchar *parameter_string)
327 GOptionContext *context;
329 context = g_new0 (GOptionContext, 1);
331 context->parameter_string = g_strdup (parameter_string);
332 context->help_enabled = TRUE;
333 context->ignore_unknown = FALSE;
339 * g_option_context_free:
340 * @context: a #GOptionContext
342 * Frees context and all the groups which have been
345 * Please note that parsed arguments need to be freed separately (see
350 void g_option_context_free (GOptionContext *context)
352 g_return_if_fail (context != NULL);
354 g_list_foreach (context->groups, (GFunc)g_option_group_free, NULL);
355 g_list_free (context->groups);
357 if (context->main_group)
358 g_option_group_free (context->main_group);
360 free_changes_list (context, FALSE);
361 free_pending_nulls (context, FALSE);
363 g_free (context->parameter_string);
364 g_free (context->summary);
365 g_free (context->description);
367 if (context->translate_notify)
368 (* context->translate_notify) (context->translate_data);
375 * g_option_context_set_help_enabled:
376 * @context: a #GOptionContext
377 * @help_enabled: %TRUE to enable <option>--help</option>, %FALSE to disable it
379 * Enables or disables automatic generation of <option>--help</option>
380 * output. By default, g_option_context_parse() recognizes
381 * <option>--help</option>, <option>-?</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 * g_option_context_get_help:
676 * @context: a #GOptionContext
677 * @main_help: if %TRUE, only include the main group
678 * @group: the #GOptionGroup to create help for, or %NULL
680 * Returns a formatted, translated help text for the given context.
681 * To obtain the text produced by <option>--help</option>, call
682 * <literal>g_option_context_get_help (context, TRUE, NULL)</literal>.
683 * To obtain the text produced by <option>--help-all</option>, call
684 * <literal>g_option_context_get_help (context, FALSE, NULL)</literal>.
685 * To obtain the help text for an option group, call
686 * <literal>g_option_context_get_help (context, FALSE, group)</literal>.
688 * Returns: A newly allocated string containing the help text
693 g_option_context_get_help (GOptionContext *context,
698 gint max_length, len;
701 GHashTable *shadow_map;
703 const gchar *rest_description;
706 string = g_string_sized_new (1024);
708 rest_description = NULL;
709 if (context->main_group)
712 for (i = 0; i < context->main_group->n_entries; i++)
714 entry = &context->main_group->entries[i];
715 if (entry->long_name[0] == 0)
717 rest_description = TRANSLATE (context->main_group, entry->arg_description);
723 g_string_append_printf (string, "%s\n %s %s",
724 _("Usage:"), g_get_prgname(), _("[OPTION...]"));
726 if (rest_description)
728 g_string_append (string, " ");
729 g_string_append (string, rest_description);
732 if (context->parameter_string)
734 g_string_append (string, " ");
735 g_string_append (string, TRANSLATE (context, context->parameter_string));
738 g_string_append (string, "\n\n");
740 if (context->summary)
742 g_string_append (string, TRANSLATE (context, context->summary));
743 g_string_append (string, "\n\n");
746 memset (seen, 0, sizeof (gboolean) * 256);
747 shadow_map = g_hash_table_new (g_str_hash, g_str_equal);
749 if (context->main_group)
751 for (i = 0; i < context->main_group->n_entries; i++)
753 entry = &context->main_group->entries[i];
754 g_hash_table_insert (shadow_map,
755 (gpointer)entry->long_name,
758 if (seen[(guchar)entry->short_name])
759 entry->short_name = 0;
761 seen[(guchar)entry->short_name] = TRUE;
765 list = context->groups;
768 GOptionGroup *g = list->data;
769 for (i = 0; i < g->n_entries; i++)
771 entry = &g->entries[i];
772 if (g_hash_table_lookup (shadow_map, entry->long_name) &&
773 !(entry->flags & G_OPTION_FLAG_NOALIAS))
774 entry->long_name = g_strdup_printf ("%s-%s", g->name, entry->long_name);
776 g_hash_table_insert (shadow_map, (gpointer)entry->long_name, entry);
778 if (seen[(guchar)entry->short_name] &&
779 !(entry->flags & G_OPTION_FLAG_NOALIAS))
780 entry->short_name = 0;
782 seen[(guchar)entry->short_name] = TRUE;
787 g_hash_table_destroy (shadow_map);
789 list = context->groups;
791 max_length = _g_utf8_strwidth ("-?, --help", -1);
795 len = _g_utf8_strwidth ("--help-all", -1);
796 max_length = MAX (max_length, len);
799 if (context->main_group)
801 len = calculate_max_length (context->main_group);
802 max_length = MAX (max_length, len);
807 GOptionGroup *g = list->data;
809 /* First, we check the --help-<groupname> options */
810 len = _g_utf8_strwidth ("--help-", -1) + _g_utf8_strwidth (g->name, -1);
811 max_length = MAX (max_length, len);
813 /* Then we go through the entries */
814 len = calculate_max_length (g);
815 max_length = MAX (max_length, len);
820 /* Add a bit of padding */
825 list = context->groups;
827 g_string_append_printf (string, "%s\n -%c, --%-*s %s\n",
828 _("Help Options:"), '?', max_length - 4, "help",
829 _("Show help options"));
831 /* We only want --help-all when there are groups */
833 g_string_append_printf (string, " --%-*s %s\n",
834 max_length, "help-all",
835 _("Show all help options"));
839 GOptionGroup *g = list->data;
841 if (group_has_visible_entries (context, g, FALSE))
842 g_string_append_printf (string, " --help-%-*s %s\n",
843 max_length - 5, g->name,
844 TRANSLATE (g, g->help_description));
849 g_string_append (string, "\n");
854 /* Print a certain group */
856 if (group_has_visible_entries (context, group, FALSE))
858 g_string_append (string, TRANSLATE (group, group->description));
859 g_string_append (string, "\n");
860 for (i = 0; i < group->n_entries; i++)
861 print_entry (group, max_length, &group->entries[i], string);
862 g_string_append (string, "\n");
867 /* Print all groups */
869 list = context->groups;
873 GOptionGroup *g = list->data;
875 if (group_has_visible_entries (context, g, FALSE))
877 g_string_append (string, g->description);
878 g_string_append (string, "\n");
879 for (i = 0; i < g->n_entries; i++)
880 if (!(g->entries[i].flags & G_OPTION_FLAG_IN_MAIN))
881 print_entry (g, max_length, &g->entries[i], string);
883 g_string_append (string, "\n");
890 /* Print application options if --help or --help-all has been specified */
891 if ((main_help || !group) &&
892 (group_has_visible_entries (context, context->main_group, TRUE) ||
893 group_list_has_visible_entires (context, context->groups, TRUE)))
895 list = context->groups;
897 g_string_append (string, _("Application Options:"));
898 g_string_append (string, "\n");
899 if (context->main_group)
900 for (i = 0; i < context->main_group->n_entries; i++)
901 print_entry (context->main_group, max_length,
902 &context->main_group->entries[i], string);
906 GOptionGroup *g = list->data;
908 /* Print main entries from other groups */
909 for (i = 0; i < g->n_entries; i++)
910 if (g->entries[i].flags & G_OPTION_FLAG_IN_MAIN)
911 print_entry (g, max_length, &g->entries[i], string);
916 g_string_append (string, "\n");
919 if (context->description)
921 g_string_append (string, TRANSLATE (context, context->description));
922 g_string_append (string, "\n");
925 return g_string_free (string, FALSE);
930 print_help (GOptionContext *context,
936 help = g_option_context_get_help (context, main_help, group);
937 g_print ("%s", help);
944 parse_int (const gchar *arg_name,
953 tmp = strtol (arg, &end, 0);
955 if (*arg == '\0' || *end != '\0')
958 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
959 _("Cannot parse integer value '%s' for %s"),
965 if (*result != tmp || errno == ERANGE)
968 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
969 _("Integer value '%s' for %s out of range"),
979 parse_double (const gchar *arg_name,
988 tmp = g_strtod (arg, &end);
990 if (*arg == '\0' || *end != '\0')
993 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
994 _("Cannot parse double value '%s' for %s"),
1001 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1002 _("Double value '%s' for %s out of range"),
1014 parse_int64 (const gchar *arg_name,
1023 tmp = g_ascii_strtoll (arg, &end, 0);
1025 if (*arg == '\0' || *end != '\0')
1028 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1029 _("Cannot parse integer value '%s' for %s"),
1033 if (errno == ERANGE)
1036 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1037 _("Integer value '%s' for %s out of range"),
1049 get_change (GOptionContext *context,
1050 GOptionArg arg_type,
1054 Change *change = NULL;
1056 for (list = context->changes; list != NULL; list = list->next)
1058 change = list->data;
1060 if (change->arg_data == arg_data)
1064 change = g_new0 (Change, 1);
1065 change->arg_type = arg_type;
1066 change->arg_data = arg_data;
1068 context->changes = g_list_prepend (context->changes, change);
1076 add_pending_null (GOptionContext *context,
1082 n = g_new0 (PendingNull, 1);
1086 context->pending_nulls = g_list_prepend (context->pending_nulls, n);
1090 parse_arg (GOptionContext *context,
1091 GOptionGroup *group,
1092 GOptionEntry *entry,
1094 const gchar *option_name,
1100 g_assert (value || OPTIONAL_ARG (entry) || NO_ARG (entry));
1104 case G_OPTION_ARG_NONE:
1106 change = get_change (context, G_OPTION_ARG_NONE,
1109 *(gboolean *)entry->arg_data = !(entry->flags & G_OPTION_FLAG_REVERSE);
1112 case G_OPTION_ARG_STRING:
1116 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1121 change = get_change (context, G_OPTION_ARG_STRING,
1123 g_free (change->allocated.str);
1125 change->prev.str = *(gchar **)entry->arg_data;
1126 change->allocated.str = data;
1128 *(gchar **)entry->arg_data = data;
1131 case G_OPTION_ARG_STRING_ARRAY:
1135 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1140 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
1143 if (change->allocated.array.len == 0)
1145 change->prev.array = *(gchar ***)entry->arg_data;
1146 change->allocated.array.data = g_new (gchar *, 2);
1149 change->allocated.array.data =
1150 g_renew (gchar *, change->allocated.array.data,
1151 change->allocated.array.len + 2);
1153 change->allocated.array.data[change->allocated.array.len] = data;
1154 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
1156 change->allocated.array.len ++;
1158 *(gchar ***)entry->arg_data = change->allocated.array.data;
1163 case G_OPTION_ARG_FILENAME:
1168 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1173 data = g_strdup (value);
1175 change = get_change (context, G_OPTION_ARG_FILENAME,
1177 g_free (change->allocated.str);
1179 change->prev.str = *(gchar **)entry->arg_data;
1180 change->allocated.str = data;
1182 *(gchar **)entry->arg_data = data;
1186 case G_OPTION_ARG_FILENAME_ARRAY:
1191 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1196 data = g_strdup (value);
1198 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
1201 if (change->allocated.array.len == 0)
1203 change->prev.array = *(gchar ***)entry->arg_data;
1204 change->allocated.array.data = g_new (gchar *, 2);
1207 change->allocated.array.data =
1208 g_renew (gchar *, change->allocated.array.data,
1209 change->allocated.array.len + 2);
1211 change->allocated.array.data[change->allocated.array.len] = data;
1212 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
1214 change->allocated.array.len ++;
1216 *(gchar ***)entry->arg_data = change->allocated.array.data;
1221 case G_OPTION_ARG_INT:
1225 if (!parse_int (option_name, value,
1230 change = get_change (context, G_OPTION_ARG_INT,
1232 change->prev.integer = *(gint *)entry->arg_data;
1233 *(gint *)entry->arg_data = data;
1236 case G_OPTION_ARG_CALLBACK:
1241 if (!value && entry->flags & G_OPTION_FLAG_OPTIONAL_ARG)
1243 else if (entry->flags & G_OPTION_FLAG_NO_ARG)
1245 else if (entry->flags & G_OPTION_FLAG_FILENAME)
1248 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1250 data = g_strdup (value);
1254 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1256 if (!(entry->flags & (G_OPTION_FLAG_NO_ARG|G_OPTION_FLAG_OPTIONAL_ARG)) &&
1260 retval = (* (GOptionArgFunc) entry->arg_data) (option_name, data, group->user_data, error);
1262 if (!retval && error != NULL && *error == NULL)
1264 G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
1265 _("Error parsing option %s"), option_name);
1273 case G_OPTION_ARG_DOUBLE:
1277 if (!parse_double (option_name, value,
1284 change = get_change (context, G_OPTION_ARG_DOUBLE,
1286 change->prev.dbl = *(gdouble *)entry->arg_data;
1287 *(gdouble *)entry->arg_data = data;
1290 case G_OPTION_ARG_INT64:
1294 if (!parse_int64 (option_name, value,
1301 change = get_change (context, G_OPTION_ARG_INT64,
1303 change->prev.int64 = *(gint64 *)entry->arg_data;
1304 *(gint64 *)entry->arg_data = data;
1308 g_assert_not_reached ();
1315 parse_short_option (GOptionContext *context,
1316 GOptionGroup *group,
1327 for (j = 0; j < group->n_entries; j++)
1329 if (arg == group->entries[j].short_name)
1332 gchar *value = NULL;
1334 option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
1336 if (NO_ARG (&group->entries[j]))
1343 G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
1344 _("Error parsing option %s"), option_name);
1345 g_free (option_name);
1349 if (idx < *argc - 1)
1351 if (!OPTIONAL_ARG (&group->entries[j]))
1353 value = (*argv)[idx + 1];
1354 add_pending_null (context, &((*argv)[idx + 1]), NULL);
1359 if ((*argv)[idx + 1][0] == '-')
1363 value = (*argv)[idx + 1];
1364 add_pending_null (context, &((*argv)[idx + 1]), NULL);
1369 else if (idx >= *argc - 1 && OPTIONAL_ARG (&group->entries[j]))
1374 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1375 _("Missing argument for %s"), option_name);
1376 g_free (option_name);
1381 if (!parse_arg (context, group, &group->entries[j],
1382 value, option_name, error))
1384 g_free (option_name);
1388 g_free (option_name);
1397 parse_long_option (GOptionContext *context,
1398 GOptionGroup *group,
1409 for (j = 0; j < group->n_entries; j++)
1414 if (aliased && (group->entries[j].flags & G_OPTION_FLAG_NOALIAS))
1417 if (NO_ARG (&group->entries[j]) &&
1418 strcmp (arg, group->entries[j].long_name) == 0)
1423 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1424 retval = parse_arg (context, group, &group->entries[j],
1425 NULL, option_name, error);
1426 g_free(option_name);
1428 add_pending_null (context, &((*argv)[*idx]), NULL);
1435 gint len = strlen (group->entries[j].long_name);
1437 if (strncmp (arg, group->entries[j].long_name, len) == 0 &&
1438 (arg[len] == '=' || arg[len] == 0))
1440 gchar *value = NULL;
1443 add_pending_null (context, &((*argv)[*idx]), NULL);
1444 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1446 if (arg[len] == '=')
1447 value = arg + len + 1;
1448 else if (*idx < *argc - 1)
1450 if (!(group->entries[j].flags & G_OPTION_FLAG_OPTIONAL_ARG))
1452 value = (*argv)[*idx + 1];
1453 add_pending_null (context, &((*argv)[*idx + 1]), NULL);
1458 if ((*argv)[*idx + 1][0] == '-')
1461 retval = parse_arg (context, group, &group->entries[j],
1462 NULL, option_name, error);
1464 g_free (option_name);
1469 value = (*argv)[*idx + 1];
1470 add_pending_null (context, &((*argv)[*idx + 1]), NULL);
1475 else if (*idx >= *argc - 1 &&
1476 group->entries[j].flags & G_OPTION_FLAG_OPTIONAL_ARG)
1479 retval = parse_arg (context, group, &group->entries[j],
1480 NULL, option_name, error);
1482 g_free (option_name);
1488 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1489 _("Missing argument for %s"), option_name);
1490 g_free (option_name);
1494 if (!parse_arg (context, group, &group->entries[j],
1495 value, option_name, error))
1497 g_free (option_name);
1501 g_free (option_name);
1511 parse_remaining_arg (GOptionContext *context,
1512 GOptionGroup *group,
1521 for (j = 0; j < group->n_entries; j++)
1526 if (group->entries[j].long_name[0])
1529 g_return_val_if_fail (group->entries[j].arg == G_OPTION_ARG_CALLBACK ||
1530 group->entries[j].arg == G_OPTION_ARG_STRING_ARRAY ||
1531 group->entries[j].arg == G_OPTION_ARG_FILENAME_ARRAY, FALSE);
1533 add_pending_null (context, &((*argv)[*idx]), NULL);
1535 if (!parse_arg (context, group, &group->entries[j], (*argv)[*idx], "", error))
1546 free_changes_list (GOptionContext *context,
1551 for (list = context->changes; list != NULL; list = list->next)
1553 Change *change = list->data;
1557 switch (change->arg_type)
1559 case G_OPTION_ARG_NONE:
1560 *(gboolean *)change->arg_data = change->prev.bool;
1562 case G_OPTION_ARG_INT:
1563 *(gint *)change->arg_data = change->prev.integer;
1565 case G_OPTION_ARG_STRING:
1566 case G_OPTION_ARG_FILENAME:
1567 g_free (change->allocated.str);
1568 *(gchar **)change->arg_data = change->prev.str;
1570 case G_OPTION_ARG_STRING_ARRAY:
1571 case G_OPTION_ARG_FILENAME_ARRAY:
1572 g_strfreev (change->allocated.array.data);
1573 *(gchar ***)change->arg_data = change->prev.array;
1575 case G_OPTION_ARG_DOUBLE:
1576 *(gdouble *)change->arg_data = change->prev.dbl;
1578 case G_OPTION_ARG_INT64:
1579 *(gint64 *)change->arg_data = change->prev.int64;
1582 g_assert_not_reached ();
1589 g_list_free (context->changes);
1590 context->changes = NULL;
1594 free_pending_nulls (GOptionContext *context,
1595 gboolean perform_nulls)
1599 for (list = context->pending_nulls; list != NULL; list = list->next)
1601 PendingNull *n = list->data;
1607 /* Copy back the short options */
1609 strcpy (*n->ptr + 1, n->value);
1619 g_list_free (context->pending_nulls);
1620 context->pending_nulls = NULL;
1624 * g_option_context_parse:
1625 * @context: a #GOptionContext
1626 * @argc: a pointer to the number of command line arguments
1627 * @argv: a pointer to the array of command line arguments
1628 * @error: a return location for errors
1630 * Parses the command line arguments, recognizing options
1631 * which have been added to @context. A side-effect of
1632 * calling this function is that g_set_prgname() will be
1635 * If the parsing is successful, any parsed arguments are
1636 * removed from the array and @argc and @argv are updated
1637 * accordingly. A '--' option is stripped from @argv
1638 * unless there are unparsed options before and after it,
1639 * or some of the options after it start with '-'. In case
1640 * of an error, @argc and @argv are left unmodified.
1642 * If automatic <option>--help</option> support is enabled
1643 * (see g_option_context_set_help_enabled()), and the
1644 * @argv array contains one of the recognized help options,
1645 * this function will produce help output to stdout and
1646 * call <literal>exit (0)</literal>.
1648 * Note that function depends on the
1649 * <link linkend="setlocale">current locale</link> for
1650 * automatic character set conversion of string and filename
1653 * Return value: %TRUE if the parsing was successful,
1654 * %FALSE if an error occurred
1659 g_option_context_parse (GOptionContext *context,
1667 /* Set program name */
1668 if (!g_get_prgname())
1670 if (argc && argv && *argc)
1674 prgname = g_path_get_basename ((*argv)[0]);
1675 g_set_prgname (prgname);
1679 g_set_prgname ("<unknown>");
1682 /* Call pre-parse hooks */
1683 list = context->groups;
1686 GOptionGroup *group = list->data;
1688 if (group->pre_parse_func)
1690 if (!(* group->pre_parse_func) (context, group,
1691 group->user_data, error))
1698 if (context->main_group && context->main_group->pre_parse_func)
1700 if (!(* context->main_group->pre_parse_func) (context, context->main_group,
1701 context->main_group->user_data, error))
1707 gboolean stop_parsing = FALSE;
1708 gboolean has_unknown = FALSE;
1709 gint separator_pos = 0;
1711 for (i = 1; i < *argc; i++)
1714 gboolean parsed = FALSE;
1716 if ((*argv)[i][0] == '-' && (*argv)[i][1] != '\0' && !stop_parsing)
1718 if ((*argv)[i][1] == '-')
1722 arg = (*argv)[i] + 2;
1724 /* '--' terminates list of arguments */
1728 stop_parsing = TRUE;
1732 /* Handle help options */
1733 if (context->help_enabled)
1735 if (strcmp (arg, "help") == 0)
1736 print_help (context, TRUE, NULL);
1737 else if (strcmp (arg, "help-all") == 0)
1738 print_help (context, FALSE, NULL);
1739 else if (strncmp (arg, "help-", 5) == 0)
1741 list = context->groups;
1745 GOptionGroup *group = list->data;
1747 if (strcmp (arg + 5, group->name) == 0)
1748 print_help (context, FALSE, group);
1755 if (context->main_group &&
1756 !parse_long_option (context, context->main_group, &i, arg,
1757 FALSE, argc, argv, error, &parsed))
1763 /* Try the groups */
1764 list = context->groups;
1767 GOptionGroup *group = list->data;
1769 if (!parse_long_option (context, group, &i, arg,
1770 FALSE, argc, argv, error, &parsed))
1782 /* Now look for --<group>-<option> */
1783 dash = strchr (arg, '-');
1786 /* Try the groups */
1787 list = context->groups;
1790 GOptionGroup *group = list->data;
1792 if (strncmp (group->name, arg, dash - arg) == 0)
1794 if (!parse_long_option (context, group, &i, dash + 1,
1795 TRUE, argc, argv, error, &parsed))
1806 if (context->ignore_unknown)
1810 { /* short option */
1811 gint new_i = i, arg_length;
1812 gboolean *nulled_out = NULL;
1813 arg = (*argv)[i] + 1;
1814 arg_length = strlen (arg);
1815 nulled_out = g_newa (gboolean, arg_length);
1816 memset (nulled_out, 0, arg_length * sizeof (gboolean));
1817 for (j = 0; j < arg_length; j++)
1819 if (context->help_enabled && arg[j] == '?')
1820 print_help (context, TRUE, NULL);
1822 if (context->main_group &&
1823 !parse_short_option (context, context->main_group,
1825 argc, argv, error, &parsed))
1829 /* Try the groups */
1830 list = context->groups;
1833 GOptionGroup *group = list->data;
1834 if (!parse_short_option (context, group, i, &new_i, arg[j],
1835 argc, argv, error, &parsed))
1843 if (context->ignore_unknown && parsed)
1844 nulled_out[j] = TRUE;
1845 else if (context->ignore_unknown)
1849 /* !context->ignore_unknown && parsed */
1851 if (context->ignore_unknown)
1853 gchar *new_arg = NULL;
1855 for (j = 0; j < arg_length; j++)
1860 new_arg = g_malloc (arg_length + 1);
1861 new_arg[arg_index++] = arg[j];
1865 new_arg[arg_index] = '\0';
1866 add_pending_null (context, &((*argv)[i]), new_arg);
1870 add_pending_null (context, &((*argv)[i]), NULL);
1878 if (!parsed && !context->ignore_unknown)
1881 G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
1882 _("Unknown option %s"), (*argv)[i]);
1888 /* Collect remaining args */
1889 if (context->main_group &&
1890 !parse_remaining_arg (context, context->main_group, &i,
1891 argc, argv, error, &parsed))
1894 if (!parsed && (has_unknown || (*argv)[i][0] == '-'))
1899 if (separator_pos > 0)
1900 add_pending_null (context, &((*argv)[separator_pos]), NULL);
1904 /* Call post-parse hooks */
1905 list = context->groups;
1908 GOptionGroup *group = list->data;
1910 if (group->post_parse_func)
1912 if (!(* group->post_parse_func) (context, group,
1913 group->user_data, error))
1920 if (context->main_group && context->main_group->post_parse_func)
1922 if (!(* context->main_group->post_parse_func) (context, context->main_group,
1923 context->main_group->user_data, error))
1929 free_pending_nulls (context, TRUE);
1931 for (i = 1; i < *argc; i++)
1933 for (k = i; k < *argc; k++)
1934 if ((*argv)[k] != NULL)
1940 for (j = i + k; j < *argc; j++)
1942 (*argv)[j-k] = (*argv)[j];
1954 /* Call error hooks */
1955 list = context->groups;
1958 GOptionGroup *group = list->data;
1960 if (group->error_func)
1961 (* group->error_func) (context, group,
1962 group->user_data, error);
1967 if (context->main_group && context->main_group->error_func)
1968 (* context->main_group->error_func) (context, context->main_group,
1969 context->main_group->user_data, error);
1971 free_changes_list (context, TRUE);
1972 free_pending_nulls (context, FALSE);
1978 * g_option_group_new:
1979 * @name: the name for the option group, this is used to provide
1980 * help for the options in this group with <option>--help-</option>@name
1981 * @description: a description for this group to be shown in
1982 * <option>--help</option>. This string is translated using the translation
1983 * domain or translation function of the group
1984 * @help_description: a description for the <option>--help-</option>@name option.
1985 * This string is translated using the translation domain or translation function
1987 * @user_data: user data that will be passed to the pre- and post-parse hooks,
1988 * the error hook and to callbacks of %G_OPTION_ARG_CALLBACK options, or %NULL
1989 * @destroy: a function that will be called to free @user_data, or %NULL
1991 * Creates a new #GOptionGroup.
1993 * Return value: a newly created option group. It should be added
1994 * to a #GOptionContext or freed with g_option_group_free().
1999 g_option_group_new (const gchar *name,
2000 const gchar *description,
2001 const gchar *help_description,
2003 GDestroyNotify destroy)
2006 GOptionGroup *group;
2008 group = g_new0 (GOptionGroup, 1);
2009 group->name = g_strdup (name);
2010 group->description = g_strdup (description);
2011 group->help_description = g_strdup (help_description);
2012 group->user_data = user_data;
2013 group->destroy_notify = destroy;
2020 * g_option_group_free:
2021 * @group: a #GOptionGroup
2023 * Frees a #GOptionGroup. Note that you must <emphasis>not</emphasis>
2024 * free groups which have been added to a #GOptionContext.
2029 g_option_group_free (GOptionGroup *group)
2031 g_return_if_fail (group != NULL);
2033 g_free (group->name);
2034 g_free (group->description);
2035 g_free (group->help_description);
2037 g_free (group->entries);
2039 if (group->destroy_notify)
2040 (* group->destroy_notify) (group->user_data);
2042 if (group->translate_notify)
2043 (* group->translate_notify) (group->translate_data);
2050 * g_option_group_add_entries:
2051 * @group: a #GOptionGroup
2052 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
2054 * Adds the options specified in @entries to @group.
2059 g_option_group_add_entries (GOptionGroup *group,
2060 const GOptionEntry *entries)
2064 g_return_if_fail (entries != NULL);
2066 for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++) ;
2068 group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
2070 memcpy (group->entries + group->n_entries, entries, sizeof (GOptionEntry) * n_entries);
2072 for (i = group->n_entries; i < group->n_entries + n_entries; i++)
2074 gchar c = group->entries[i].short_name;
2078 if (c == '-' || !g_ascii_isprint (c))
2080 g_warning (G_STRLOC": ignoring invalid short option '%c' (%d)", c, c);
2081 group->entries[i].short_name = 0;
2086 group->n_entries += n_entries;
2090 * g_option_group_set_parse_hooks:
2091 * @group: a #GOptionGroup
2092 * @pre_parse_func: a function to call before parsing, or %NULL
2093 * @post_parse_func: a function to call after parsing, or %NULL
2095 * Associates two functions with @group which will be called
2096 * from g_option_context_parse() before the first option is parsed
2097 * and after the last option has been parsed, respectively.
2099 * Note that the user data to be passed to @pre_parse_func and
2100 * @post_parse_func can be specified when constructing the group
2101 * with g_option_group_new().
2106 g_option_group_set_parse_hooks (GOptionGroup *group,
2107 GOptionParseFunc pre_parse_func,
2108 GOptionParseFunc post_parse_func)
2110 g_return_if_fail (group != NULL);
2112 group->pre_parse_func = pre_parse_func;
2113 group->post_parse_func = post_parse_func;
2117 * g_option_group_set_error_hook:
2118 * @group: a #GOptionGroup
2119 * @error_func: a function to call when an error occurs
2121 * Associates a function with @group which will be called
2122 * from g_option_context_parse() when an error occurs.
2124 * Note that the user data to be passed to @error_func can be
2125 * specified when constructing the group with g_option_group_new().
2130 g_option_group_set_error_hook (GOptionGroup *group,
2131 GOptionErrorFunc error_func)
2133 g_return_if_fail (group != NULL);
2135 group->error_func = error_func;
2140 * g_option_group_set_translate_func:
2141 * @group: a #GOptionGroup
2142 * @func: the #GTranslateFunc, or %NULL
2143 * @data: user data to pass to @func, or %NULL
2144 * @destroy_notify: a function which gets called to free @data, or %NULL
2146 * Sets the function which is used to translate user-visible
2147 * strings, for <option>--help</option> output. Different
2148 * groups can use different #GTranslateFunc<!-- -->s. If @func
2149 * is %NULL, strings are not translated.
2151 * If you are using gettext(), you only need to set the translation
2152 * domain, see g_option_group_set_translation_domain().
2157 g_option_group_set_translate_func (GOptionGroup *group,
2158 GTranslateFunc func,
2160 GDestroyNotify destroy_notify)
2162 g_return_if_fail (group != NULL);
2164 if (group->translate_notify)
2165 group->translate_notify (group->translate_data);
2167 group->translate_func = func;
2168 group->translate_data = data;
2169 group->translate_notify = destroy_notify;
2172 static const gchar *
2173 dgettext_swapped (const gchar *msgid,
2174 const gchar *domainname)
2176 return g_dgettext (domainname, msgid);
2180 * g_option_group_set_translation_domain:
2181 * @group: a #GOptionGroup
2182 * @domain: the domain to use
2184 * A convenience function to use gettext() for translating
2185 * user-visible strings.
2190 g_option_group_set_translation_domain (GOptionGroup *group,
2191 const gchar *domain)
2193 g_return_if_fail (group != NULL);
2195 g_option_group_set_translate_func (group,
2196 (GTranslateFunc)dgettext_swapped,
2202 * g_option_context_set_translate_func:
2203 * @context: a #GOptionContext
2204 * @func: the #GTranslateFunc, or %NULL
2205 * @data: user data to pass to @func, or %NULL
2206 * @destroy_notify: a function which gets called to free @data, or %NULL
2208 * Sets the function which is used to translate the contexts
2209 * user-visible strings, for <option>--help</option> output.
2210 * If @func is %NULL, strings are not translated.
2212 * Note that option groups have their own translation functions,
2213 * this function only affects the @parameter_string (see g_option_context_new()),
2214 * the summary (see g_option_context_set_summary()) and the description
2215 * (see g_option_context_set_description()).
2217 * If you are using gettext(), you only need to set the translation
2218 * domain, see g_option_context_set_translation_domain().
2223 g_option_context_set_translate_func (GOptionContext *context,
2224 GTranslateFunc func,
2226 GDestroyNotify destroy_notify)
2228 g_return_if_fail (context != NULL);
2230 if (context->translate_notify)
2231 context->translate_notify (context->translate_data);
2233 context->translate_func = func;
2234 context->translate_data = data;
2235 context->translate_notify = destroy_notify;
2239 * g_option_context_set_translation_domain:
2240 * @context: a #GOptionContext
2241 * @domain: the domain to use
2243 * A convenience function to use gettext() for translating
2244 * user-visible strings.
2249 g_option_context_set_translation_domain (GOptionContext *context,
2250 const gchar *domain)
2252 g_return_if_fail (context != NULL);
2254 g_option_context_set_translate_func (context,
2255 (GTranslateFunc)dgettext_swapped,
2261 * g_option_context_set_summary:
2262 * @context: a #GOptionContext
2263 * @summary: a string to be shown in <option>--help</option> output
2264 * before the list of options, or %NULL
2266 * Adds a string to be displayed in <option>--help</option> output
2267 * before the list of options. This is typically a summary of the
2268 * program functionality.
2270 * Note that the summary is translated (see
2271 * g_option_context_set_translate_func() and
2272 * g_option_context_set_translation_domain()).
2277 g_option_context_set_summary (GOptionContext *context,
2278 const gchar *summary)
2280 g_return_if_fail (context != NULL);
2282 g_free (context->summary);
2283 context->summary = g_strdup (summary);
2288 * g_option_context_get_summary:
2289 * @context: a #GOptionContext
2291 * Returns the summary. See g_option_context_set_summary().
2293 * Returns: the summary
2297 G_CONST_RETURN gchar *
2298 g_option_context_get_summary (GOptionContext *context)
2300 g_return_val_if_fail (context != NULL, NULL);
2302 return context->summary;
2306 * g_option_context_set_description:
2307 * @context: a #GOptionContext
2308 * @description: a string to be shown in <option>--help</option> output
2309 * after the list of options, or %NULL
2311 * Adds a string to be displayed in <option>--help</option> output
2312 * after the list of options. This text often includes a bug reporting
2315 * Note that the summary is translated (see
2316 * g_option_context_set_translate_func()).
2321 g_option_context_set_description (GOptionContext *context,
2322 const gchar *description)
2324 g_return_if_fail (context != NULL);
2326 g_free (context->description);
2327 context->description = g_strdup (description);
2332 * g_option_context_get_description:
2333 * @context: a #GOptionContext
2335 * Returns the description. See g_option_context_set_description().
2337 * Returns: the description
2341 G_CONST_RETURN gchar *
2342 g_option_context_get_description (GOptionContext *context)
2344 g_return_val_if_fail (context != NULL, NULL);
2346 return context->description;
2350 #define __G_OPTION_C__
2351 #include "galiasdef.c"