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
34 * - Options can be single letters, prefixed by a single dash.
36 * - Multiple short options can be grouped behind a single dash.
38 * - Long options are prefixed by two consecutive dashes.
40 * - Options can have an extra argument, which can be a number, a string or
41 * a filename. For long options, the extra argument can be appended with
42 * an equals sign after the option name, which is useful if the extra
43 * argument starts with a dash, which would otherwise cause it to be
44 * interpreted as another option.
46 * - Non-option arguments are returned to the application as rest arguments.
48 * - An argument consisting solely of two dashes turns off further parsing,
49 * any remaining arguments (even those starting with a dash) are returned
50 * to the application as rest arguments.
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 #GOptionGroups, 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.
94 * |[<!-- language="C" -->
95 * static gint repeats = 2;
96 * static gint max_size = 8;
97 * static gboolean verbose = FALSE;
98 * static gboolean beep = FALSE;
99 * static gboolean randomize = FALSE;
101 * static GOptionEntry entries[] =
103 * { "repeats", 'r', 0, G_OPTION_ARG_INT, &repeats, "Average over N repetitions", "N" },
104 * { "max-size", 'm', 0, G_OPTION_ARG_INT, &max_size, "Test up to 2^M items", "M" },
105 * { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL },
106 * { "beep", 'b', 0, G_OPTION_ARG_NONE, &beep, "Beep when done", NULL },
107 * { "rand", 0, 0, G_OPTION_ARG_NONE, &randomize, "Randomize the data", NULL },
112 * main (int argc, char *argv[])
114 * GError *error = NULL;
115 * GOptionContext *context;
117 * context = g_option_context_new ("- test tree model performance");
118 * g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
119 * g_option_context_add_group (context, gtk_get_option_group (TRUE));
120 * if (!g_option_context_parse (context, &argc, &argv, &error))
122 * g_print ("option parsing failed: %s\n", error->message);
131 * On UNIX systems, the argv that is passed to main() has no particular
132 * encoding, even to the extent that different parts of it may have
133 * different encodings. In general, normal arguments and flags will be
134 * in the current locale and filenames should be considered to be opaque
135 * byte strings. Proper use of %G_OPTION_ARG_FILENAME vs
136 * %G_OPTION_ARG_STRING is therefore important.
138 * Note that on Windows, filenames do have an encoding, but using
139 * #GOptionContext with the argv as passed to main() will result in a
140 * program that can only accept commandline arguments with characters
141 * from the system codepage. This can cause problems when attempting to
142 * deal with filenames containing Unicode characters that fall outside
145 * A solution to this is to use g_win32_get_command_line() and
146 * g_option_context_parse_strv() which will properly handle full Unicode
147 * filenames. If you are using #GApplication, this is done
148 * automatically for you.
150 * The following example shows how you can use #GOptionContext directly
151 * in order to correctly deal with Unicode filenames on Windows:
153 * |[<!-- language="C" -->
155 * main (int argc, char **argv)
157 * GError *error = NULL;
158 * GOptionContext *context;
162 * args = g_win32_get_command_line ();
164 * args = g_strdupv (argv);
167 * /* ... setup context ... */
169 * if (!g_option_context_parse_strv (context, &args, &error))
171 * /* ... error ... */
190 #if defined __OpenBSD__
191 #include <sys/types.h>
193 #include <sys/param.h>
194 #include <sys/sysctl.h>
200 #include "glibintl.h"
202 #define TRANSLATE(group, str) (((group)->translate_func ? (* (group)->translate_func) ((str), (group)->translate_data) : (str)))
204 #define NO_ARG(entry) ((entry)->arg == G_OPTION_ARG_NONE || \
205 ((entry)->arg == G_OPTION_ARG_CALLBACK && \
206 ((entry)->flags & G_OPTION_FLAG_NO_ARG)))
208 #define OPTIONAL_ARG(entry) ((entry)->arg == G_OPTION_ARG_CALLBACK && \
209 (entry)->flags & G_OPTION_FLAG_OPTIONAL_ARG)
241 struct _GOptionContext
245 gchar *parameter_string;
249 GTranslateFunc translate_func;
250 GDestroyNotify translate_notify;
251 gpointer translate_data;
253 guint help_enabled : 1;
254 guint ignore_unknown : 1;
257 GOptionGroup *main_group;
259 /* We keep a list of change so we can revert them */
262 /* We also keep track of all argv elements
263 * that should be NULLed or modified.
265 GList *pending_nulls;
272 gchar *help_description;
274 GDestroyNotify destroy_notify;
277 GTranslateFunc translate_func;
278 GDestroyNotify translate_notify;
279 gpointer translate_data;
281 GOptionEntry *entries;
284 GOptionParseFunc pre_parse_func;
285 GOptionParseFunc post_parse_func;
286 GOptionErrorFunc error_func;
289 static void free_changes_list (GOptionContext *context,
291 static void free_pending_nulls (GOptionContext *context,
292 gboolean perform_nulls);
296 _g_unichar_get_width (gunichar c)
298 if (G_UNLIKELY (g_unichar_iszerowidth (c)))
301 /* we ignore the fact that we should call g_unichar_iswide_cjk() under
302 * some locales (legacy East Asian ones) */
303 if (g_unichar_iswide (c))
310 _g_utf8_strwidth (const gchar *p)
313 g_return_val_if_fail (p != NULL, 0);
317 len += _g_unichar_get_width (g_utf8_get_char (p));
318 p = g_utf8_next_char (p);
324 G_DEFINE_QUARK (g-option-context-error-quark, g_option_error)
327 * g_option_context_new:
328 * @parameter_string: (allow-none): a string which is displayed in
329 * the first line of <option>--help</option> output, after the
331 * <literal><replaceable>programname</replaceable> [OPTION...]</literal>
333 * Creates a new option context.
335 * The @parameter_string can serve multiple purposes. It can be used
336 * to add descriptions for "rest" arguments, which are not parsed by
337 * the #GOptionContext, typically something like "FILES" or
338 * "FILE1 FILE2...". If you are using #G_OPTION_REMAINING for
339 * collecting "rest" arguments, GLib handles this automatically by
340 * using the @arg_description of the corresponding #GOptionEntry in
343 * Another usage is to give a short summary of the program
344 * functionality, like " - frob the strings", which will be displayed
345 * in the same line as the usage. For a longer description of the
346 * program functionality that should be displayed as a paragraph
347 * below the usage line, use g_option_context_set_summary().
349 * Note that the @parameter_string is translated using the
350 * function set with g_option_context_set_translate_func(), so
351 * it should normally be passed untranslated.
353 * Returns: a newly created #GOptionContext, which must be
354 * freed with g_option_context_free() after use.
359 g_option_context_new (const gchar *parameter_string)
362 GOptionContext *context;
364 context = g_new0 (GOptionContext, 1);
366 context->parameter_string = g_strdup (parameter_string);
367 context->help_enabled = TRUE;
368 context->ignore_unknown = FALSE;
374 * g_option_context_free:
375 * @context: a #GOptionContext
377 * Frees context and all the groups which have been
380 * Please note that parsed arguments need to be freed separately (see
385 void g_option_context_free (GOptionContext *context)
387 g_return_if_fail (context != NULL);
389 g_list_free_full (context->groups, (GDestroyNotify) g_option_group_free);
391 if (context->main_group)
392 g_option_group_free (context->main_group);
394 free_changes_list (context, FALSE);
395 free_pending_nulls (context, FALSE);
397 g_free (context->parameter_string);
398 g_free (context->summary);
399 g_free (context->description);
401 if (context->translate_notify)
402 (* context->translate_notify) (context->translate_data);
409 * g_option_context_set_help_enabled:
410 * @context: a #GOptionContext
411 * @help_enabled: %TRUE to enable <option>--help</option>, %FALSE to disable it
413 * Enables or disables automatic generation of <option>--help</option>
414 * output. By default, g_option_context_parse() recognizes
415 * <option>--help</option>, <option>-h</option>,
416 * <option>-?</option>, <option>--help-all</option>
417 * and <option>--help-</option><replaceable>groupname</replaceable> and creates
418 * suitable output to stdout.
422 void g_option_context_set_help_enabled (GOptionContext *context,
423 gboolean help_enabled)
426 g_return_if_fail (context != NULL);
428 context->help_enabled = help_enabled;
432 * g_option_context_get_help_enabled:
433 * @context: a #GOptionContext
435 * Returns whether automatic <option>--help</option> generation
436 * is turned on for @context. See g_option_context_set_help_enabled().
438 * Returns: %TRUE if automatic help generation is turned on.
443 g_option_context_get_help_enabled (GOptionContext *context)
445 g_return_val_if_fail (context != NULL, FALSE);
447 return context->help_enabled;
451 * g_option_context_set_ignore_unknown_options:
452 * @context: a #GOptionContext
453 * @ignore_unknown: %TRUE to ignore unknown options, %FALSE to produce
454 * an error when unknown options are met
456 * Sets whether to ignore unknown options or not. If an argument is
457 * ignored, it is left in the @argv array after parsing. By default,
458 * g_option_context_parse() treats unknown options as error.
460 * This setting does not affect non-option arguments (i.e. arguments
461 * which don't start with a dash). But note that GOption cannot reliably
462 * determine whether a non-option belongs to a preceding unknown option.
467 g_option_context_set_ignore_unknown_options (GOptionContext *context,
468 gboolean ignore_unknown)
470 g_return_if_fail (context != NULL);
472 context->ignore_unknown = ignore_unknown;
476 * g_option_context_get_ignore_unknown_options:
477 * @context: a #GOptionContext
479 * Returns whether unknown options are ignored or not. See
480 * g_option_context_set_ignore_unknown_options().
482 * Returns: %TRUE if unknown options are ignored.
487 g_option_context_get_ignore_unknown_options (GOptionContext *context)
489 g_return_val_if_fail (context != NULL, FALSE);
491 return context->ignore_unknown;
495 * g_option_context_add_group:
496 * @context: a #GOptionContext
497 * @group: the group to add
499 * Adds a #GOptionGroup to the @context, so that parsing with @context
500 * will recognize the options in the group. Note that the group will
501 * be freed together with the context when g_option_context_free() is
502 * called, so you must not free the group yourself after adding it
508 g_option_context_add_group (GOptionContext *context,
513 g_return_if_fail (context != NULL);
514 g_return_if_fail (group != NULL);
515 g_return_if_fail (group->name != NULL);
516 g_return_if_fail (group->description != NULL);
517 g_return_if_fail (group->help_description != NULL);
519 for (list = context->groups; list; list = list->next)
521 GOptionGroup *g = (GOptionGroup *)list->data;
523 if ((group->name == NULL && g->name == NULL) ||
524 (group->name && g->name && strcmp (group->name, g->name) == 0))
525 g_warning ("A group named \"%s\" is already part of this GOptionContext",
529 context->groups = g_list_append (context->groups, group);
533 * g_option_context_set_main_group:
534 * @context: a #GOptionContext
535 * @group: the group to set as main group
537 * Sets a #GOptionGroup as main group of the @context.
538 * This has the same effect as calling g_option_context_add_group(),
539 * the only difference is that the options in the main group are
540 * treated differently when generating <option>--help</option> output.
545 g_option_context_set_main_group (GOptionContext *context,
548 g_return_if_fail (context != NULL);
549 g_return_if_fail (group != NULL);
551 if (context->main_group)
553 g_warning ("This GOptionContext already has a main group");
558 context->main_group = group;
562 * g_option_context_get_main_group:
563 * @context: a #GOptionContext
565 * Returns a pointer to the main group of @context.
567 * Return value: the main group of @context, or %NULL if @context doesn't
568 * have a main group. Note that group belongs to @context and should
569 * not be modified or freed.
574 g_option_context_get_main_group (GOptionContext *context)
576 g_return_val_if_fail (context != NULL, NULL);
578 return context->main_group;
582 * g_option_context_add_main_entries:
583 * @context: a #GOptionContext
584 * @entries: a %NULL-terminated array of #GOptionEntrys
585 * @translation_domain: (allow-none): a translation domain to use for translating
586 * the <option>--help</option> output for the options in @entries
587 * with gettext(), or %NULL
589 * A convenience function which creates a main group if it doesn't
590 * exist, adds the @entries to it and sets the translation domain.
595 g_option_context_add_main_entries (GOptionContext *context,
596 const GOptionEntry *entries,
597 const gchar *translation_domain)
599 g_return_if_fail (entries != NULL);
601 if (!context->main_group)
602 context->main_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
604 g_option_group_add_entries (context->main_group, entries);
605 g_option_group_set_translation_domain (context->main_group, translation_domain);
609 calculate_max_length (GOptionGroup *group,
613 gint i, len, max_length;
614 const gchar *long_name;
618 for (i = 0; i < group->n_entries; i++)
620 entry = &group->entries[i];
622 if (entry->flags & G_OPTION_FLAG_HIDDEN)
625 long_name = g_hash_table_lookup (aliases, &entry->long_name);
627 long_name = entry->long_name;
628 len = _g_utf8_strwidth (long_name);
630 if (entry->short_name)
633 if (!NO_ARG (entry) && entry->arg_description)
634 len += 1 + _g_utf8_strwidth (TRANSLATE (group, entry->arg_description));
636 max_length = MAX (max_length, len);
643 print_entry (GOptionGroup *group,
645 const GOptionEntry *entry,
650 const gchar *long_name;
652 if (entry->flags & G_OPTION_FLAG_HIDDEN)
655 if (entry->long_name[0] == 0)
658 long_name = g_hash_table_lookup (aliases, &entry->long_name);
660 long_name = entry->long_name;
662 str = g_string_new (NULL);
664 if (entry->short_name)
665 g_string_append_printf (str, " -%c, --%s", entry->short_name, long_name);
667 g_string_append_printf (str, " --%s", long_name);
669 if (entry->arg_description)
670 g_string_append_printf (str, "=%s", TRANSLATE (group, entry->arg_description));
672 g_string_append_printf (string, "%s%*s %s\n", str->str,
673 (int) (max_length + 4 - _g_utf8_strwidth (str->str)), "",
674 entry->description ? TRANSLATE (group, entry->description) : "");
675 g_string_free (str, TRUE);
679 group_has_visible_entries (GOptionContext *context,
681 gboolean main_entries)
683 GOptionFlags reject_filter = G_OPTION_FLAG_HIDDEN;
686 gboolean main_group = group == context->main_group;
689 reject_filter |= G_OPTION_FLAG_IN_MAIN;
691 for (i = 0, l = (group ? group->n_entries : 0); i < l; i++)
693 entry = &group->entries[i];
695 if (main_entries && !main_group && !(entry->flags & G_OPTION_FLAG_IN_MAIN))
697 if (entry->long_name[0] == 0) /* ignore rest entry */
699 if (!(entry->flags & reject_filter))
707 group_list_has_visible_entries (GOptionContext *context,
709 gboolean main_entries)
713 if (group_has_visible_entries (context, group_list->data, main_entries))
716 group_list = group_list->next;
723 context_has_h_entry (GOptionContext *context)
728 if (context->main_group)
730 for (i = 0; i < context->main_group->n_entries; i++)
732 if (context->main_group->entries[i].short_name == 'h')
737 for (list = context->groups; list != NULL; list = g_list_next (list))
741 group = (GOptionGroup*)list->data;
742 for (i = 0; i < group->n_entries; i++)
744 if (group->entries[i].short_name == 'h')
752 * g_option_context_get_help:
753 * @context: a #GOptionContext
754 * @main_help: if %TRUE, only include the main group
755 * @group: (allow-none): the #GOptionGroup to create help for, or %NULL
757 * Returns a formatted, translated help text for the given context.
758 * To obtain the text produced by <option>--help</option>, call
759 * <literal>g_option_context_get_help (context, TRUE, NULL)</literal>.
760 * To obtain the text produced by <option>--help-all</option>, call
761 * <literal>g_option_context_get_help (context, FALSE, NULL)</literal>.
762 * To obtain the help text for an option group, call
763 * <literal>g_option_context_get_help (context, FALSE, group)</literal>.
765 * Returns: A newly allocated string containing the help text
770 g_option_context_get_help (GOptionContext *context,
775 gint max_length = 0, len;
778 GHashTable *shadow_map;
781 const gchar *rest_description;
785 string = g_string_sized_new (1024);
787 rest_description = NULL;
788 if (context->main_group)
791 for (i = 0; i < context->main_group->n_entries; i++)
793 entry = &context->main_group->entries[i];
794 if (entry->long_name[0] == 0)
796 rest_description = TRANSLATE (context->main_group, entry->arg_description);
802 g_string_append_printf (string, "%s\n %s %s",
803 _("Usage:"), g_get_prgname(), _("[OPTION...]"));
805 if (rest_description)
807 g_string_append (string, " ");
808 g_string_append (string, rest_description);
811 if (context->parameter_string)
813 g_string_append (string, " ");
814 g_string_append (string, TRANSLATE (context, context->parameter_string));
817 g_string_append (string, "\n\n");
819 if (context->summary)
821 g_string_append (string, TRANSLATE (context, context->summary));
822 g_string_append (string, "\n\n");
825 memset (seen, 0, sizeof (gboolean) * 256);
826 shadow_map = g_hash_table_new (g_str_hash, g_str_equal);
827 aliases = g_hash_table_new_full (NULL, NULL, NULL, g_free);
829 if (context->main_group)
831 for (i = 0; i < context->main_group->n_entries; i++)
833 entry = &context->main_group->entries[i];
834 g_hash_table_insert (shadow_map,
835 (gpointer)entry->long_name,
838 if (seen[(guchar)entry->short_name])
839 entry->short_name = 0;
841 seen[(guchar)entry->short_name] = TRUE;
845 list = context->groups;
848 GOptionGroup *g = list->data;
849 for (i = 0; i < g->n_entries; i++)
851 entry = &g->entries[i];
852 if (g_hash_table_lookup (shadow_map, entry->long_name) &&
853 !(entry->flags & G_OPTION_FLAG_NOALIAS))
855 g_hash_table_insert (aliases, &entry->long_name,
856 g_strdup_printf ("%s-%s", g->name, entry->long_name));
859 g_hash_table_insert (shadow_map, (gpointer)entry->long_name, entry);
861 if (seen[(guchar)entry->short_name] &&
862 !(entry->flags & G_OPTION_FLAG_NOALIAS))
863 entry->short_name = 0;
865 seen[(guchar)entry->short_name] = TRUE;
870 g_hash_table_destroy (shadow_map);
872 list = context->groups;
874 if (context->help_enabled)
876 max_length = _g_utf8_strwidth ("-?, --help");
880 len = _g_utf8_strwidth ("--help-all");
881 max_length = MAX (max_length, len);
885 if (context->main_group)
887 len = calculate_max_length (context->main_group, aliases);
888 max_length = MAX (max_length, len);
893 GOptionGroup *g = list->data;
895 if (context->help_enabled)
897 /* First, we check the --help-<groupname> options */
898 len = _g_utf8_strwidth ("--help-") + _g_utf8_strwidth (g->name);
899 max_length = MAX (max_length, len);
902 /* Then we go through the entries */
903 len = calculate_max_length (g, aliases);
904 max_length = MAX (max_length, len);
909 /* Add a bit of padding */
912 if (!group && context->help_enabled)
914 list = context->groups;
916 token = context_has_h_entry (context) ? '?' : 'h';
918 g_string_append_printf (string, "%s\n -%c, --%-*s %s\n",
919 _("Help Options:"), token, max_length - 4, "help",
920 _("Show help options"));
922 /* We only want --help-all when there are groups */
924 g_string_append_printf (string, " --%-*s %s\n",
925 max_length, "help-all",
926 _("Show all help options"));
930 GOptionGroup *g = list->data;
932 if (group_has_visible_entries (context, g, FALSE))
933 g_string_append_printf (string, " --help-%-*s %s\n",
934 max_length - 5, g->name,
935 TRANSLATE (g, g->help_description));
940 g_string_append (string, "\n");
945 /* Print a certain group */
947 if (group_has_visible_entries (context, group, FALSE))
949 g_string_append (string, TRANSLATE (group, group->description));
950 g_string_append (string, "\n");
951 for (i = 0; i < group->n_entries; i++)
952 print_entry (group, max_length, &group->entries[i], string, aliases);
953 g_string_append (string, "\n");
958 /* Print all groups */
960 list = context->groups;
964 GOptionGroup *g = list->data;
966 if (group_has_visible_entries (context, g, FALSE))
968 g_string_append (string, g->description);
969 g_string_append (string, "\n");
970 for (i = 0; i < g->n_entries; i++)
971 if (!(g->entries[i].flags & G_OPTION_FLAG_IN_MAIN))
972 print_entry (g, max_length, &g->entries[i], string, aliases);
974 g_string_append (string, "\n");
981 /* Print application options if --help or --help-all has been specified */
982 if ((main_help || !group) &&
983 (group_has_visible_entries (context, context->main_group, TRUE) ||
984 group_list_has_visible_entries (context, context->groups, TRUE)))
986 list = context->groups;
988 g_string_append (string, _("Application Options:"));
989 g_string_append (string, "\n");
990 if (context->main_group)
991 for (i = 0; i < context->main_group->n_entries; i++)
992 print_entry (context->main_group, max_length,
993 &context->main_group->entries[i], string, aliases);
997 GOptionGroup *g = list->data;
999 /* Print main entries from other groups */
1000 for (i = 0; i < g->n_entries; i++)
1001 if (g->entries[i].flags & G_OPTION_FLAG_IN_MAIN)
1002 print_entry (g, max_length, &g->entries[i], string, aliases);
1007 g_string_append (string, "\n");
1010 if (context->description)
1012 g_string_append (string, TRANSLATE (context, context->description));
1013 g_string_append (string, "\n");
1016 g_hash_table_destroy (aliases);
1018 return g_string_free (string, FALSE);
1023 print_help (GOptionContext *context,
1025 GOptionGroup *group)
1029 help = g_option_context_get_help (context, main_help, group);
1030 g_print ("%s", help);
1037 parse_int (const gchar *arg_name,
1046 tmp = strtol (arg, &end, 0);
1048 if (*arg == '\0' || *end != '\0')
1051 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1052 _("Cannot parse integer value '%s' for %s"),
1058 if (*result != tmp || errno == ERANGE)
1061 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1062 _("Integer value '%s' for %s out of range"),
1072 parse_double (const gchar *arg_name,
1081 tmp = g_strtod (arg, &end);
1083 if (*arg == '\0' || *end != '\0')
1086 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1087 _("Cannot parse double value '%s' for %s"),
1091 if (errno == ERANGE)
1094 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1095 _("Double value '%s' for %s out of range"),
1107 parse_int64 (const gchar *arg_name,
1116 tmp = g_ascii_strtoll (arg, &end, 0);
1118 if (*arg == '\0' || *end != '\0')
1121 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1122 _("Cannot parse integer value '%s' for %s"),
1126 if (errno == ERANGE)
1129 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1130 _("Integer value '%s' for %s out of range"),
1142 get_change (GOptionContext *context,
1143 GOptionArg arg_type,
1147 Change *change = NULL;
1149 for (list = context->changes; list != NULL; list = list->next)
1151 change = list->data;
1153 if (change->arg_data == arg_data)
1157 change = g_new0 (Change, 1);
1158 change->arg_type = arg_type;
1159 change->arg_data = arg_data;
1161 context->changes = g_list_prepend (context->changes, change);
1169 add_pending_null (GOptionContext *context,
1175 n = g_new0 (PendingNull, 1);
1179 context->pending_nulls = g_list_prepend (context->pending_nulls, n);
1183 parse_arg (GOptionContext *context,
1184 GOptionGroup *group,
1185 GOptionEntry *entry,
1187 const gchar *option_name,
1193 g_assert (value || OPTIONAL_ARG (entry) || NO_ARG (entry));
1197 case G_OPTION_ARG_NONE:
1199 change = get_change (context, G_OPTION_ARG_NONE,
1202 *(gboolean *)entry->arg_data = !(entry->flags & G_OPTION_FLAG_REVERSE);
1205 case G_OPTION_ARG_STRING:
1210 if (!context->strv_mode)
1211 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1213 data = g_strdup (value);
1215 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1221 change = get_change (context, G_OPTION_ARG_STRING,
1223 g_free (change->allocated.str);
1225 change->prev.str = *(gchar **)entry->arg_data;
1226 change->allocated.str = data;
1228 *(gchar **)entry->arg_data = data;
1231 case G_OPTION_ARG_STRING_ARRAY:
1236 if (!context->strv_mode)
1237 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1239 data = g_strdup (value);
1241 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1247 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
1250 if (change->allocated.array.len == 0)
1252 change->prev.array = *(gchar ***)entry->arg_data;
1253 change->allocated.array.data = g_new (gchar *, 2);
1256 change->allocated.array.data =
1257 g_renew (gchar *, change->allocated.array.data,
1258 change->allocated.array.len + 2);
1260 change->allocated.array.data[change->allocated.array.len] = data;
1261 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
1263 change->allocated.array.len ++;
1265 *(gchar ***)entry->arg_data = change->allocated.array.data;
1270 case G_OPTION_ARG_FILENAME:
1275 if (!context->strv_mode)
1276 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1278 data = g_strdup (value);
1283 data = g_strdup (value);
1285 change = get_change (context, G_OPTION_ARG_FILENAME,
1287 g_free (change->allocated.str);
1289 change->prev.str = *(gchar **)entry->arg_data;
1290 change->allocated.str = data;
1292 *(gchar **)entry->arg_data = data;
1296 case G_OPTION_ARG_FILENAME_ARRAY:
1301 if (!context->strv_mode)
1302 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1304 data = g_strdup (value);
1309 data = g_strdup (value);
1311 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
1314 if (change->allocated.array.len == 0)
1316 change->prev.array = *(gchar ***)entry->arg_data;
1317 change->allocated.array.data = g_new (gchar *, 2);
1320 change->allocated.array.data =
1321 g_renew (gchar *, change->allocated.array.data,
1322 change->allocated.array.len + 2);
1324 change->allocated.array.data[change->allocated.array.len] = data;
1325 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
1327 change->allocated.array.len ++;
1329 *(gchar ***)entry->arg_data = change->allocated.array.data;
1334 case G_OPTION_ARG_INT:
1338 if (!parse_int (option_name, value,
1343 change = get_change (context, G_OPTION_ARG_INT,
1345 change->prev.integer = *(gint *)entry->arg_data;
1346 *(gint *)entry->arg_data = data;
1349 case G_OPTION_ARG_CALLBACK:
1354 if (!value && entry->flags & G_OPTION_FLAG_OPTIONAL_ARG)
1356 else if (entry->flags & G_OPTION_FLAG_NO_ARG)
1358 else if (entry->flags & G_OPTION_FLAG_FILENAME)
1361 if (!context->strv_mode)
1362 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1364 data = g_strdup (value);
1366 data = g_strdup (value);
1370 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1372 if (!(entry->flags & (G_OPTION_FLAG_NO_ARG|G_OPTION_FLAG_OPTIONAL_ARG)) &&
1376 retval = (* (GOptionArgFunc) entry->arg_data) (option_name, data, group->user_data, error);
1378 if (!retval && error != NULL && *error == NULL)
1380 G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
1381 _("Error parsing option %s"), option_name);
1389 case G_OPTION_ARG_DOUBLE:
1393 if (!parse_double (option_name, value,
1400 change = get_change (context, G_OPTION_ARG_DOUBLE,
1402 change->prev.dbl = *(gdouble *)entry->arg_data;
1403 *(gdouble *)entry->arg_data = data;
1406 case G_OPTION_ARG_INT64:
1410 if (!parse_int64 (option_name, value,
1417 change = get_change (context, G_OPTION_ARG_INT64,
1419 change->prev.int64 = *(gint64 *)entry->arg_data;
1420 *(gint64 *)entry->arg_data = data;
1424 g_assert_not_reached ();
1431 parse_short_option (GOptionContext *context,
1432 GOptionGroup *group,
1443 for (j = 0; j < group->n_entries; j++)
1445 if (arg == group->entries[j].short_name)
1448 gchar *value = NULL;
1450 option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
1452 if (NO_ARG (&group->entries[j]))
1459 G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
1460 _("Error parsing option %s"), option_name);
1461 g_free (option_name);
1465 if (idx < *argc - 1)
1467 if (!OPTIONAL_ARG (&group->entries[j]))
1469 value = (*argv)[idx + 1];
1470 add_pending_null (context, &((*argv)[idx + 1]), NULL);
1475 if ((*argv)[idx + 1][0] == '-')
1479 value = (*argv)[idx + 1];
1480 add_pending_null (context, &((*argv)[idx + 1]), NULL);
1485 else if (idx >= *argc - 1 && OPTIONAL_ARG (&group->entries[j]))
1490 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1491 _("Missing argument for %s"), option_name);
1492 g_free (option_name);
1497 if (!parse_arg (context, group, &group->entries[j],
1498 value, option_name, error))
1500 g_free (option_name);
1504 g_free (option_name);
1513 parse_long_option (GOptionContext *context,
1514 GOptionGroup *group,
1525 for (j = 0; j < group->n_entries; j++)
1530 if (aliased && (group->entries[j].flags & G_OPTION_FLAG_NOALIAS))
1533 if (NO_ARG (&group->entries[j]) &&
1534 strcmp (arg, group->entries[j].long_name) == 0)
1539 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1540 retval = parse_arg (context, group, &group->entries[j],
1541 NULL, option_name, error);
1542 g_free (option_name);
1544 add_pending_null (context, &((*argv)[*idx]), NULL);
1551 gint len = strlen (group->entries[j].long_name);
1553 if (strncmp (arg, group->entries[j].long_name, len) == 0 &&
1554 (arg[len] == '=' || arg[len] == 0))
1556 gchar *value = NULL;
1559 add_pending_null (context, &((*argv)[*idx]), NULL);
1560 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1562 if (arg[len] == '=')
1563 value = arg + len + 1;
1564 else if (*idx < *argc - 1)
1566 if (!OPTIONAL_ARG (&group->entries[j]))
1568 value = (*argv)[*idx + 1];
1569 add_pending_null (context, &((*argv)[*idx + 1]), NULL);
1574 if ((*argv)[*idx + 1][0] == '-')
1577 retval = parse_arg (context, group, &group->entries[j],
1578 NULL, option_name, error);
1580 g_free (option_name);
1585 value = (*argv)[*idx + 1];
1586 add_pending_null (context, &((*argv)[*idx + 1]), NULL);
1591 else if (*idx >= *argc - 1 && OPTIONAL_ARG (&group->entries[j]))
1594 retval = parse_arg (context, group, &group->entries[j],
1595 NULL, option_name, error);
1597 g_free (option_name);
1603 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1604 _("Missing argument for %s"), option_name);
1605 g_free (option_name);
1609 if (!parse_arg (context, group, &group->entries[j],
1610 value, option_name, error))
1612 g_free (option_name);
1616 g_free (option_name);
1626 parse_remaining_arg (GOptionContext *context,
1627 GOptionGroup *group,
1636 for (j = 0; j < group->n_entries; j++)
1641 if (group->entries[j].long_name[0])
1644 g_return_val_if_fail (group->entries[j].arg == G_OPTION_ARG_CALLBACK ||
1645 group->entries[j].arg == G_OPTION_ARG_STRING_ARRAY ||
1646 group->entries[j].arg == G_OPTION_ARG_FILENAME_ARRAY, FALSE);
1648 add_pending_null (context, &((*argv)[*idx]), NULL);
1650 if (!parse_arg (context, group, &group->entries[j], (*argv)[*idx], "", error))
1661 free_changes_list (GOptionContext *context,
1666 for (list = context->changes; list != NULL; list = list->next)
1668 Change *change = list->data;
1672 switch (change->arg_type)
1674 case G_OPTION_ARG_NONE:
1675 *(gboolean *)change->arg_data = change->prev.bool;
1677 case G_OPTION_ARG_INT:
1678 *(gint *)change->arg_data = change->prev.integer;
1680 case G_OPTION_ARG_STRING:
1681 case G_OPTION_ARG_FILENAME:
1682 g_free (change->allocated.str);
1683 *(gchar **)change->arg_data = change->prev.str;
1685 case G_OPTION_ARG_STRING_ARRAY:
1686 case G_OPTION_ARG_FILENAME_ARRAY:
1687 g_strfreev (change->allocated.array.data);
1688 *(gchar ***)change->arg_data = change->prev.array;
1690 case G_OPTION_ARG_DOUBLE:
1691 *(gdouble *)change->arg_data = change->prev.dbl;
1693 case G_OPTION_ARG_INT64:
1694 *(gint64 *)change->arg_data = change->prev.int64;
1697 g_assert_not_reached ();
1704 g_list_free (context->changes);
1705 context->changes = NULL;
1709 free_pending_nulls (GOptionContext *context,
1710 gboolean perform_nulls)
1714 for (list = context->pending_nulls; list != NULL; list = list->next)
1716 PendingNull *n = list->data;
1722 /* Copy back the short options */
1724 strcpy (*n->ptr + 1, n->value);
1728 if (context->strv_mode)
1739 g_list_free (context->pending_nulls);
1740 context->pending_nulls = NULL;
1743 /* Use a platform-specific mechanism to look up the first argument to
1744 * the current process.
1745 * Note if you implement this for other platforms, also add it to
1746 * tests/option-argv0.c
1749 platform_get_argv0 (void)
1756 if (!g_file_get_contents ("/proc/self/cmdline",
1761 /* Sanity check for a NUL terminator. */
1762 if (!memchr (cmdline, 0, len))
1764 /* We could just return cmdline, but I think it's better
1765 * to hold on to a smaller malloc block; the arguments
1768 base_arg0 = g_path_get_basename (cmdline);
1771 #elif defined __OpenBSD__
1772 char **cmdline = NULL;
1774 gsize len = PATH_MAX;
1776 int mib[] = { CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ARGV };
1778 cmdline = (char **) realloc (cmdline, len);
1780 if (sysctl (mib, G_N_ELEMENTS (mib), cmdline, &len, NULL, 0) == -1)
1786 /* We could just return cmdline, but I think it's better
1787 * to hold on to a smaller malloc block; the arguments
1790 base_arg0 = g_path_get_basename (*cmdline);
1799 * g_option_context_parse:
1800 * @context: a #GOptionContext
1801 * @argc: (inout) (allow-none): a pointer to the number of command line arguments
1802 * @argv: (inout) (array length=argc) (allow-none): a pointer to the array of command line arguments
1803 * @error: a return location for errors
1805 * Parses the command line arguments, recognizing options
1806 * which have been added to @context. A side-effect of
1807 * calling this function is that g_set_prgname() will be
1810 * If the parsing is successful, any parsed arguments are
1811 * removed from the array and @argc and @argv are updated
1812 * accordingly. A '--' option is stripped from @argv
1813 * unless there are unparsed options before and after it,
1814 * or some of the options after it start with '-'. In case
1815 * of an error, @argc and @argv are left unmodified.
1817 * If automatic <option>--help</option> support is enabled
1818 * (see g_option_context_set_help_enabled()), and the
1819 * @argv array contains one of the recognized help options,
1820 * this function will produce help output to stdout and
1821 * call <literal>exit (0)</literal>.
1823 * Note that function depends on the
1824 * <link linkend="setlocale">current locale</link> for
1825 * automatic character set conversion of string and filename
1828 * Return value: %TRUE if the parsing was successful,
1829 * %FALSE if an error occurred
1834 g_option_context_parse (GOptionContext *context,
1842 /* Set program name */
1843 if (!g_get_prgname())
1847 if (argc && argv && *argc)
1848 prgname = g_path_get_basename ((*argv)[0]);
1850 prgname = platform_get_argv0 ();
1853 g_set_prgname (prgname);
1855 g_set_prgname ("<unknown>");
1860 /* Call pre-parse hooks */
1861 list = context->groups;
1864 GOptionGroup *group = list->data;
1866 if (group->pre_parse_func)
1868 if (!(* group->pre_parse_func) (context, group,
1869 group->user_data, error))
1876 if (context->main_group && context->main_group->pre_parse_func)
1878 if (!(* context->main_group->pre_parse_func) (context, context->main_group,
1879 context->main_group->user_data, error))
1885 gboolean stop_parsing = FALSE;
1886 gboolean has_unknown = FALSE;
1887 gint separator_pos = 0;
1889 for (i = 1; i < *argc; i++)
1892 gboolean parsed = FALSE;
1894 if ((*argv)[i][0] == '-' && (*argv)[i][1] != '\0' && !stop_parsing)
1896 if ((*argv)[i][1] == '-')
1900 arg = (*argv)[i] + 2;
1902 /* '--' terminates list of arguments */
1906 stop_parsing = TRUE;
1910 /* Handle help options */
1911 if (context->help_enabled)
1913 if (strcmp (arg, "help") == 0)
1914 print_help (context, TRUE, NULL);
1915 else if (strcmp (arg, "help-all") == 0)
1916 print_help (context, FALSE, NULL);
1917 else if (strncmp (arg, "help-", 5) == 0)
1919 list = context->groups;
1923 GOptionGroup *group = list->data;
1925 if (strcmp (arg + 5, group->name) == 0)
1926 print_help (context, FALSE, group);
1933 if (context->main_group &&
1934 !parse_long_option (context, context->main_group, &i, arg,
1935 FALSE, argc, argv, error, &parsed))
1941 /* Try the groups */
1942 list = context->groups;
1945 GOptionGroup *group = list->data;
1947 if (!parse_long_option (context, group, &i, arg,
1948 FALSE, argc, argv, error, &parsed))
1960 /* Now look for --<group>-<option> */
1961 dash = strchr (arg, '-');
1964 /* Try the groups */
1965 list = context->groups;
1968 GOptionGroup *group = list->data;
1970 if (strncmp (group->name, arg, dash - arg) == 0)
1972 if (!parse_long_option (context, group, &i, dash + 1,
1973 TRUE, argc, argv, error, &parsed))
1984 if (context->ignore_unknown)
1988 { /* short option */
1989 gint new_i = i, arg_length;
1990 gboolean *nulled_out = NULL;
1991 gboolean has_h_entry = context_has_h_entry (context);
1992 arg = (*argv)[i] + 1;
1993 arg_length = strlen (arg);
1994 nulled_out = g_newa (gboolean, arg_length);
1995 memset (nulled_out, 0, arg_length * sizeof (gboolean));
1996 for (j = 0; j < arg_length; j++)
1998 if (context->help_enabled && (arg[j] == '?' ||
1999 (arg[j] == 'h' && !has_h_entry)))
2000 print_help (context, TRUE, NULL);
2002 if (context->main_group &&
2003 !parse_short_option (context, context->main_group,
2005 argc, argv, error, &parsed))
2009 /* Try the groups */
2010 list = context->groups;
2013 GOptionGroup *group = list->data;
2014 if (!parse_short_option (context, group, i, &new_i, arg[j],
2015 argc, argv, error, &parsed))
2023 if (context->ignore_unknown && parsed)
2024 nulled_out[j] = TRUE;
2025 else if (context->ignore_unknown)
2029 /* !context->ignore_unknown && parsed */
2031 if (context->ignore_unknown)
2033 gchar *new_arg = NULL;
2035 for (j = 0; j < arg_length; j++)
2040 new_arg = g_malloc (arg_length + 1);
2041 new_arg[arg_index++] = arg[j];
2045 new_arg[arg_index] = '\0';
2046 add_pending_null (context, &((*argv)[i]), new_arg);
2050 add_pending_null (context, &((*argv)[i]), NULL);
2058 if (!parsed && !context->ignore_unknown)
2061 G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
2062 _("Unknown option %s"), (*argv)[i]);
2068 /* Collect remaining args */
2069 if (context->main_group &&
2070 !parse_remaining_arg (context, context->main_group, &i,
2071 argc, argv, error, &parsed))
2074 if (!parsed && (has_unknown || (*argv)[i][0] == '-'))
2079 if (separator_pos > 0)
2080 add_pending_null (context, &((*argv)[separator_pos]), NULL);
2084 /* Call post-parse hooks */
2085 list = context->groups;
2088 GOptionGroup *group = list->data;
2090 if (group->post_parse_func)
2092 if (!(* group->post_parse_func) (context, group,
2093 group->user_data, error))
2100 if (context->main_group && context->main_group->post_parse_func)
2102 if (!(* context->main_group->post_parse_func) (context, context->main_group,
2103 context->main_group->user_data, error))
2109 free_pending_nulls (context, TRUE);
2111 for (i = 1; i < *argc; i++)
2113 for (k = i; k < *argc; k++)
2114 if ((*argv)[k] != NULL)
2120 for (j = i + k; j < *argc; j++)
2122 (*argv)[j-k] = (*argv)[j];
2134 /* Call error hooks */
2135 list = context->groups;
2138 GOptionGroup *group = list->data;
2140 if (group->error_func)
2141 (* group->error_func) (context, group,
2142 group->user_data, error);
2147 if (context->main_group && context->main_group->error_func)
2148 (* context->main_group->error_func) (context, context->main_group,
2149 context->main_group->user_data, error);
2151 free_changes_list (context, TRUE);
2152 free_pending_nulls (context, FALSE);
2158 * g_option_group_new:
2159 * @name: the name for the option group, this is used to provide
2160 * help for the options in this group with <option>--help-</option>@name
2161 * @description: a description for this group to be shown in
2162 * <option>--help</option>. This string is translated using the translation
2163 * domain or translation function of the group
2164 * @help_description: a description for the <option>--help-</option>@name option.
2165 * This string is translated using the translation domain or translation function
2167 * @user_data: (allow-none): user data that will be passed to the pre- and post-parse hooks,
2168 * the error hook and to callbacks of %G_OPTION_ARG_CALLBACK options, or %NULL
2169 * @destroy: (allow-none): a function that will be called to free @user_data, or %NULL
2171 * Creates a new #GOptionGroup.
2173 * Return value: a newly created option group. It should be added
2174 * to a #GOptionContext or freed with g_option_group_free().
2179 g_option_group_new (const gchar *name,
2180 const gchar *description,
2181 const gchar *help_description,
2183 GDestroyNotify destroy)
2186 GOptionGroup *group;
2188 group = g_new0 (GOptionGroup, 1);
2189 group->name = g_strdup (name);
2190 group->description = g_strdup (description);
2191 group->help_description = g_strdup (help_description);
2192 group->user_data = user_data;
2193 group->destroy_notify = destroy;
2200 * g_option_group_free:
2201 * @group: a #GOptionGroup
2203 * Frees a #GOptionGroup. Note that you must not free groups
2204 * which have been added to a #GOptionContext.
2209 g_option_group_free (GOptionGroup *group)
2211 g_return_if_fail (group != NULL);
2213 g_free (group->name);
2214 g_free (group->description);
2215 g_free (group->help_description);
2217 g_free (group->entries);
2219 if (group->destroy_notify)
2220 (* group->destroy_notify) (group->user_data);
2222 if (group->translate_notify)
2223 (* group->translate_notify) (group->translate_data);
2230 * g_option_group_add_entries:
2231 * @group: a #GOptionGroup
2232 * @entries: a %NULL-terminated array of #GOptionEntrys
2234 * Adds the options specified in @entries to @group.
2239 g_option_group_add_entries (GOptionGroup *group,
2240 const GOptionEntry *entries)
2244 g_return_if_fail (entries != NULL);
2246 for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++) ;
2248 group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
2250 memcpy (group->entries + group->n_entries, entries, sizeof (GOptionEntry) * n_entries);
2252 for (i = group->n_entries; i < group->n_entries + n_entries; i++)
2254 gchar c = group->entries[i].short_name;
2256 if (c == '-' || (c != 0 && !g_ascii_isprint (c)))
2258 g_warning (G_STRLOC ": ignoring invalid short option '%c' (%d) in entry %s:%s",
2259 c, c, group->name, group->entries[i].long_name);
2260 group->entries[i].short_name = '\0';
2263 if (group->entries[i].arg != G_OPTION_ARG_NONE &&
2264 (group->entries[i].flags & G_OPTION_FLAG_REVERSE) != 0)
2266 g_warning (G_STRLOC ": ignoring reverse flag on option of arg-type %d in entry %s:%s",
2267 group->entries[i].arg, group->name, group->entries[i].long_name);
2269 group->entries[i].flags &= ~G_OPTION_FLAG_REVERSE;
2272 if (group->entries[i].arg != G_OPTION_ARG_CALLBACK &&
2273 (group->entries[i].flags & (G_OPTION_FLAG_NO_ARG|G_OPTION_FLAG_OPTIONAL_ARG|G_OPTION_FLAG_FILENAME)) != 0)
2275 g_warning (G_STRLOC ": ignoring no-arg, optional-arg or filename flags (%d) on option of arg-type %d in entry %s:%s",
2276 group->entries[i].flags, group->entries[i].arg, group->name, group->entries[i].long_name);
2278 group->entries[i].flags &= ~(G_OPTION_FLAG_NO_ARG|G_OPTION_FLAG_OPTIONAL_ARG|G_OPTION_FLAG_FILENAME);
2282 group->n_entries += n_entries;
2286 * g_option_group_set_parse_hooks:
2287 * @group: a #GOptionGroup
2288 * @pre_parse_func: (allow-none): a function to call before parsing, or %NULL
2289 * @post_parse_func: (allow-none): a function to call after parsing, or %NULL
2291 * Associates two functions with @group which will be called
2292 * from g_option_context_parse() before the first option is parsed
2293 * and after the last option has been parsed, respectively.
2295 * Note that the user data to be passed to @pre_parse_func and
2296 * @post_parse_func can be specified when constructing the group
2297 * with g_option_group_new().
2302 g_option_group_set_parse_hooks (GOptionGroup *group,
2303 GOptionParseFunc pre_parse_func,
2304 GOptionParseFunc post_parse_func)
2306 g_return_if_fail (group != NULL);
2308 group->pre_parse_func = pre_parse_func;
2309 group->post_parse_func = post_parse_func;
2313 * g_option_group_set_error_hook:
2314 * @group: a #GOptionGroup
2315 * @error_func: a function to call when an error occurs
2317 * Associates a function with @group which will be called
2318 * from g_option_context_parse() when an error occurs.
2320 * Note that the user data to be passed to @error_func can be
2321 * specified when constructing the group with g_option_group_new().
2326 g_option_group_set_error_hook (GOptionGroup *group,
2327 GOptionErrorFunc error_func)
2329 g_return_if_fail (group != NULL);
2331 group->error_func = error_func;
2336 * g_option_group_set_translate_func:
2337 * @group: a #GOptionGroup
2338 * @func: (allow-none): the #GTranslateFunc, or %NULL
2339 * @data: (allow-none): user data to pass to @func, or %NULL
2340 * @destroy_notify: (allow-none): a function which gets called to free @data, or %NULL
2342 * Sets the function which is used to translate user-visible
2343 * strings, for <option>--help</option> output. Different
2344 * groups can use different #GTranslateFuncs. If @func
2345 * is %NULL, strings are not translated.
2347 * If you are using gettext(), you only need to set the translation
2348 * domain, see g_option_group_set_translation_domain().
2353 g_option_group_set_translate_func (GOptionGroup *group,
2354 GTranslateFunc func,
2356 GDestroyNotify destroy_notify)
2358 g_return_if_fail (group != NULL);
2360 if (group->translate_notify)
2361 group->translate_notify (group->translate_data);
2363 group->translate_func = func;
2364 group->translate_data = data;
2365 group->translate_notify = destroy_notify;
2368 static const gchar *
2369 dgettext_swapped (const gchar *msgid,
2370 const gchar *domainname)
2372 return g_dgettext (domainname, msgid);
2376 * g_option_group_set_translation_domain:
2377 * @group: a #GOptionGroup
2378 * @domain: the domain to use
2380 * A convenience function to use gettext() for translating
2381 * user-visible strings.
2386 g_option_group_set_translation_domain (GOptionGroup *group,
2387 const gchar *domain)
2389 g_return_if_fail (group != NULL);
2391 g_option_group_set_translate_func (group,
2392 (GTranslateFunc)dgettext_swapped,
2398 * g_option_context_set_translate_func:
2399 * @context: a #GOptionContext
2400 * @func: (allow-none): the #GTranslateFunc, or %NULL
2401 * @data: (allow-none): user data to pass to @func, or %NULL
2402 * @destroy_notify: (allow-none): a function which gets called to free @data, or %NULL
2404 * Sets the function which is used to translate the contexts
2405 * user-visible strings, for <option>--help</option> output.
2406 * If @func is %NULL, strings are not translated.
2408 * Note that option groups have their own translation functions,
2409 * this function only affects the @parameter_string (see g_option_context_new()),
2410 * the summary (see g_option_context_set_summary()) and the description
2411 * (see g_option_context_set_description()).
2413 * If you are using gettext(), you only need to set the translation
2414 * domain, see g_option_context_set_translation_domain().
2419 g_option_context_set_translate_func (GOptionContext *context,
2420 GTranslateFunc func,
2422 GDestroyNotify destroy_notify)
2424 g_return_if_fail (context != NULL);
2426 if (context->translate_notify)
2427 context->translate_notify (context->translate_data);
2429 context->translate_func = func;
2430 context->translate_data = data;
2431 context->translate_notify = destroy_notify;
2435 * g_option_context_set_translation_domain:
2436 * @context: a #GOptionContext
2437 * @domain: the domain to use
2439 * A convenience function to use gettext() for translating
2440 * user-visible strings.
2445 g_option_context_set_translation_domain (GOptionContext *context,
2446 const gchar *domain)
2448 g_return_if_fail (context != NULL);
2450 g_option_context_set_translate_func (context,
2451 (GTranslateFunc)dgettext_swapped,
2457 * g_option_context_set_summary:
2458 * @context: a #GOptionContext
2459 * @summary: (allow-none): a string to be shown in <option>--help</option> output
2460 * before the list of options, or %NULL
2462 * Adds a string to be displayed in <option>--help</option> output
2463 * before the list of options. This is typically a summary of the
2464 * program functionality.
2466 * Note that the summary is translated (see
2467 * g_option_context_set_translate_func() and
2468 * g_option_context_set_translation_domain()).
2473 g_option_context_set_summary (GOptionContext *context,
2474 const gchar *summary)
2476 g_return_if_fail (context != NULL);
2478 g_free (context->summary);
2479 context->summary = g_strdup (summary);
2484 * g_option_context_get_summary:
2485 * @context: a #GOptionContext
2487 * Returns the summary. See g_option_context_set_summary().
2489 * Returns: the summary
2494 g_option_context_get_summary (GOptionContext *context)
2496 g_return_val_if_fail (context != NULL, NULL);
2498 return context->summary;
2502 * g_option_context_set_description:
2503 * @context: a #GOptionContext
2504 * @description: (allow-none): a string to be shown in <option>--help</option> output
2505 * after the list of options, or %NULL
2507 * Adds a string to be displayed in <option>--help</option> output
2508 * after the list of options. This text often includes a bug reporting
2511 * Note that the summary is translated (see
2512 * g_option_context_set_translate_func()).
2517 g_option_context_set_description (GOptionContext *context,
2518 const gchar *description)
2520 g_return_if_fail (context != NULL);
2522 g_free (context->description);
2523 context->description = g_strdup (description);
2528 * g_option_context_get_description:
2529 * @context: a #GOptionContext
2531 * Returns the description. See g_option_context_set_description().
2533 * Returns: the description
2538 g_option_context_get_description (GOptionContext *context)
2540 g_return_val_if_fail (context != NULL, NULL);
2542 return context->description;
2546 * g_option_context_parse_strv:
2547 * @context: a #GOptionContext
2548 * @arguments: (inout) (array null-terminated=1): a pointer to the
2549 * command line arguments (which must be in UTF-8 on Windows)
2550 * @error: a return location for errors
2552 * Parses the command line arguments.
2554 * This function is similar to g_option_context_parse() except that it
2555 * respects the normal memory rules when dealing with a strv instead of
2556 * assuming that the passed-in array is the argv of the main function.
2558 * In particular, strings that are removed from the arguments list will
2559 * be freed using g_free().
2561 * On Windows, the strings are expected to be in UTF-8. This is in
2562 * contrast to g_option_context_parse() which expects them to be in the
2563 * system codepage, which is how they are passed as @argv to main().
2564 * See g_win32_get_command_line() for a solution.
2566 * This function is useful if you are trying to use #GOptionContext with
2569 * Returns: %TRUE if the parsing was successful,
2570 * %FALSE if an error occurred
2575 g_option_context_parse_strv (GOptionContext *context,
2582 context->strv_mode = TRUE;
2583 argc = g_strv_length (*arguments);
2584 success = g_option_context_parse (context, &argc, arguments, error);
2585 context->strv_mode = FALSE;