1 /* goption.c - Option parser
3 * Copyright (C) 1999, 2003 Red Hat Software
4 * Copyright (C) 2004 Anders Carlsson <andersca@gnome.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
34 #define TRANSLATE(group, str) (((group)->translate_func ? (* (group)->translate_func) ((str), (group)->translate_data) : (str)))
36 #define NO_ARG(entry) ((entry)->arg == G_OPTION_ARG_NONE || \
37 ((entry)->arg == G_OPTION_ARG_CALLBACK && \
38 ((entry)->flags & G_OPTION_FLAG_NO_ARG)))
40 #define OPTIONAL_ARG(entry) ((entry)->arg == G_OPTION_ARG_CALLBACK && \
41 (entry)->flags & G_OPTION_FLAG_OPTIONAL_ARG)
72 struct _GOptionContext
76 gchar *parameter_string;
78 gboolean help_enabled;
79 gboolean ignore_unknown;
81 GOptionGroup *main_group;
83 /* We keep a list of change so we can revert them */
86 /* We also keep track of all argv elements that should be NULLed or
96 gchar *help_description;
98 GDestroyNotify destroy_notify;
101 GTranslateFunc translate_func;
102 GDestroyNotify translate_notify;
103 gpointer translate_data;
105 GOptionEntry *entries;
108 GOptionParseFunc pre_parse_func;
109 GOptionParseFunc post_parse_func;
110 GOptionErrorFunc error_func;
113 static void free_changes_list (GOptionContext *context,
115 static void free_pending_nulls (GOptionContext *context,
116 gboolean perform_nulls);
119 g_option_error_quark (void)
124 q = g_quark_from_static_string ("g-option-context-error-quark");
130 * g_option_context_new:
131 * @parameter_string: a string which is displayed in
132 * the first line of <option>--help</option> output, after the
134 * <literal><replaceable>programname</replaceable> [OPTION...]</literal>
136 * Creates a new option context.
138 * The @parameter_text can serve multiple purposes. It can be used
139 * to add descriptions for "rest" arguments, which are not parsed by
140 * the #GOptionContext, typically something like "FILES" or
141 * "FILE1 FILE2...". (If you are using #G_OPTION_REMAINING for
142 * collecting "rest" arguments, GLib handles this automatically by
143 * using the @arg_description of the corresponding #GOptionEntry in
144 * the usage summary.)
146 * Another common usage is to give a summary of the program
147 * functionality. This can be a short summary on the same line,
148 * like " - frob the strings", or a longer description in a paragraph
149 * below the usage summary. In this case, @parameter_string should start
150 * with two newlines, to separate the description from the usage summary:
151 * "\n\nA program to frob strings, which will..."
153 * Returns: a newly created #GOptionContext, which must be
154 * freed with g_option_context_free() after use.
159 g_option_context_new (const gchar *parameter_string)
162 GOptionContext *context;
164 context = g_new0 (GOptionContext, 1);
166 context->parameter_string = g_strdup (parameter_string);
167 context->help_enabled = TRUE;
168 context->ignore_unknown = FALSE;
174 * g_option_context_free:
175 * @context: a #GOptionContext
177 * Frees context and all the groups which have been
182 void g_option_context_free (GOptionContext *context)
184 g_return_if_fail (context != NULL);
186 g_list_foreach (context->groups, (GFunc)g_option_group_free, NULL);
187 g_list_free (context->groups);
189 if (context->main_group)
190 g_option_group_free (context->main_group);
192 free_changes_list (context, FALSE);
193 free_pending_nulls (context, FALSE);
195 g_free (context->parameter_string);
202 * g_option_context_set_help_enabled:
203 * @context: a #GOptionContext
204 * @help_enabled: %TRUE to enable <option>--help</option>, %FALSE to disable it
206 * Enables or disables automatic generation of <option>--help</option>
207 * output. By default, g_option_context_parse() recognizes
208 * <option>--help</option>, <option>-?</option>, <option>--help-all</option>
209 * and <option>--help-</option><replaceable>groupname</replaceable> and creates
210 * suitable output to stdout.
214 void g_option_context_set_help_enabled (GOptionContext *context,
215 gboolean help_enabled)
218 g_return_if_fail (context != NULL);
220 context->help_enabled = help_enabled;
224 * g_option_context_get_help_enabled:
225 * @context: a #GOptionContext
227 * Returns whether automatic <option>--help</option> generation
228 * is turned on for @context. See g_option_context_set_help_enabled().
230 * Returns: %TRUE if automatic help generation is turned on.
235 g_option_context_get_help_enabled (GOptionContext *context)
237 g_return_val_if_fail (context != NULL, FALSE);
239 return context->help_enabled;
243 * g_option_context_set_ignore_unknown_options:
244 * @context: a #GOptionContext
245 * @ignore_unknown: %TRUE to ignore unknown options, %FALSE to produce
246 * an error when unknown options are met
248 * Sets whether to ignore unknown options or not. If an argument is
249 * ignored, it is left in the @argv array after parsing. By default,
250 * g_option_context_parse() treats unknown options as error.
252 * This setting does not affect non-option arguments (i.e. arguments
253 * which don't start with a dash). But note that GOption cannot reliably
254 * determine whether a non-option belongs to a preceding unknown option.
259 g_option_context_set_ignore_unknown_options (GOptionContext *context,
260 gboolean ignore_unknown)
262 g_return_if_fail (context != NULL);
264 context->ignore_unknown = ignore_unknown;
268 * g_option_context_get_ignore_unknown_options:
269 * @context: a #GOptionContext
271 * Returns whether unknown options are ignored or not. See
272 * g_option_context_set_ignore_unknown_options().
274 * Returns: %TRUE if unknown options are ignored.
279 g_option_context_get_ignore_unknown_options (GOptionContext *context)
281 g_return_val_if_fail (context != NULL, FALSE);
283 return context->ignore_unknown;
287 * g_option_context_add_group:
288 * @context: a #GOptionContext
289 * @group: the group to add
291 * Adds a #GOptionGroup to the @context, so that parsing with @context
292 * will recognize the options in the group. Note that the group will
293 * be freed together with the context when g_option_context_free() is
294 * called, so you must not free the group yourself after adding it
300 g_option_context_add_group (GOptionContext *context,
305 g_return_if_fail (context != NULL);
306 g_return_if_fail (group != NULL);
307 g_return_if_fail (group->name != NULL);
308 g_return_if_fail (group->description != NULL);
309 g_return_if_fail (group->help_description != NULL);
311 for (list = context->groups; list; list = list->next)
313 GOptionGroup *g = (GOptionGroup *)list->data;
315 if ((group->name == NULL && g->name == NULL) ||
316 (group->name && g->name && strcmp (group->name, g->name) == 0))
317 g_warning ("A group named \"%s\" is already part of this GOptionContext",
321 context->groups = g_list_append (context->groups, group);
325 * g_option_context_set_main_group:
326 * @context: a #GOptionContext
327 * @group: the group to set as main group
329 * Sets a #GOptionGroup as main group of the @context.
330 * This has the same effect as calling g_option_context_add_group(),
331 * the only difference is that the options in the main group are
332 * treated differently when generating <option>--help</option> output.
337 g_option_context_set_main_group (GOptionContext *context,
340 g_return_if_fail (context != NULL);
341 g_return_if_fail (group != NULL);
343 if (context->main_group)
345 g_warning ("This GOptionContext already has a main group");
350 context->main_group = group;
354 * g_option_context_get_main_group:
355 * @context: a #GOptionContext
357 * Returns a pointer to the main group of @context.
359 * Return value: the main group of @context, or %NULL if @context doesn't
360 * have a main group. Note that group belongs to @context and should
361 * not be modified or freed.
366 g_option_context_get_main_group (GOptionContext *context)
368 g_return_val_if_fail (context != NULL, NULL);
370 return context->main_group;
374 * g_option_context_add_main_entries:
375 * @context: a #GOptionContext
376 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
377 * @translation_domain: a translation domain to use for translating
378 * the <option>--help</option> output for the options in @entries
379 * with gettext(), or %NULL
381 * A convenience function which creates a main group if it doesn't
382 * exist, adds the @entries to it and sets the translation domain.
387 g_option_context_add_main_entries (GOptionContext *context,
388 const GOptionEntry *entries,
389 const gchar *translation_domain)
391 g_return_if_fail (entries != NULL);
393 if (!context->main_group)
394 context->main_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
396 g_option_group_add_entries (context->main_group, entries);
397 g_option_group_set_translation_domain (context->main_group, translation_domain);
401 calculate_max_length (GOptionGroup *group)
404 gint i, len, max_length;
408 for (i = 0; i < group->n_entries; i++)
410 entry = &group->entries[i];
412 if (entry->flags & G_OPTION_FLAG_HIDDEN)
415 len = g_utf8_strlen (entry->long_name, -1);
417 if (entry->short_name)
420 if (!NO_ARG (entry) && entry->arg_description)
421 len += 1 + g_utf8_strlen (TRANSLATE (group, entry->arg_description), -1);
423 max_length = MAX (max_length, len);
430 print_entry (GOptionGroup *group,
432 const GOptionEntry *entry)
436 if (entry->flags & G_OPTION_FLAG_HIDDEN)
439 if (entry->long_name[0] == 0)
442 str = g_string_new (NULL);
444 if (entry->short_name)
445 g_string_append_printf (str, " -%c, --%s", entry->short_name, entry->long_name);
447 g_string_append_printf (str, " --%s", entry->long_name);
449 if (entry->arg_description)
450 g_string_append_printf (str, "=%s", TRANSLATE (group, entry->arg_description));
452 g_print ("%-*s %s\n", max_length + 4, str->str,
453 entry->description ? TRANSLATE (group, entry->description) : "");
454 g_string_free (str, TRUE);
458 print_help (GOptionContext *context,
463 gint max_length, len;
466 GHashTable *shadow_map;
468 const gchar *rest_description;
470 rest_description = NULL;
471 if (context->main_group)
473 for (i = 0; i < context->main_group->n_entries; i++)
475 entry = &context->main_group->entries[i];
476 if (entry->long_name[0] == 0)
478 rest_description = entry->arg_description;
484 g_print ("%s\n %s %s%s%s%s%s\n\n",
485 _("Usage:"), g_get_prgname(), _("[OPTION...]"),
486 rest_description ? " " : "",
487 rest_description ? rest_description : "",
488 context->parameter_string ? " " : "",
489 context->parameter_string ? context->parameter_string : "");
491 memset (seen, 0, sizeof (gboolean) * 256);
492 shadow_map = g_hash_table_new (g_str_hash, g_str_equal);
494 if (context->main_group)
496 for (i = 0; i < context->main_group->n_entries; i++)
498 entry = &context->main_group->entries[i];
499 g_hash_table_insert (shadow_map,
500 (gpointer)entry->long_name,
503 if (seen[(guchar)entry->short_name])
504 entry->short_name = 0;
506 seen[(guchar)entry->short_name] = TRUE;
510 list = context->groups;
513 GOptionGroup *group = list->data;
514 for (i = 0; i < group->n_entries; i++)
516 entry = &group->entries[i];
517 if (g_hash_table_lookup (shadow_map, entry->long_name) &&
518 !(entry->flags && G_OPTION_FLAG_NOALIAS))
519 entry->long_name = g_strdup_printf ("%s-%s", group->name, entry->long_name);
521 g_hash_table_insert (shadow_map, (gpointer)entry->long_name, entry);
523 if (seen[(guchar)entry->short_name] &&
524 !(entry->flags && G_OPTION_FLAG_NOALIAS))
525 entry->short_name = 0;
527 seen[(guchar)entry->short_name] = TRUE;
532 g_hash_table_destroy (shadow_map);
534 list = context->groups;
536 max_length = g_utf8_strlen ("-?, --help", -1);
540 len = g_utf8_strlen ("--help-all", -1);
541 max_length = MAX (max_length, len);
544 if (context->main_group)
546 len = calculate_max_length (context->main_group);
547 max_length = MAX (max_length, len);
552 GOptionGroup *group = list->data;
554 /* First, we check the --help-<groupname> options */
555 len = g_utf8_strlen ("--help-", -1) + g_utf8_strlen (group->name, -1);
556 max_length = MAX (max_length, len);
558 /* Then we go through the entries */
559 len = calculate_max_length (group);
560 max_length = MAX (max_length, len);
565 /* Add a bit of padding */
570 list = context->groups;
572 g_print ("%s\n -%c, --%-*s %s\n",
573 _("Help Options:"), '?', max_length - 4, "help",
574 _("Show help options"));
576 /* We only want --help-all when there are groups */
578 g_print (" --%-*s %s\n", max_length, "help-all",
579 _("Show all help options"));
583 GOptionGroup *group = list->data;
585 g_print (" --help-%-*s %s\n", max_length - 5, group->name,
586 TRANSLATE (group, group->help_description));
596 /* Print a certain group */
598 g_print ("%s\n", TRANSLATE (group, group->description));
599 for (i = 0; i < group->n_entries; i++)
600 print_entry (group, max_length, &group->entries[i]);
605 /* Print all groups */
607 list = context->groups;
611 GOptionGroup *group = list->data;
613 g_print ("%s\n", group->description);
615 for (i = 0; i < group->n_entries; i++)
616 if (!(group->entries[i].flags & G_OPTION_FLAG_IN_MAIN))
617 print_entry (group, max_length, &group->entries[i]);
624 /* Print application options if --help or --help-all has been specified */
625 if (main_help || !group)
627 list = context->groups;
629 g_print ("%s\n", _("Application Options:"));
631 if (context->main_group)
632 for (i = 0; i < context->main_group->n_entries; i++)
633 print_entry (context->main_group, max_length,
634 &context->main_group->entries[i]);
638 GOptionGroup *group = list->data;
640 /* Print main entries from other groups */
641 for (i = 0; i < group->n_entries; i++)
642 if (group->entries[i].flags & G_OPTION_FLAG_IN_MAIN)
643 print_entry (group, max_length, &group->entries[i]);
655 parse_int (const gchar *arg_name,
664 tmp = strtol (arg, &end, 0);
666 if (*arg == '\0' || *end != '\0')
669 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
670 _("Cannot parse integer value '%s' for %s"),
676 if (*result != tmp || errno == ERANGE)
679 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
680 _("Integer value '%s' for %s out of range"),
690 parse_double (const gchar *arg_name,
699 tmp = g_strtod (arg, &end);
701 if (*arg == '\0' || *end != '\0')
704 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
705 _("Cannot parse double value '%s' for %s"),
712 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
713 _("Double value '%s' for %s out of range"),
725 get_change (GOptionContext *context,
730 Change *change = NULL;
732 for (list = context->changes; list != NULL; list = list->next)
736 if (change->arg_data == arg_data)
740 change = g_new0 (Change, 1);
741 change->arg_type = arg_type;
742 change->arg_data = arg_data;
744 context->changes = g_list_prepend (context->changes, change);
752 add_pending_null (GOptionContext *context,
758 n = g_new0 (PendingNull, 1);
762 context->pending_nulls = g_list_prepend (context->pending_nulls, n);
766 parse_arg (GOptionContext *context,
770 const gchar *option_name,
778 case G_OPTION_ARG_NONE:
780 change = get_change (context, G_OPTION_ARG_NONE,
783 *(gboolean *)entry->arg_data = !(entry->flags & G_OPTION_FLAG_REVERSE);
786 case G_OPTION_ARG_STRING:
790 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
795 change = get_change (context, G_OPTION_ARG_STRING,
797 g_free (change->allocated.str);
799 change->prev.str = *(gchar **)entry->arg_data;
800 change->allocated.str = data;
802 *(gchar **)entry->arg_data = data;
805 case G_OPTION_ARG_STRING_ARRAY:
809 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
814 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
817 if (change->allocated.array.len == 0)
819 change->prev.array = *(gchar ***)entry->arg_data;
820 change->allocated.array.data = g_new (gchar *, 2);
823 change->allocated.array.data =
824 g_renew (gchar *, change->allocated.array.data,
825 change->allocated.array.len + 2);
827 change->allocated.array.data[change->allocated.array.len] = data;
828 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
830 change->allocated.array.len ++;
832 *(gchar ***)entry->arg_data = change->allocated.array.data;
837 case G_OPTION_ARG_FILENAME:
842 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
847 data = g_strdup (value);
849 change = get_change (context, G_OPTION_ARG_FILENAME,
851 g_free (change->allocated.str);
853 change->prev.str = *(gchar **)entry->arg_data;
854 change->allocated.str = data;
856 *(gchar **)entry->arg_data = data;
860 case G_OPTION_ARG_FILENAME_ARRAY:
865 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
870 data = g_strdup (value);
872 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
875 if (change->allocated.array.len == 0)
877 change->prev.array = *(gchar ***)entry->arg_data;
878 change->allocated.array.data = g_new (gchar *, 2);
881 change->allocated.array.data =
882 g_renew (gchar *, change->allocated.array.data,
883 change->allocated.array.len + 2);
885 change->allocated.array.data[change->allocated.array.len] = data;
886 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
888 change->allocated.array.len ++;
890 *(gchar ***)entry->arg_data = change->allocated.array.data;
895 case G_OPTION_ARG_INT:
899 if (!parse_int (option_name, value,
904 change = get_change (context, G_OPTION_ARG_INT,
906 change->prev.integer = *(gint *)entry->arg_data;
907 *(gint *)entry->arg_data = data;
910 case G_OPTION_ARG_CALLBACK:
915 if (!value && entry->flags & G_OPTION_FLAG_OPTIONAL_ARG)
917 else if (entry->flags & G_OPTION_FLAG_NO_ARG)
919 else if (entry->flags & G_OPTION_FLAG_FILENAME)
922 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
924 data = g_strdup (value);
928 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
930 if (!(entry->flags & (G_OPTION_FLAG_NO_ARG|G_OPTION_FLAG_OPTIONAL_ARG)) &&
934 retval = (* (GOptionArgFunc) entry->arg_data) (option_name, data, group->user_data, error);
942 case G_OPTION_ARG_DOUBLE:
946 if (!parse_double (option_name, value,
953 change = get_change (context, G_OPTION_ARG_DOUBLE,
955 change->prev.dbl = *(gdouble *)entry->arg_data;
956 *(gdouble *)entry->arg_data = data;
960 g_assert_not_reached ();
967 parse_short_option (GOptionContext *context,
979 for (j = 0; j < group->n_entries; j++)
981 if (arg == group->entries[j].short_name)
986 option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
988 if (NO_ARG (&group->entries[j]))
992 if (*new_index > index)
995 G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
996 _("Error parsing option %s"), option_name);
997 g_free (option_name);
1001 if (index < *argc - 1)
1003 if (!OPTIONAL_ARG (&group->entries[j]))
1005 value = (*argv)[index + 1];
1006 add_pending_null (context, &((*argv)[index + 1]), NULL);
1007 *new_index = index+1;
1011 if ((*argv)[index + 1][0] == '-')
1015 value = (*argv)[index + 1];
1016 add_pending_null (context, &((*argv)[index + 1]), NULL);
1017 *new_index = index + 1;
1021 else if (index >= *argc - 1 && OPTIONAL_ARG (&group->entries[j]))
1026 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1027 _("Missing argument for %s"), option_name);
1028 g_free (option_name);
1033 if (!parse_arg (context, group, &group->entries[j],
1034 value, option_name, error))
1036 g_free (option_name);
1040 g_free (option_name);
1049 parse_long_option (GOptionContext *context,
1050 GOptionGroup *group,
1061 for (j = 0; j < group->n_entries; j++)
1063 if (*index >= *argc)
1066 if (aliased && (group->entries[j].flags & G_OPTION_FLAG_NOALIAS))
1069 if (NO_ARG (&group->entries[j]) &&
1070 strcmp (arg, group->entries[j].long_name) == 0)
1074 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1075 parse_arg (context, group, &group->entries[j],
1076 NULL, option_name, error);
1077 g_free(option_name);
1079 add_pending_null (context, &((*argv)[*index]), NULL);
1084 gint len = strlen (group->entries[j].long_name);
1086 if (strncmp (arg, group->entries[j].long_name, len) == 0 &&
1087 (arg[len] == '=' || arg[len] == 0))
1089 gchar *value = NULL;
1092 add_pending_null (context, &((*argv)[*index]), NULL);
1093 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1095 if (arg[len] == '=')
1096 value = arg + len + 1;
1097 else if (*index < *argc - 1)
1099 if (!(group->entries[j].flags & G_OPTION_FLAG_OPTIONAL_ARG))
1101 value = (*argv)[*index + 1];
1102 add_pending_null (context, &((*argv)[*index + 1]), NULL);
1107 if ((*argv)[*index + 1][0] == '-')
1110 retval = parse_arg (context, group, &group->entries[j],
1111 NULL, option_name, error);
1113 g_free (option_name);
1118 value = (*argv)[*index + 1];
1119 add_pending_null (context, &((*argv)[*index + 1]), NULL);
1124 else if (*index >= *argc - 1 &&
1125 group->entries[j].flags & G_OPTION_FLAG_OPTIONAL_ARG)
1128 retval = parse_arg (context, group, &group->entries[j],
1129 NULL, option_name, error);
1131 g_free (option_name);
1137 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1138 _("Missing argument for %s"), option_name);
1139 g_free (option_name);
1143 if (!parse_arg (context, group, &group->entries[j],
1144 value, option_name, error))
1146 g_free (option_name);
1150 g_free (option_name);
1160 parse_remaining_arg (GOptionContext *context,
1161 GOptionGroup *group,
1170 for (j = 0; j < group->n_entries; j++)
1172 if (*index >= *argc)
1175 if (group->entries[j].long_name[0])
1178 g_return_val_if_fail (group->entries[j].arg == G_OPTION_ARG_STRING_ARRAY ||
1179 group->entries[j].arg == G_OPTION_ARG_FILENAME_ARRAY, FALSE);
1181 add_pending_null (context, &((*argv)[*index]), NULL);
1183 if (!parse_arg (context, group, &group->entries[j], (*argv)[*index], "", error))
1194 free_changes_list (GOptionContext *context,
1199 for (list = context->changes; list != NULL; list = list->next)
1201 Change *change = list->data;
1205 switch (change->arg_type)
1207 case G_OPTION_ARG_NONE:
1208 *(gboolean *)change->arg_data = change->prev.bool;
1210 case G_OPTION_ARG_INT:
1211 *(gint *)change->arg_data = change->prev.integer;
1213 case G_OPTION_ARG_STRING:
1214 case G_OPTION_ARG_FILENAME:
1215 g_free (change->allocated.str);
1216 *(gchar **)change->arg_data = change->prev.str;
1218 case G_OPTION_ARG_STRING_ARRAY:
1219 case G_OPTION_ARG_FILENAME_ARRAY:
1220 g_strfreev (change->allocated.array.data);
1221 *(gchar ***)change->arg_data = change->prev.array;
1223 case G_OPTION_ARG_DOUBLE:
1224 *(gdouble *)change->arg_data = change->prev.dbl;
1227 g_assert_not_reached ();
1234 g_list_free (context->changes);
1235 context->changes = NULL;
1239 free_pending_nulls (GOptionContext *context,
1240 gboolean perform_nulls)
1244 for (list = context->pending_nulls; list != NULL; list = list->next)
1246 PendingNull *n = list->data;
1252 /* Copy back the short options */
1254 strcpy (*n->ptr + 1, n->value);
1264 g_list_free (context->pending_nulls);
1265 context->pending_nulls = NULL;
1269 * g_option_context_parse:
1270 * @context: a #GOptionContext
1271 * @argc: a pointer to the number of command line arguments.
1272 * @argv: a pointer to the array of command line arguments.
1273 * @error: a return location for errors
1275 * Parses the command line arguments, recognizing options
1276 * which have been added to @context. A side-effect of
1277 * calling this function is that g_set_prgname() will be
1280 * If the parsing is successful, any parsed arguments are
1281 * removed from the array and @argc and @argv are updated
1282 * accordingly. A '--' option is stripped from @argv
1283 * unless there are unparsed options before and after it,
1284 * or some of the options after it start with '-'. In case
1285 * of an error, @argc and @argv are left unmodified.
1287 * If automatic <option>--help</option> support is enabled
1288 * (see g_option_context_set_help_enabled()), and the
1289 * @argv array contains one of the recognized help options,
1290 * this function will produce help output to stdout and
1291 * call <literal>exit (0)</literal>.
1293 * Return value: %TRUE if the parsing was successful,
1294 * %FALSE if an error occurred
1299 g_option_context_parse (GOptionContext *context,
1307 /* Set program name */
1308 if (!g_get_prgname())
1310 if (argc && argv && *argc)
1314 prgname = g_path_get_basename ((*argv)[0]);
1315 g_set_prgname (prgname);
1319 g_set_prgname ("<unknown>");
1322 /* Call pre-parse hooks */
1323 list = context->groups;
1326 GOptionGroup *group = list->data;
1328 if (group->pre_parse_func)
1330 if (!(* group->pre_parse_func) (context, group,
1331 group->user_data, error))
1338 if (context->main_group && context->main_group->pre_parse_func)
1340 if (!(* context->main_group->pre_parse_func) (context, context->main_group,
1341 context->main_group->user_data, error))
1347 gboolean stop_parsing = FALSE;
1348 gboolean has_unknown = FALSE;
1349 gint separator_pos = 0;
1351 for (i = 1; i < *argc; i++)
1354 gboolean parsed = FALSE;
1356 if ((*argv)[i][0] == '-' && (*argv)[i][1] != '\0' && !stop_parsing)
1358 if ((*argv)[i][1] == '-')
1362 arg = (*argv)[i] + 2;
1364 /* '--' terminates list of arguments */
1368 stop_parsing = TRUE;
1372 /* Handle help options */
1373 if (context->help_enabled)
1375 if (strcmp (arg, "help") == 0)
1376 print_help (context, TRUE, NULL);
1377 else if (strcmp (arg, "help-all") == 0)
1378 print_help (context, FALSE, NULL);
1379 else if (strncmp (arg, "help-", 5) == 0)
1383 list = context->groups;
1387 GOptionGroup *group = list->data;
1389 if (strcmp (arg + 5, group->name) == 0)
1390 print_help (context, FALSE, group);
1397 if (context->main_group &&
1398 !parse_long_option (context, context->main_group, &i, arg,
1399 FALSE, argc, argv, error, &parsed))
1405 /* Try the groups */
1406 list = context->groups;
1409 GOptionGroup *group = list->data;
1411 if (!parse_long_option (context, group, &i, arg,
1412 FALSE, argc, argv, error, &parsed))
1424 /* Now look for --<group>-<option> */
1425 dash = strchr (arg, '-');
1428 /* Try the groups */
1429 list = context->groups;
1432 GOptionGroup *group = list->data;
1434 if (strncmp (group->name, arg, dash - arg) == 0)
1436 if (!parse_long_option (context, group, &i, dash + 1,
1437 TRUE, argc, argv, error, &parsed))
1448 if (context->ignore_unknown)
1456 gboolean *nulled_out = NULL;
1458 arg = (*argv)[i] + 1;
1462 if (context->ignore_unknown)
1463 nulled_out = g_new0 (gboolean, strlen (arg));
1465 for (j = 0; j < strlen (arg); j++)
1467 if (context->help_enabled && arg[j] == '?')
1468 print_help (context, TRUE, NULL);
1472 if (context->main_group &&
1473 !parse_short_option (context, context->main_group,
1475 argc, argv, error, &parsed))
1478 g_free (nulled_out);
1484 /* Try the groups */
1485 list = context->groups;
1488 GOptionGroup *group = list->data;
1490 if (!parse_short_option (context, group, i, &new_i, arg[j],
1491 argc, argv, error, &parsed))
1501 if (context->ignore_unknown)
1504 nulled_out[j] = TRUE;
1513 if (context->ignore_unknown)
1515 gchar *new_arg = NULL;
1518 for (j = 0; j < strlen (arg); j++)
1523 new_arg = g_malloc (strlen (arg) + 1);
1524 new_arg[arg_index++] = arg[j];
1528 new_arg[arg_index] = '\0';
1530 add_pending_null (context, &((*argv)[i]), new_arg);
1534 add_pending_null (context, &((*argv)[i]), NULL);
1542 if (!parsed && !context->ignore_unknown)
1545 G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
1546 _("Unknown option %s"), (*argv)[i]);
1552 /* Collect remaining args */
1553 if (context->main_group &&
1554 !parse_remaining_arg (context, context->main_group, &i,
1555 argc, argv, error, &parsed))
1558 if (!parsed && (has_unknown || (*argv)[i][0] == '-'))
1563 if (separator_pos > 0)
1564 add_pending_null (context, &((*argv)[separator_pos]), NULL);
1568 /* Call post-parse hooks */
1569 list = context->groups;
1572 GOptionGroup *group = list->data;
1574 if (group->post_parse_func)
1576 if (!(* group->post_parse_func) (context, group,
1577 group->user_data, error))
1584 if (context->main_group && context->main_group->post_parse_func)
1586 if (!(* context->main_group->post_parse_func) (context, context->main_group,
1587 context->main_group->user_data, error))
1593 free_pending_nulls (context, TRUE);
1595 for (i = 1; i < *argc; i++)
1597 for (k = i; k < *argc; k++)
1598 if ((*argv)[k] != NULL)
1604 for (j = i + k; j < *argc; j++)
1606 (*argv)[j-k] = (*argv)[j];
1618 /* Call error hooks */
1619 list = context->groups;
1622 GOptionGroup *group = list->data;
1624 if (group->error_func)
1625 (* group->error_func) (context, group,
1626 group->user_data, error);
1631 if (context->main_group && context->main_group->error_func)
1632 (* context->main_group->error_func) (context, context->main_group,
1633 context->main_group->user_data, error);
1635 free_changes_list (context, TRUE);
1636 free_pending_nulls (context, FALSE);
1642 * g_option_group_new:
1643 * @name: the name for the option group, this is used to provide
1644 * help for the options in this group with <option>--help-</option>@name
1645 * @description: a description for this group to be shown in
1646 * <option>--help</option>. This string is translated using the translation
1647 * domain or translation function of the group
1648 * @help_description: a description for the <option>--help-</option>@name option.
1649 * This string is translated using the translation domain or translation function
1651 * @user_data: user data that will be passed to the pre- and post-parse hooks,
1652 * the error hook and to callbacks of %G_OPTION_ARG_CALLBACK options, or %NULL
1653 * @destroy: a function that will be called to free @user_data, or %NULL
1655 * Creates a new #GOptionGroup.
1657 * Return value: a newly created option group. It should be added
1658 * to a #GOptionContext or freed with g_option_group_free().
1663 g_option_group_new (const gchar *name,
1664 const gchar *description,
1665 const gchar *help_description,
1667 GDestroyNotify destroy)
1670 GOptionGroup *group;
1672 group = g_new0 (GOptionGroup, 1);
1673 group->name = g_strdup (name);
1674 group->description = g_strdup (description);
1675 group->help_description = g_strdup (help_description);
1676 group->user_data = user_data;
1677 group->destroy_notify = destroy;
1684 * g_option_group_free:
1685 * @group: a #GOptionGroup
1687 * Frees a #GOptionGroup. Note that you must <emphasis>not</emphasis>
1688 * free groups which have been added to a #GOptionContext.
1693 g_option_group_free (GOptionGroup *group)
1695 g_return_if_fail (group != NULL);
1697 g_free (group->name);
1698 g_free (group->description);
1699 g_free (group->help_description);
1701 g_free (group->entries);
1703 if (group->destroy_notify)
1704 (* group->destroy_notify) (group->user_data);
1706 if (group->translate_notify)
1707 (* group->translate_notify) (group->translate_data);
1714 * g_option_group_add_entries:
1715 * @group: a #GOptionGroup
1716 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
1718 * Adds the options specified in @entries to @group.
1723 g_option_group_add_entries (GOptionGroup *group,
1724 const GOptionEntry *entries)
1728 g_return_if_fail (entries != NULL);
1730 for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++) ;
1732 group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
1734 memcpy (group->entries + group->n_entries, entries, sizeof (GOptionEntry) * n_entries);
1736 for (i = group->n_entries; i < group->n_entries + n_entries; i++)
1738 gchar c = group->entries[i].short_name;
1742 if (c == '-' || !g_ascii_isprint (c))
1744 g_warning (G_STRLOC": ignoring invalid short option '%c' (%d)", c, c);
1745 group->entries[i].short_name = 0;
1750 group->n_entries += n_entries;
1754 * g_option_group_set_parse_hooks:
1755 * @group: a #GOptionGroup
1756 * @pre_parse_func: a function to call before parsing, or %NULL
1757 * @post_parse_func: a function to call after parsing, or %NULL
1759 * Associates two functions with @group which will be called
1760 * from g_option_context_parse() before the first option is parsed
1761 * and after the last option has been parsed, respectively.
1763 * Note that the user data to be passed to @pre_parse_func and
1764 * @post_parse_func can be specified when constructing the group
1765 * with g_option_group_new().
1770 g_option_group_set_parse_hooks (GOptionGroup *group,
1771 GOptionParseFunc pre_parse_func,
1772 GOptionParseFunc post_parse_func)
1774 g_return_if_fail (group != NULL);
1776 group->pre_parse_func = pre_parse_func;
1777 group->post_parse_func = post_parse_func;
1781 * g_option_group_set_error_hook:
1782 * @group: a #GOptionGroup
1783 * @error_func: a function to call when an error occurs
1785 * Associates a function with @group which will be called
1786 * from g_option_context_parse() when an error occurs.
1788 * Note that the user data to be passed to @pre_parse_func and
1789 * @post_parse_func can be specified when constructing the group
1790 * with g_option_group_new().
1795 g_option_group_set_error_hook (GOptionGroup *group,
1796 GOptionErrorFunc error_func)
1798 g_return_if_fail (group != NULL);
1800 group->error_func = error_func;
1805 * g_option_group_set_translate_func:
1806 * @group: a #GOptionGroup
1807 * @func: the #GTranslateFunc, or %NULL
1808 * @data: user data to pass to @func, or %NULL
1809 * @destroy_notify: a function which gets called to free @data, or %NULL
1811 * Sets the function which is used to translate user-visible
1812 * strings, for <option>--help</option> output. Different
1813 * groups can use different #GTranslateFunc<!-- -->s. If @func
1814 * is %NULL, strings are not translated.
1816 * If you are using gettext(), you only need to set the translation
1817 * domain, see g_option_group_set_translation_domain().
1822 g_option_group_set_translate_func (GOptionGroup *group,
1823 GTranslateFunc func,
1825 GDestroyNotify destroy_notify)
1827 g_return_if_fail (group != NULL);
1829 if (group->translate_notify)
1830 group->translate_notify (group->translate_data);
1832 group->translate_func = func;
1833 group->translate_data = data;
1834 group->translate_notify = destroy_notify;
1838 dgettext_swapped (const gchar *msgid,
1839 const gchar *domainname)
1841 return dgettext (domainname, msgid);
1845 * g_option_group_set_translation_domain:
1846 * @group: a #GOptionGroup
1847 * @domain: the domain to use
1849 * A convenience function to use gettext() for translating
1850 * user-visible strings.
1855 g_option_group_set_translation_domain (GOptionGroup *group,
1856 const gchar *domain)
1858 g_return_if_fail (group != NULL);
1860 g_option_group_set_translate_func (group,
1861 (GTranslateFunc)dgettext_swapped,
1866 #define __G_OPTION_C__
1867 #include "galiasdef.c"