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, see <http://www.gnu.org/licenses/>.
22 * @Short_description: parses commandline options
23 * @Title: Commandline option parser
25 * The GOption commandline parser is intended to be a simpler replacement
26 * for the popt library. It supports short and long commandline options,
27 * as shown in the following example:
29 * <literal>testtreemodel -r 1 --max-size 20 --rand --display=:1.0 -vb -- file1 file2</literal>
31 * The example demonstrates a number of features of the GOption
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
40 * a filename. For long options, the extra argument can be appended with
41 * an equals sign after the option name, which is useful if the extra
42 * argument starts with a dash, which would otherwise cause it to be
43 * interpreted as another option.
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
53 * generate nicely formatted help output. Unless it is explicitly turned
54 * off with g_option_context_set_help_enabled(), GOption will recognize
55 * the <option>--help</option>, <option>-?</option>,
56 * <option>--help-all</option> and
57 * <option>--help-</option><replaceable>groupname</replaceable> options
58 * (where <replaceable>groupname</replaceable> is the name of a
59 * #GOptionGroup) and write a text similar to the one shown in the
60 * following example to stdout.
62 * <informalexample><screen>
64 * testtreemodel [OPTION...] - test tree model performance
67 * -h, --help Show help options
68 * --help-all Show all help options
69 * --help-gtk Show GTK+ Options
71 * Application Options:
72 * -r, --repeats=N Average over N repetitions
73 * -m, --max-size=M Test up to 2^M items
74 * --display=DISPLAY X display to use
75 * -v, --verbose Be verbose
76 * -b, --beep Beep when done
77 * --rand Randomize the data
78 * </screen></informalexample>
80 * GOption groups options in #GOptionGroup<!-- -->s, which makes it easy to
81 * incorporate options from multiple sources. The intended use for this is
82 * to let applications collect option groups from the libraries it uses,
83 * add them to their #GOptionContext, and parse all options by a single call
84 * to g_option_context_parse(). See gtk_get_option_group() for an example.
86 * If an option is declared to be of type string or filename, GOption takes
87 * care of converting it to the right encoding; strings are returned in
88 * UTF-8, filenames are returned in the GLib filename encoding. Note that
89 * this only works if setlocale() has been called before
90 * g_option_context_parse().
92 * Here is a complete example of setting up GOption to parse the example
93 * commandline above and produce the example help output.
95 * <informalexample><programlisting>
96 * static gint repeats = 2;
97 * static gint max_size = 8;
98 * static gboolean verbose = FALSE;
99 * static gboolean beep = FALSE;
100 * static gboolean randomize = FALSE;
102 * static GOptionEntry entries[] =
104 * { "repeats", 'r', 0, G_OPTION_ARG_INT, &repeats, "Average over N repetitions", "N" },
105 * { "max-size", 'm', 0, G_OPTION_ARG_INT, &max_size, "Test up to 2^M items", "M" },
106 * { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL },
107 * { "beep", 'b', 0, G_OPTION_ARG_NONE, &beep, "Beep when done", NULL },
108 * { "rand", 0, 0, G_OPTION_ARG_NONE, &randomize, "Randomize the data", NULL },
113 * main (int argc, char *argv[])
115 * GError *error = NULL;
116 * GOptionContext *context;
118 * context = g_option_context_new ("- test tree model performance");
119 * g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
120 * g_option_context_add_group (context, gtk_get_option_group (TRUE));
121 * if (!g_option_context_parse (context, &argc, &argv, &error))
123 * g_print ("option parsing failed: %s\n", error->message);
130 * </programlisting></informalexample>
132 * On UNIX systems, the argv that is passed to main() has no particular
133 * encoding, even to the extent that different parts of it may have
134 * different encodings. In general, normal arguments and flags will be
135 * in the current locale and filenames should be considered to be opaque
136 * byte strings. Proper use of %G_OPTION_ARG_FILENAME vs
137 * %G_OPTION_ARG_STRING is therefore important.
139 * Note that on Windows, filenames do have an encoding, but using
140 * #GOptionContext with the argv as passed to main() will result in a
141 * program that can only accept commandline arguments with characters
142 * from the system codepage. This can cause problems when attempting to
143 * deal with filenames containing Unicode characters that fall outside
146 * A solution to this is to use g_win32_get_command_line() and
147 * g_option_context_parse_strv() which will properly handle full Unicode
148 * filenames. If you are using #GApplication, this is done
149 * automatically for you.
151 * The following example shows how you can use #GOptionContext directly
152 * in order to correctly deal with Unicode filenames on Windows:
156 * main (int argc, char **argv)
158 * GError *error = NULL;
159 * GOptionContext *context;
163 * args = g_win32_get_command_line ();
165 * args = g_strdupv (argv);
168 * /* ... setup context ... */
170 * if (!g_option_context_parse_strv (context, &args, &error))
172 * /* ... error ... */
191 #if defined __OpenBSD__
192 #include <sys/types.h>
194 #include <sys/param.h>
195 #include <sys/sysctl.h>
201 #include "glibintl.h"
203 #define TRANSLATE(group, str) (((group)->translate_func ? (* (group)->translate_func) ((str), (group)->translate_data) : (str)))
205 #define NO_ARG(entry) ((entry)->arg == G_OPTION_ARG_NONE || \
206 ((entry)->arg == G_OPTION_ARG_CALLBACK && \
207 ((entry)->flags & G_OPTION_FLAG_NO_ARG)))
209 #define OPTIONAL_ARG(entry) ((entry)->arg == G_OPTION_ARG_CALLBACK && \
210 (entry)->flags & G_OPTION_FLAG_OPTIONAL_ARG)
242 struct _GOptionContext
246 gchar *parameter_string;
250 GTranslateFunc translate_func;
251 GDestroyNotify translate_notify;
252 gpointer translate_data;
254 guint help_enabled : 1;
255 guint ignore_unknown : 1;
258 GOptionGroup *main_group;
260 /* We keep a list of change so we can revert them */
263 /* We also keep track of all argv elements
264 * that should be NULLed or modified.
266 GList *pending_nulls;
273 gchar *help_description;
275 GDestroyNotify destroy_notify;
278 GTranslateFunc translate_func;
279 GDestroyNotify translate_notify;
280 gpointer translate_data;
282 GOptionEntry *entries;
285 GOptionParseFunc pre_parse_func;
286 GOptionParseFunc post_parse_func;
287 GOptionErrorFunc error_func;
290 static void free_changes_list (GOptionContext *context,
292 static void free_pending_nulls (GOptionContext *context,
293 gboolean perform_nulls);
297 _g_unichar_get_width (gunichar c)
299 if (G_UNLIKELY (g_unichar_iszerowidth (c)))
302 /* we ignore the fact that we should call g_unichar_iswide_cjk() under
303 * some locales (legacy East Asian ones) */
304 if (g_unichar_iswide (c))
311 _g_utf8_strwidth (const gchar *p)
314 g_return_val_if_fail (p != NULL, 0);
318 len += _g_unichar_get_width (g_utf8_get_char (p));
319 p = g_utf8_next_char (p);
325 G_DEFINE_QUARK (g-option-context-error-quark, g_option_error)
328 * g_option_context_new:
329 * @parameter_string: (allow-none): a string which is displayed in
330 * the first line of <option>--help</option> output, after the
332 * <literal><replaceable>programname</replaceable> [OPTION...]</literal>
334 * Creates a new option context.
336 * The @parameter_string can serve multiple purposes. It can be used
337 * to add descriptions for "rest" arguments, which are not parsed by
338 * the #GOptionContext, typically something like "FILES" or
339 * "FILE1 FILE2...". If you are using #G_OPTION_REMAINING for
340 * collecting "rest" arguments, GLib handles this automatically by
341 * using the @arg_description of the corresponding #GOptionEntry in
344 * Another usage is to give a short summary of the program
345 * functionality, like " - frob the strings", which will be displayed
346 * in the same line as the usage. For a longer description of the
347 * program functionality that should be displayed as a paragraph
348 * below the usage line, use g_option_context_set_summary().
350 * Note that the @parameter_string is translated using the
351 * function set with g_option_context_set_translate_func(), so
352 * it should normally be passed untranslated.
354 * Returns: a newly created #GOptionContext, which must be
355 * freed with g_option_context_free() after use.
360 g_option_context_new (const gchar *parameter_string)
363 GOptionContext *context;
365 context = g_new0 (GOptionContext, 1);
367 context->parameter_string = g_strdup (parameter_string);
368 context->help_enabled = TRUE;
369 context->ignore_unknown = FALSE;
375 * g_option_context_free:
376 * @context: a #GOptionContext
378 * Frees context and all the groups which have been
381 * Please note that parsed arguments need to be freed separately (see
386 void g_option_context_free (GOptionContext *context)
388 g_return_if_fail (context != NULL);
390 g_list_free_full (context->groups, (GDestroyNotify) g_option_group_free);
392 if (context->main_group)
393 g_option_group_free (context->main_group);
395 free_changes_list (context, FALSE);
396 free_pending_nulls (context, FALSE);
398 g_free (context->parameter_string);
399 g_free (context->summary);
400 g_free (context->description);
402 if (context->translate_notify)
403 (* context->translate_notify) (context->translate_data);
410 * g_option_context_set_help_enabled:
411 * @context: a #GOptionContext
412 * @help_enabled: %TRUE to enable <option>--help</option>, %FALSE to disable it
414 * Enables or disables automatic generation of <option>--help</option>
415 * output. By default, g_option_context_parse() recognizes
416 * <option>--help</option>, <option>-h</option>,
417 * <option>-?</option>, <option>--help-all</option>
418 * and <option>--help-</option><replaceable>groupname</replaceable> and creates
419 * suitable output to stdout.
423 void g_option_context_set_help_enabled (GOptionContext *context,
424 gboolean help_enabled)
427 g_return_if_fail (context != NULL);
429 context->help_enabled = help_enabled;
433 * g_option_context_get_help_enabled:
434 * @context: a #GOptionContext
436 * Returns whether automatic <option>--help</option> generation
437 * is turned on for @context. See g_option_context_set_help_enabled().
439 * Returns: %TRUE if automatic help generation is turned on.
444 g_option_context_get_help_enabled (GOptionContext *context)
446 g_return_val_if_fail (context != NULL, FALSE);
448 return context->help_enabled;
452 * g_option_context_set_ignore_unknown_options:
453 * @context: a #GOptionContext
454 * @ignore_unknown: %TRUE to ignore unknown options, %FALSE to produce
455 * an error when unknown options are met
457 * Sets whether to ignore unknown options or not. If an argument is
458 * ignored, it is left in the @argv array after parsing. By default,
459 * g_option_context_parse() treats unknown options as error.
461 * This setting does not affect non-option arguments (i.e. arguments
462 * which don't start with a dash). But note that GOption cannot reliably
463 * determine whether a non-option belongs to a preceding unknown option.
468 g_option_context_set_ignore_unknown_options (GOptionContext *context,
469 gboolean ignore_unknown)
471 g_return_if_fail (context != NULL);
473 context->ignore_unknown = ignore_unknown;
477 * g_option_context_get_ignore_unknown_options:
478 * @context: a #GOptionContext
480 * Returns whether unknown options are ignored or not. See
481 * g_option_context_set_ignore_unknown_options().
483 * Returns: %TRUE if unknown options are ignored.
488 g_option_context_get_ignore_unknown_options (GOptionContext *context)
490 g_return_val_if_fail (context != NULL, FALSE);
492 return context->ignore_unknown;
496 * g_option_context_add_group:
497 * @context: a #GOptionContext
498 * @group: the group to add
500 * Adds a #GOptionGroup to the @context, so that parsing with @context
501 * will recognize the options in the group. Note that the group will
502 * be freed together with the context when g_option_context_free() is
503 * called, so you must not free the group yourself after adding it
509 g_option_context_add_group (GOptionContext *context,
514 g_return_if_fail (context != NULL);
515 g_return_if_fail (group != NULL);
516 g_return_if_fail (group->name != NULL);
517 g_return_if_fail (group->description != NULL);
518 g_return_if_fail (group->help_description != NULL);
520 for (list = context->groups; list; list = list->next)
522 GOptionGroup *g = (GOptionGroup *)list->data;
524 if ((group->name == NULL && g->name == NULL) ||
525 (group->name && g->name && strcmp (group->name, g->name) == 0))
526 g_warning ("A group named \"%s\" is already part of this GOptionContext",
530 context->groups = g_list_append (context->groups, group);
534 * g_option_context_set_main_group:
535 * @context: a #GOptionContext
536 * @group: the group to set as main group
538 * Sets a #GOptionGroup as main group of the @context.
539 * This has the same effect as calling g_option_context_add_group(),
540 * the only difference is that the options in the main group are
541 * treated differently when generating <option>--help</option> output.
546 g_option_context_set_main_group (GOptionContext *context,
549 g_return_if_fail (context != NULL);
550 g_return_if_fail (group != NULL);
552 if (context->main_group)
554 g_warning ("This GOptionContext already has a main group");
559 context->main_group = group;
563 * g_option_context_get_main_group:
564 * @context: a #GOptionContext
566 * Returns a pointer to the main group of @context.
568 * Return value: the main group of @context, or %NULL if @context doesn't
569 * have a main group. Note that group belongs to @context and should
570 * not be modified or freed.
575 g_option_context_get_main_group (GOptionContext *context)
577 g_return_val_if_fail (context != NULL, NULL);
579 return context->main_group;
583 * g_option_context_add_main_entries:
584 * @context: a #GOptionContext
585 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
586 * @translation_domain: (allow-none): a translation domain to use for translating
587 * the <option>--help</option> output for the options in @entries
588 * with gettext(), or %NULL
590 * A convenience function which creates a main group if it doesn't
591 * exist, adds the @entries to it and sets the translation domain.
596 g_option_context_add_main_entries (GOptionContext *context,
597 const GOptionEntry *entries,
598 const gchar *translation_domain)
600 g_return_if_fail (entries != NULL);
602 if (!context->main_group)
603 context->main_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
605 g_option_group_add_entries (context->main_group, entries);
606 g_option_group_set_translation_domain (context->main_group, translation_domain);
610 calculate_max_length (GOptionGroup *group,
614 gint i, len, max_length;
615 const gchar *long_name;
619 for (i = 0; i < group->n_entries; i++)
621 entry = &group->entries[i];
623 if (entry->flags & G_OPTION_FLAG_HIDDEN)
626 long_name = g_hash_table_lookup (aliases, &entry->long_name);
628 long_name = entry->long_name;
629 len = _g_utf8_strwidth (long_name);
631 if (entry->short_name)
634 if (!NO_ARG (entry) && entry->arg_description)
635 len += 1 + _g_utf8_strwidth (TRANSLATE (group, entry->arg_description));
637 max_length = MAX (max_length, len);
644 print_entry (GOptionGroup *group,
646 const GOptionEntry *entry,
651 const gchar *long_name;
653 if (entry->flags & G_OPTION_FLAG_HIDDEN)
656 if (entry->long_name[0] == 0)
659 long_name = g_hash_table_lookup (aliases, &entry->long_name);
661 long_name = entry->long_name;
663 str = g_string_new (NULL);
665 if (entry->short_name)
666 g_string_append_printf (str, " -%c, --%s", entry->short_name, long_name);
668 g_string_append_printf (str, " --%s", long_name);
670 if (entry->arg_description)
671 g_string_append_printf (str, "=%s", TRANSLATE (group, entry->arg_description));
673 g_string_append_printf (string, "%s%*s %s\n", str->str,
674 (int) (max_length + 4 - _g_utf8_strwidth (str->str)), "",
675 entry->description ? TRANSLATE (group, entry->description) : "");
676 g_string_free (str, TRUE);
680 group_has_visible_entries (GOptionContext *context,
682 gboolean main_entries)
684 GOptionFlags reject_filter = G_OPTION_FLAG_HIDDEN;
687 gboolean main_group = group == context->main_group;
690 reject_filter |= G_OPTION_FLAG_IN_MAIN;
692 for (i = 0, l = (group ? group->n_entries : 0); i < l; i++)
694 entry = &group->entries[i];
696 if (main_entries && !main_group && !(entry->flags & G_OPTION_FLAG_IN_MAIN))
698 if (entry->long_name[0] == 0) /* ignore rest entry */
700 if (!(entry->flags & reject_filter))
708 group_list_has_visible_entries (GOptionContext *context,
710 gboolean main_entries)
714 if (group_has_visible_entries (context, group_list->data, main_entries))
717 group_list = group_list->next;
724 context_has_h_entry (GOptionContext *context)
729 if (context->main_group)
731 for (i = 0; i < context->main_group->n_entries; i++)
733 if (context->main_group->entries[i].short_name == 'h')
738 for (list = context->groups; list != NULL; list = g_list_next (list))
742 group = (GOptionGroup*)list->data;
743 for (i = 0; i < group->n_entries; i++)
745 if (group->entries[i].short_name == 'h')
753 * g_option_context_get_help:
754 * @context: a #GOptionContext
755 * @main_help: if %TRUE, only include the main group
756 * @group: (allow-none): the #GOptionGroup to create help for, or %NULL
758 * Returns a formatted, translated help text for the given context.
759 * To obtain the text produced by <option>--help</option>, call
760 * <literal>g_option_context_get_help (context, TRUE, NULL)</literal>.
761 * To obtain the text produced by <option>--help-all</option>, call
762 * <literal>g_option_context_get_help (context, FALSE, NULL)</literal>.
763 * To obtain the help text for an option group, call
764 * <literal>g_option_context_get_help (context, FALSE, group)</literal>.
766 * Returns: A newly allocated string containing the help text
771 g_option_context_get_help (GOptionContext *context,
776 gint max_length = 0, len;
779 GHashTable *shadow_map;
782 const gchar *rest_description;
786 string = g_string_sized_new (1024);
788 rest_description = NULL;
789 if (context->main_group)
792 for (i = 0; i < context->main_group->n_entries; i++)
794 entry = &context->main_group->entries[i];
795 if (entry->long_name[0] == 0)
797 rest_description = TRANSLATE (context->main_group, entry->arg_description);
803 g_string_append_printf (string, "%s\n %s %s",
804 _("Usage:"), g_get_prgname(), _("[OPTION...]"));
806 if (rest_description)
808 g_string_append (string, " ");
809 g_string_append (string, rest_description);
812 if (context->parameter_string)
814 g_string_append (string, " ");
815 g_string_append (string, TRANSLATE (context, context->parameter_string));
818 g_string_append (string, "\n\n");
820 if (context->summary)
822 g_string_append (string, TRANSLATE (context, context->summary));
823 g_string_append (string, "\n\n");
826 memset (seen, 0, sizeof (gboolean) * 256);
827 shadow_map = g_hash_table_new (g_str_hash, g_str_equal);
828 aliases = g_hash_table_new_full (NULL, NULL, NULL, g_free);
830 if (context->main_group)
832 for (i = 0; i < context->main_group->n_entries; i++)
834 entry = &context->main_group->entries[i];
835 g_hash_table_insert (shadow_map,
836 (gpointer)entry->long_name,
839 if (seen[(guchar)entry->short_name])
840 entry->short_name = 0;
842 seen[(guchar)entry->short_name] = TRUE;
846 list = context->groups;
849 GOptionGroup *g = list->data;
850 for (i = 0; i < g->n_entries; i++)
852 entry = &g->entries[i];
853 if (g_hash_table_lookup (shadow_map, entry->long_name) &&
854 !(entry->flags & G_OPTION_FLAG_NOALIAS))
856 g_hash_table_insert (aliases, &entry->long_name,
857 g_strdup_printf ("%s-%s", g->name, entry->long_name));
860 g_hash_table_insert (shadow_map, (gpointer)entry->long_name, entry);
862 if (seen[(guchar)entry->short_name] &&
863 !(entry->flags & G_OPTION_FLAG_NOALIAS))
864 entry->short_name = 0;
866 seen[(guchar)entry->short_name] = TRUE;
871 g_hash_table_destroy (shadow_map);
873 list = context->groups;
875 if (context->help_enabled)
877 max_length = _g_utf8_strwidth ("-?, --help");
881 len = _g_utf8_strwidth ("--help-all");
882 max_length = MAX (max_length, len);
886 if (context->main_group)
888 len = calculate_max_length (context->main_group, aliases);
889 max_length = MAX (max_length, len);
894 GOptionGroup *g = list->data;
896 if (context->help_enabled)
898 /* First, we check the --help-<groupname> options */
899 len = _g_utf8_strwidth ("--help-") + _g_utf8_strwidth (g->name);
900 max_length = MAX (max_length, len);
903 /* Then we go through the entries */
904 len = calculate_max_length (g, aliases);
905 max_length = MAX (max_length, len);
910 /* Add a bit of padding */
913 if (!group && context->help_enabled)
915 list = context->groups;
917 token = context_has_h_entry (context) ? '?' : 'h';
919 g_string_append_printf (string, "%s\n -%c, --%-*s %s\n",
920 _("Help Options:"), token, max_length - 4, "help",
921 _("Show help options"));
923 /* We only want --help-all when there are groups */
925 g_string_append_printf (string, " --%-*s %s\n",
926 max_length, "help-all",
927 _("Show all help options"));
931 GOptionGroup *g = list->data;
933 if (group_has_visible_entries (context, g, FALSE))
934 g_string_append_printf (string, " --help-%-*s %s\n",
935 max_length - 5, g->name,
936 TRANSLATE (g, g->help_description));
941 g_string_append (string, "\n");
946 /* Print a certain group */
948 if (group_has_visible_entries (context, group, FALSE))
950 g_string_append (string, TRANSLATE (group, group->description));
951 g_string_append (string, "\n");
952 for (i = 0; i < group->n_entries; i++)
953 print_entry (group, max_length, &group->entries[i], string, aliases);
954 g_string_append (string, "\n");
959 /* Print all groups */
961 list = context->groups;
965 GOptionGroup *g = list->data;
967 if (group_has_visible_entries (context, g, FALSE))
969 g_string_append (string, g->description);
970 g_string_append (string, "\n");
971 for (i = 0; i < g->n_entries; i++)
972 if (!(g->entries[i].flags & G_OPTION_FLAG_IN_MAIN))
973 print_entry (g, max_length, &g->entries[i], string, aliases);
975 g_string_append (string, "\n");
982 /* Print application options if --help or --help-all has been specified */
983 if ((main_help || !group) &&
984 (group_has_visible_entries (context, context->main_group, TRUE) ||
985 group_list_has_visible_entries (context, context->groups, TRUE)))
987 list = context->groups;
989 g_string_append (string, _("Application Options:"));
990 g_string_append (string, "\n");
991 if (context->main_group)
992 for (i = 0; i < context->main_group->n_entries; i++)
993 print_entry (context->main_group, max_length,
994 &context->main_group->entries[i], string, aliases);
998 GOptionGroup *g = list->data;
1000 /* Print main entries from other groups */
1001 for (i = 0; i < g->n_entries; i++)
1002 if (g->entries[i].flags & G_OPTION_FLAG_IN_MAIN)
1003 print_entry (g, max_length, &g->entries[i], string, aliases);
1008 g_string_append (string, "\n");
1011 if (context->description)
1013 g_string_append (string, TRANSLATE (context, context->description));
1014 g_string_append (string, "\n");
1017 g_hash_table_destroy (aliases);
1019 return g_string_free (string, FALSE);
1024 print_help (GOptionContext *context,
1026 GOptionGroup *group)
1030 help = g_option_context_get_help (context, main_help, group);
1031 g_print ("%s", help);
1038 parse_int (const gchar *arg_name,
1047 tmp = strtol (arg, &end, 0);
1049 if (*arg == '\0' || *end != '\0')
1052 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1053 _("Cannot parse integer value '%s' for %s"),
1059 if (*result != tmp || errno == ERANGE)
1062 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1063 _("Integer value '%s' for %s out of range"),
1073 parse_double (const gchar *arg_name,
1082 tmp = g_strtod (arg, &end);
1084 if (*arg == '\0' || *end != '\0')
1087 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1088 _("Cannot parse double value '%s' for %s"),
1092 if (errno == ERANGE)
1095 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1096 _("Double value '%s' for %s out of range"),
1108 parse_int64 (const gchar *arg_name,
1117 tmp = g_ascii_strtoll (arg, &end, 0);
1119 if (*arg == '\0' || *end != '\0')
1122 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1123 _("Cannot parse integer value '%s' for %s"),
1127 if (errno == ERANGE)
1130 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1131 _("Integer value '%s' for %s out of range"),
1143 get_change (GOptionContext *context,
1144 GOptionArg arg_type,
1148 Change *change = NULL;
1150 for (list = context->changes; list != NULL; list = list->next)
1152 change = list->data;
1154 if (change->arg_data == arg_data)
1158 change = g_new0 (Change, 1);
1159 change->arg_type = arg_type;
1160 change->arg_data = arg_data;
1162 context->changes = g_list_prepend (context->changes, change);
1170 add_pending_null (GOptionContext *context,
1176 n = g_new0 (PendingNull, 1);
1180 context->pending_nulls = g_list_prepend (context->pending_nulls, n);
1184 parse_arg (GOptionContext *context,
1185 GOptionGroup *group,
1186 GOptionEntry *entry,
1188 const gchar *option_name,
1194 g_assert (value || OPTIONAL_ARG (entry) || NO_ARG (entry));
1198 case G_OPTION_ARG_NONE:
1200 change = get_change (context, G_OPTION_ARG_NONE,
1203 *(gboolean *)entry->arg_data = !(entry->flags & G_OPTION_FLAG_REVERSE);
1206 case G_OPTION_ARG_STRING:
1211 if (!context->strv_mode)
1212 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1214 data = g_strdup (value);
1216 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1222 change = get_change (context, G_OPTION_ARG_STRING,
1224 g_free (change->allocated.str);
1226 change->prev.str = *(gchar **)entry->arg_data;
1227 change->allocated.str = data;
1229 *(gchar **)entry->arg_data = data;
1232 case G_OPTION_ARG_STRING_ARRAY:
1237 if (!context->strv_mode)
1238 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1240 data = g_strdup (value);
1242 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1248 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
1251 if (change->allocated.array.len == 0)
1253 change->prev.array = *(gchar ***)entry->arg_data;
1254 change->allocated.array.data = g_new (gchar *, 2);
1257 change->allocated.array.data =
1258 g_renew (gchar *, change->allocated.array.data,
1259 change->allocated.array.len + 2);
1261 change->allocated.array.data[change->allocated.array.len] = data;
1262 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
1264 change->allocated.array.len ++;
1266 *(gchar ***)entry->arg_data = change->allocated.array.data;
1271 case G_OPTION_ARG_FILENAME:
1276 if (!context->strv_mode)
1277 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1279 data = g_strdup (value);
1284 data = g_strdup (value);
1286 change = get_change (context, G_OPTION_ARG_FILENAME,
1288 g_free (change->allocated.str);
1290 change->prev.str = *(gchar **)entry->arg_data;
1291 change->allocated.str = data;
1293 *(gchar **)entry->arg_data = data;
1297 case G_OPTION_ARG_FILENAME_ARRAY:
1302 if (!context->strv_mode)
1303 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1305 data = g_strdup (value);
1310 data = g_strdup (value);
1312 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
1315 if (change->allocated.array.len == 0)
1317 change->prev.array = *(gchar ***)entry->arg_data;
1318 change->allocated.array.data = g_new (gchar *, 2);
1321 change->allocated.array.data =
1322 g_renew (gchar *, change->allocated.array.data,
1323 change->allocated.array.len + 2);
1325 change->allocated.array.data[change->allocated.array.len] = data;
1326 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
1328 change->allocated.array.len ++;
1330 *(gchar ***)entry->arg_data = change->allocated.array.data;
1335 case G_OPTION_ARG_INT:
1339 if (!parse_int (option_name, value,
1344 change = get_change (context, G_OPTION_ARG_INT,
1346 change->prev.integer = *(gint *)entry->arg_data;
1347 *(gint *)entry->arg_data = data;
1350 case G_OPTION_ARG_CALLBACK:
1355 if (!value && entry->flags & G_OPTION_FLAG_OPTIONAL_ARG)
1357 else if (entry->flags & G_OPTION_FLAG_NO_ARG)
1359 else if (entry->flags & G_OPTION_FLAG_FILENAME)
1362 if (!context->strv_mode)
1363 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1365 data = g_strdup (value);
1367 data = g_strdup (value);
1371 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1373 if (!(entry->flags & (G_OPTION_FLAG_NO_ARG|G_OPTION_FLAG_OPTIONAL_ARG)) &&
1377 retval = (* (GOptionArgFunc) entry->arg_data) (option_name, data, group->user_data, error);
1379 if (!retval && error != NULL && *error == NULL)
1381 G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
1382 _("Error parsing option %s"), option_name);
1390 case G_OPTION_ARG_DOUBLE:
1394 if (!parse_double (option_name, value,
1401 change = get_change (context, G_OPTION_ARG_DOUBLE,
1403 change->prev.dbl = *(gdouble *)entry->arg_data;
1404 *(gdouble *)entry->arg_data = data;
1407 case G_OPTION_ARG_INT64:
1411 if (!parse_int64 (option_name, value,
1418 change = get_change (context, G_OPTION_ARG_INT64,
1420 change->prev.int64 = *(gint64 *)entry->arg_data;
1421 *(gint64 *)entry->arg_data = data;
1425 g_assert_not_reached ();
1432 parse_short_option (GOptionContext *context,
1433 GOptionGroup *group,
1444 for (j = 0; j < group->n_entries; j++)
1446 if (arg == group->entries[j].short_name)
1449 gchar *value = NULL;
1451 option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
1453 if (NO_ARG (&group->entries[j]))
1460 G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
1461 _("Error parsing option %s"), option_name);
1462 g_free (option_name);
1466 if (idx < *argc - 1)
1468 if (!OPTIONAL_ARG (&group->entries[j]))
1470 value = (*argv)[idx + 1];
1471 add_pending_null (context, &((*argv)[idx + 1]), NULL);
1476 if ((*argv)[idx + 1][0] == '-')
1480 value = (*argv)[idx + 1];
1481 add_pending_null (context, &((*argv)[idx + 1]), NULL);
1486 else if (idx >= *argc - 1 && OPTIONAL_ARG (&group->entries[j]))
1491 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1492 _("Missing argument for %s"), option_name);
1493 g_free (option_name);
1498 if (!parse_arg (context, group, &group->entries[j],
1499 value, option_name, error))
1501 g_free (option_name);
1505 g_free (option_name);
1514 parse_long_option (GOptionContext *context,
1515 GOptionGroup *group,
1526 for (j = 0; j < group->n_entries; j++)
1531 if (aliased && (group->entries[j].flags & G_OPTION_FLAG_NOALIAS))
1534 if (NO_ARG (&group->entries[j]) &&
1535 strcmp (arg, group->entries[j].long_name) == 0)
1540 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1541 retval = parse_arg (context, group, &group->entries[j],
1542 NULL, option_name, error);
1543 g_free (option_name);
1545 add_pending_null (context, &((*argv)[*idx]), NULL);
1552 gint len = strlen (group->entries[j].long_name);
1554 if (strncmp (arg, group->entries[j].long_name, len) == 0 &&
1555 (arg[len] == '=' || arg[len] == 0))
1557 gchar *value = NULL;
1560 add_pending_null (context, &((*argv)[*idx]), NULL);
1561 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1563 if (arg[len] == '=')
1564 value = arg + len + 1;
1565 else if (*idx < *argc - 1)
1567 if (!OPTIONAL_ARG (&group->entries[j]))
1569 value = (*argv)[*idx + 1];
1570 add_pending_null (context, &((*argv)[*idx + 1]), NULL);
1575 if ((*argv)[*idx + 1][0] == '-')
1578 retval = parse_arg (context, group, &group->entries[j],
1579 NULL, option_name, error);
1581 g_free (option_name);
1586 value = (*argv)[*idx + 1];
1587 add_pending_null (context, &((*argv)[*idx + 1]), NULL);
1592 else if (*idx >= *argc - 1 && OPTIONAL_ARG (&group->entries[j]))
1595 retval = parse_arg (context, group, &group->entries[j],
1596 NULL, option_name, error);
1598 g_free (option_name);
1604 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1605 _("Missing argument for %s"), option_name);
1606 g_free (option_name);
1610 if (!parse_arg (context, group, &group->entries[j],
1611 value, option_name, error))
1613 g_free (option_name);
1617 g_free (option_name);
1627 parse_remaining_arg (GOptionContext *context,
1628 GOptionGroup *group,
1637 for (j = 0; j < group->n_entries; j++)
1642 if (group->entries[j].long_name[0])
1645 g_return_val_if_fail (group->entries[j].arg == G_OPTION_ARG_CALLBACK ||
1646 group->entries[j].arg == G_OPTION_ARG_STRING_ARRAY ||
1647 group->entries[j].arg == G_OPTION_ARG_FILENAME_ARRAY, FALSE);
1649 add_pending_null (context, &((*argv)[*idx]), NULL);
1651 if (!parse_arg (context, group, &group->entries[j], (*argv)[*idx], "", error))
1662 free_changes_list (GOptionContext *context,
1667 for (list = context->changes; list != NULL; list = list->next)
1669 Change *change = list->data;
1673 switch (change->arg_type)
1675 case G_OPTION_ARG_NONE:
1676 *(gboolean *)change->arg_data = change->prev.bool;
1678 case G_OPTION_ARG_INT:
1679 *(gint *)change->arg_data = change->prev.integer;
1681 case G_OPTION_ARG_STRING:
1682 case G_OPTION_ARG_FILENAME:
1683 g_free (change->allocated.str);
1684 *(gchar **)change->arg_data = change->prev.str;
1686 case G_OPTION_ARG_STRING_ARRAY:
1687 case G_OPTION_ARG_FILENAME_ARRAY:
1688 g_strfreev (change->allocated.array.data);
1689 *(gchar ***)change->arg_data = change->prev.array;
1691 case G_OPTION_ARG_DOUBLE:
1692 *(gdouble *)change->arg_data = change->prev.dbl;
1694 case G_OPTION_ARG_INT64:
1695 *(gint64 *)change->arg_data = change->prev.int64;
1698 g_assert_not_reached ();
1705 g_list_free (context->changes);
1706 context->changes = NULL;
1710 free_pending_nulls (GOptionContext *context,
1711 gboolean perform_nulls)
1715 for (list = context->pending_nulls; list != NULL; list = list->next)
1717 PendingNull *n = list->data;
1721 if (context->strv_mode)
1726 /* Copy back the short options */
1728 strcpy (*n->ptr + 1, n->value);
1738 g_list_free (context->pending_nulls);
1739 context->pending_nulls = NULL;
1742 /* Use a platform-specific mechanism to look up the first argument to
1743 * the current process.
1744 * Note if you implement this for other platforms, also add it to
1745 * tests/option-argv0.c
1748 platform_get_argv0 (void)
1755 if (!g_file_get_contents ("/proc/self/cmdline",
1760 /* Sanity check for a NUL terminator. */
1761 if (!memchr (cmdline, 0, len))
1763 /* We could just return cmdline, but I think it's better
1764 * to hold on to a smaller malloc block; the arguments
1767 base_arg0 = g_path_get_basename (cmdline);
1770 #elif defined __OpenBSD__
1771 char **cmdline = NULL;
1773 gsize len = PATH_MAX;
1775 int mib[] = { CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ARGV };
1777 cmdline = (char **) realloc (cmdline, len);
1779 if (sysctl (mib, G_N_ELEMENTS (mib), cmdline, &len, NULL, 0) == -1)
1785 /* We could just return cmdline, but I think it's better
1786 * to hold on to a smaller malloc block; the arguments
1789 base_arg0 = g_path_get_basename (*cmdline);
1798 * g_option_context_parse:
1799 * @context: a #GOptionContext
1800 * @argc: (inout) (allow-none): a pointer to the number of command line arguments
1801 * @argv: (inout) (array length=argc) (allow-none): a pointer to the array of command line arguments
1802 * @error: a return location for errors
1804 * Parses the command line arguments, recognizing options
1805 * which have been added to @context. A side-effect of
1806 * calling this function is that g_set_prgname() will be
1809 * If the parsing is successful, any parsed arguments are
1810 * removed from the array and @argc and @argv are updated
1811 * accordingly. A '--' option is stripped from @argv
1812 * unless there are unparsed options before and after it,
1813 * or some of the options after it start with '-'. In case
1814 * of an error, @argc and @argv are left unmodified.
1816 * If automatic <option>--help</option> support is enabled
1817 * (see g_option_context_set_help_enabled()), and the
1818 * @argv array contains one of the recognized help options,
1819 * this function will produce help output to stdout and
1820 * call <literal>exit (0)</literal>.
1822 * Note that function depends on the
1823 * <link linkend="setlocale">current locale</link> for
1824 * automatic character set conversion of string and filename
1827 * Return value: %TRUE if the parsing was successful,
1828 * %FALSE if an error occurred
1833 g_option_context_parse (GOptionContext *context,
1841 /* Set program name */
1842 if (!g_get_prgname())
1846 if (argc && argv && *argc)
1847 prgname = g_path_get_basename ((*argv)[0]);
1849 prgname = platform_get_argv0 ();
1852 g_set_prgname (prgname);
1854 g_set_prgname ("<unknown>");
1859 /* Call pre-parse hooks */
1860 list = context->groups;
1863 GOptionGroup *group = list->data;
1865 if (group->pre_parse_func)
1867 if (!(* group->pre_parse_func) (context, group,
1868 group->user_data, error))
1875 if (context->main_group && context->main_group->pre_parse_func)
1877 if (!(* context->main_group->pre_parse_func) (context, context->main_group,
1878 context->main_group->user_data, error))
1884 gboolean stop_parsing = FALSE;
1885 gboolean has_unknown = FALSE;
1886 gint separator_pos = 0;
1888 for (i = 1; i < *argc; i++)
1891 gboolean parsed = FALSE;
1893 if ((*argv)[i][0] == '-' && (*argv)[i][1] != '\0' && !stop_parsing)
1895 if ((*argv)[i][1] == '-')
1899 arg = (*argv)[i] + 2;
1901 /* '--' terminates list of arguments */
1905 stop_parsing = TRUE;
1909 /* Handle help options */
1910 if (context->help_enabled)
1912 if (strcmp (arg, "help") == 0)
1913 print_help (context, TRUE, NULL);
1914 else if (strcmp (arg, "help-all") == 0)
1915 print_help (context, FALSE, NULL);
1916 else if (strncmp (arg, "help-", 5) == 0)
1918 list = context->groups;
1922 GOptionGroup *group = list->data;
1924 if (strcmp (arg + 5, group->name) == 0)
1925 print_help (context, FALSE, group);
1932 if (context->main_group &&
1933 !parse_long_option (context, context->main_group, &i, arg,
1934 FALSE, argc, argv, error, &parsed))
1940 /* Try the groups */
1941 list = context->groups;
1944 GOptionGroup *group = list->data;
1946 if (!parse_long_option (context, group, &i, arg,
1947 FALSE, argc, argv, error, &parsed))
1959 /* Now look for --<group>-<option> */
1960 dash = strchr (arg, '-');
1963 /* Try the groups */
1964 list = context->groups;
1967 GOptionGroup *group = list->data;
1969 if (strncmp (group->name, arg, dash - arg) == 0)
1971 if (!parse_long_option (context, group, &i, dash + 1,
1972 TRUE, argc, argv, error, &parsed))
1983 if (context->ignore_unknown)
1987 { /* short option */
1988 gint new_i = i, arg_length;
1989 gboolean *nulled_out = NULL;
1990 gboolean has_h_entry = context_has_h_entry (context);
1991 arg = (*argv)[i] + 1;
1992 arg_length = strlen (arg);
1993 nulled_out = g_newa (gboolean, arg_length);
1994 memset (nulled_out, 0, arg_length * sizeof (gboolean));
1995 for (j = 0; j < arg_length; j++)
1997 if (context->help_enabled && (arg[j] == '?' ||
1998 (arg[j] == 'h' && !has_h_entry)))
1999 print_help (context, TRUE, NULL);
2001 if (context->main_group &&
2002 !parse_short_option (context, context->main_group,
2004 argc, argv, error, &parsed))
2008 /* Try the groups */
2009 list = context->groups;
2012 GOptionGroup *group = list->data;
2013 if (!parse_short_option (context, group, i, &new_i, arg[j],
2014 argc, argv, error, &parsed))
2022 if (context->ignore_unknown && parsed)
2023 nulled_out[j] = TRUE;
2024 else if (context->ignore_unknown)
2028 /* !context->ignore_unknown && parsed */
2030 if (context->ignore_unknown)
2032 gchar *new_arg = NULL;
2034 for (j = 0; j < arg_length; j++)
2039 new_arg = g_malloc (arg_length + 1);
2040 new_arg[arg_index++] = arg[j];
2044 new_arg[arg_index] = '\0';
2045 add_pending_null (context, &((*argv)[i]), new_arg);
2049 add_pending_null (context, &((*argv)[i]), NULL);
2057 if (!parsed && !context->ignore_unknown)
2060 G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
2061 _("Unknown option %s"), (*argv)[i]);
2067 /* Collect remaining args */
2068 if (context->main_group &&
2069 !parse_remaining_arg (context, context->main_group, &i,
2070 argc, argv, error, &parsed))
2073 if (!parsed && (has_unknown || (*argv)[i][0] == '-'))
2078 if (separator_pos > 0)
2079 add_pending_null (context, &((*argv)[separator_pos]), NULL);
2083 /* Call post-parse hooks */
2084 list = context->groups;
2087 GOptionGroup *group = list->data;
2089 if (group->post_parse_func)
2091 if (!(* group->post_parse_func) (context, group,
2092 group->user_data, error))
2099 if (context->main_group && context->main_group->post_parse_func)
2101 if (!(* context->main_group->post_parse_func) (context, context->main_group,
2102 context->main_group->user_data, error))
2108 free_pending_nulls (context, TRUE);
2110 for (i = 1; i < *argc; i++)
2112 for (k = i; k < *argc; k++)
2113 if ((*argv)[k] != NULL)
2119 for (j = i + k; j < *argc; j++)
2121 (*argv)[j-k] = (*argv)[j];
2133 /* Call error hooks */
2134 list = context->groups;
2137 GOptionGroup *group = list->data;
2139 if (group->error_func)
2140 (* group->error_func) (context, group,
2141 group->user_data, error);
2146 if (context->main_group && context->main_group->error_func)
2147 (* context->main_group->error_func) (context, context->main_group,
2148 context->main_group->user_data, error);
2150 free_changes_list (context, TRUE);
2151 free_pending_nulls (context, FALSE);
2157 * g_option_group_new:
2158 * @name: the name for the option group, this is used to provide
2159 * help for the options in this group with <option>--help-</option>@name
2160 * @description: a description for this group to be shown in
2161 * <option>--help</option>. This string is translated using the translation
2162 * domain or translation function of the group
2163 * @help_description: a description for the <option>--help-</option>@name option.
2164 * This string is translated using the translation domain or translation function
2166 * @user_data: (allow-none): user data that will be passed to the pre- and post-parse hooks,
2167 * the error hook and to callbacks of %G_OPTION_ARG_CALLBACK options, or %NULL
2168 * @destroy: (allow-none): a function that will be called to free @user_data, or %NULL
2170 * Creates a new #GOptionGroup.
2172 * Return value: a newly created option group. It should be added
2173 * to a #GOptionContext or freed with g_option_group_free().
2178 g_option_group_new (const gchar *name,
2179 const gchar *description,
2180 const gchar *help_description,
2182 GDestroyNotify destroy)
2185 GOptionGroup *group;
2187 group = g_new0 (GOptionGroup, 1);
2188 group->name = g_strdup (name);
2189 group->description = g_strdup (description);
2190 group->help_description = g_strdup (help_description);
2191 group->user_data = user_data;
2192 group->destroy_notify = destroy;
2199 * g_option_group_free:
2200 * @group: a #GOptionGroup
2202 * Frees a #GOptionGroup. Note that you must not free groups
2203 * which have been added to a #GOptionContext.
2208 g_option_group_free (GOptionGroup *group)
2210 g_return_if_fail (group != NULL);
2212 g_free (group->name);
2213 g_free (group->description);
2214 g_free (group->help_description);
2216 g_free (group->entries);
2218 if (group->destroy_notify)
2219 (* group->destroy_notify) (group->user_data);
2221 if (group->translate_notify)
2222 (* group->translate_notify) (group->translate_data);
2229 * g_option_group_add_entries:
2230 * @group: a #GOptionGroup
2231 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
2233 * Adds the options specified in @entries to @group.
2238 g_option_group_add_entries (GOptionGroup *group,
2239 const GOptionEntry *entries)
2243 g_return_if_fail (entries != NULL);
2245 for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++) ;
2247 group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
2249 memcpy (group->entries + group->n_entries, entries, sizeof (GOptionEntry) * n_entries);
2251 for (i = group->n_entries; i < group->n_entries + n_entries; i++)
2253 gchar c = group->entries[i].short_name;
2255 if (c == '-' || (c != 0 && !g_ascii_isprint (c)))
2257 g_warning (G_STRLOC ": ignoring invalid short option '%c' (%d) in entry %s:%s",
2258 c, c, group->name, group->entries[i].long_name);
2259 group->entries[i].short_name = '\0';
2262 if (group->entries[i].arg != G_OPTION_ARG_NONE &&
2263 (group->entries[i].flags & G_OPTION_FLAG_REVERSE) != 0)
2265 g_warning (G_STRLOC ": ignoring reverse flag on option of arg-type %d in entry %s:%s",
2266 group->entries[i].arg, group->name, group->entries[i].long_name);
2268 group->entries[i].flags &= ~G_OPTION_FLAG_REVERSE;
2271 if (group->entries[i].arg != G_OPTION_ARG_CALLBACK &&
2272 (group->entries[i].flags & (G_OPTION_FLAG_NO_ARG|G_OPTION_FLAG_OPTIONAL_ARG|G_OPTION_FLAG_FILENAME)) != 0)
2274 g_warning (G_STRLOC ": ignoring no-arg, optional-arg or filename flags (%d) on option of arg-type %d in entry %s:%s",
2275 group->entries[i].flags, group->entries[i].arg, group->name, group->entries[i].long_name);
2277 group->entries[i].flags &= ~(G_OPTION_FLAG_NO_ARG|G_OPTION_FLAG_OPTIONAL_ARG|G_OPTION_FLAG_FILENAME);
2281 group->n_entries += n_entries;
2285 * g_option_group_set_parse_hooks:
2286 * @group: a #GOptionGroup
2287 * @pre_parse_func: (allow-none): a function to call before parsing, or %NULL
2288 * @post_parse_func: (allow-none): a function to call after parsing, or %NULL
2290 * Associates two functions with @group which will be called
2291 * from g_option_context_parse() before the first option is parsed
2292 * and after the last option has been parsed, respectively.
2294 * Note that the user data to be passed to @pre_parse_func and
2295 * @post_parse_func can be specified when constructing the group
2296 * with g_option_group_new().
2301 g_option_group_set_parse_hooks (GOptionGroup *group,
2302 GOptionParseFunc pre_parse_func,
2303 GOptionParseFunc post_parse_func)
2305 g_return_if_fail (group != NULL);
2307 group->pre_parse_func = pre_parse_func;
2308 group->post_parse_func = post_parse_func;
2312 * g_option_group_set_error_hook:
2313 * @group: a #GOptionGroup
2314 * @error_func: a function to call when an error occurs
2316 * Associates a function with @group which will be called
2317 * from g_option_context_parse() when an error occurs.
2319 * Note that the user data to be passed to @error_func can be
2320 * specified when constructing the group with g_option_group_new().
2325 g_option_group_set_error_hook (GOptionGroup *group,
2326 GOptionErrorFunc error_func)
2328 g_return_if_fail (group != NULL);
2330 group->error_func = error_func;
2335 * g_option_group_set_translate_func:
2336 * @group: a #GOptionGroup
2337 * @func: (allow-none): the #GTranslateFunc, or %NULL
2338 * @data: (allow-none): user data to pass to @func, or %NULL
2339 * @destroy_notify: (allow-none): a function which gets called to free @data, or %NULL
2341 * Sets the function which is used to translate user-visible
2342 * strings, for <option>--help</option> output. Different
2343 * groups can use different #GTranslateFunc<!-- -->s. If @func
2344 * is %NULL, strings are not translated.
2346 * If you are using gettext(), you only need to set the translation
2347 * domain, see g_option_group_set_translation_domain().
2352 g_option_group_set_translate_func (GOptionGroup *group,
2353 GTranslateFunc func,
2355 GDestroyNotify destroy_notify)
2357 g_return_if_fail (group != NULL);
2359 if (group->translate_notify)
2360 group->translate_notify (group->translate_data);
2362 group->translate_func = func;
2363 group->translate_data = data;
2364 group->translate_notify = destroy_notify;
2367 static const gchar *
2368 dgettext_swapped (const gchar *msgid,
2369 const gchar *domainname)
2371 return g_dgettext (domainname, msgid);
2375 * g_option_group_set_translation_domain:
2376 * @group: a #GOptionGroup
2377 * @domain: the domain to use
2379 * A convenience function to use gettext() for translating
2380 * user-visible strings.
2385 g_option_group_set_translation_domain (GOptionGroup *group,
2386 const gchar *domain)
2388 g_return_if_fail (group != NULL);
2390 g_option_group_set_translate_func (group,
2391 (GTranslateFunc)dgettext_swapped,
2397 * g_option_context_set_translate_func:
2398 * @context: a #GOptionContext
2399 * @func: (allow-none): the #GTranslateFunc, or %NULL
2400 * @data: (allow-none): user data to pass to @func, or %NULL
2401 * @destroy_notify: (allow-none): a function which gets called to free @data, or %NULL
2403 * Sets the function which is used to translate the contexts
2404 * user-visible strings, for <option>--help</option> output.
2405 * If @func is %NULL, strings are not translated.
2407 * Note that option groups have their own translation functions,
2408 * this function only affects the @parameter_string (see g_option_context_new()),
2409 * the summary (see g_option_context_set_summary()) and the description
2410 * (see g_option_context_set_description()).
2412 * If you are using gettext(), you only need to set the translation
2413 * domain, see g_option_context_set_translation_domain().
2418 g_option_context_set_translate_func (GOptionContext *context,
2419 GTranslateFunc func,
2421 GDestroyNotify destroy_notify)
2423 g_return_if_fail (context != NULL);
2425 if (context->translate_notify)
2426 context->translate_notify (context->translate_data);
2428 context->translate_func = func;
2429 context->translate_data = data;
2430 context->translate_notify = destroy_notify;
2434 * g_option_context_set_translation_domain:
2435 * @context: a #GOptionContext
2436 * @domain: the domain to use
2438 * A convenience function to use gettext() for translating
2439 * user-visible strings.
2444 g_option_context_set_translation_domain (GOptionContext *context,
2445 const gchar *domain)
2447 g_return_if_fail (context != NULL);
2449 g_option_context_set_translate_func (context,
2450 (GTranslateFunc)dgettext_swapped,
2456 * g_option_context_set_summary:
2457 * @context: a #GOptionContext
2458 * @summary: (allow-none): a string to be shown in <option>--help</option> output
2459 * before the list of options, or %NULL
2461 * Adds a string to be displayed in <option>--help</option> output
2462 * before the list of options. This is typically a summary of the
2463 * program functionality.
2465 * Note that the summary is translated (see
2466 * g_option_context_set_translate_func() and
2467 * g_option_context_set_translation_domain()).
2472 g_option_context_set_summary (GOptionContext *context,
2473 const gchar *summary)
2475 g_return_if_fail (context != NULL);
2477 g_free (context->summary);
2478 context->summary = g_strdup (summary);
2483 * g_option_context_get_summary:
2484 * @context: a #GOptionContext
2486 * Returns the summary. See g_option_context_set_summary().
2488 * Returns: the summary
2493 g_option_context_get_summary (GOptionContext *context)
2495 g_return_val_if_fail (context != NULL, NULL);
2497 return context->summary;
2501 * g_option_context_set_description:
2502 * @context: a #GOptionContext
2503 * @description: (allow-none): a string to be shown in <option>--help</option> output
2504 * after the list of options, or %NULL
2506 * Adds a string to be displayed in <option>--help</option> output
2507 * after the list of options. This text often includes a bug reporting
2510 * Note that the summary is translated (see
2511 * g_option_context_set_translate_func()).
2516 g_option_context_set_description (GOptionContext *context,
2517 const gchar *description)
2519 g_return_if_fail (context != NULL);
2521 g_free (context->description);
2522 context->description = g_strdup (description);
2527 * g_option_context_get_description:
2528 * @context: a #GOptionContext
2530 * Returns the description. See g_option_context_set_description().
2532 * Returns: the description
2537 g_option_context_get_description (GOptionContext *context)
2539 g_return_val_if_fail (context != NULL, NULL);
2541 return context->description;
2545 * g_option_context_parse_strv:
2546 * @context: a #GOptionContext
2547 * @arguments: (inout) (array null-terminated=1): a pointer to the
2548 * command line arguments (which must be in UTF-8 on Windows)
2549 * @error: a return location for errors
2551 * Parses the command line arguments.
2553 * This function is similar to g_option_context_parse() except that it
2554 * respects the normal memory rules when dealing with a strv instead of
2555 * assuming that the passed-in array is the argv of the main function.
2557 * In particular, strings that are removed from the arguments list will
2558 * be freed using g_free().
2560 * On Windows, the strings are expected to be in UTF-8. This is in
2561 * contrast to g_option_context_parse() which expects them to be in the
2562 * system codepage, which is how they are passed as @argv to main().
2563 * See g_win32_get_command_line() for a solution.
2565 * This function is useful if you are trying to use #GOptionContext with
2568 * Returns: %TRUE if the parsing was successful,
2569 * %FALSE if an error occurred
2574 g_option_context_parse_strv (GOptionContext *context,
2581 context->strv_mode = TRUE;
2582 argc = g_strv_length (*arguments);
2583 success = g_option_context_parse (context, &argc, arguments, error);
2584 context->strv_mode = FALSE;