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)
121 return g_quark_from_static_string ("g-option-context-error-quark");
125 * g_option_context_new:
126 * @parameter_string: a string which is displayed in
127 * the first line of <option>--help</option> output, after the
129 * <literal><replaceable>programname</replaceable> [OPTION...]</literal>
131 * Creates a new option context.
133 * The @parameter_text can serve multiple purposes. It can be used
134 * to add descriptions for "rest" arguments, which are not parsed by
135 * the #GOptionContext, typically something like "FILES" or
136 * "FILE1 FILE2...". (If you are using #G_OPTION_REMAINING for
137 * collecting "rest" arguments, GLib handles this automatically by
138 * using the @arg_description of the corresponding #GOptionEntry in
139 * the usage summary.)
141 * Another common usage is to give a summary of the program
142 * functionality. This can be a short summary on the same line,
143 * like " - frob the strings", or a longer description in a paragraph
144 * below the usage summary. In this case, @parameter_string should start
145 * with two newlines, to separate the description from the usage summary:
146 * "\n\nA program to frob strings, which will..."
148 * Returns: a newly created #GOptionContext, which must be
149 * freed with g_option_context_free() after use.
154 g_option_context_new (const gchar *parameter_string)
157 GOptionContext *context;
159 context = g_new0 (GOptionContext, 1);
161 context->parameter_string = g_strdup (parameter_string);
162 context->help_enabled = TRUE;
163 context->ignore_unknown = FALSE;
169 * g_option_context_free:
170 * @context: a #GOptionContext
172 * Frees context and all the groups which have been
177 void g_option_context_free (GOptionContext *context)
179 g_return_if_fail (context != NULL);
181 g_list_foreach (context->groups, (GFunc)g_option_group_free, NULL);
182 g_list_free (context->groups);
184 if (context->main_group)
185 g_option_group_free (context->main_group);
187 free_changes_list (context, FALSE);
188 free_pending_nulls (context, FALSE);
190 g_free (context->parameter_string);
197 * g_option_context_set_help_enabled:
198 * @context: a #GOptionContext
199 * @help_enabled: %TRUE to enable <option>--help</option>, %FALSE to disable it
201 * Enables or disables automatic generation of <option>--help</option>
202 * output. By default, g_option_context_parse() recognizes
203 * <option>--help</option>, <option>-?</option>, <option>--help-all</option>
204 * and <option>--help-</option><replaceable>groupname</replaceable> and creates
205 * suitable output to stdout.
209 void g_option_context_set_help_enabled (GOptionContext *context,
210 gboolean help_enabled)
213 g_return_if_fail (context != NULL);
215 context->help_enabled = help_enabled;
219 * g_option_context_get_help_enabled:
220 * @context: a #GOptionContext
222 * Returns whether automatic <option>--help</option> generation
223 * is turned on for @context. See g_option_context_set_help_enabled().
225 * Returns: %TRUE if automatic help generation is turned on.
230 g_option_context_get_help_enabled (GOptionContext *context)
232 g_return_val_if_fail (context != NULL, FALSE);
234 return context->help_enabled;
238 * g_option_context_set_ignore_unknown_options:
239 * @context: a #GOptionContext
240 * @ignore_unknown: %TRUE to ignore unknown options, %FALSE to produce
241 * an error when unknown options are met
243 * Sets whether to ignore unknown options or not. If an argument is
244 * ignored, it is left in the @argv array after parsing. By default,
245 * g_option_context_parse() treats unknown options as error.
247 * This setting does not affect non-option arguments (i.e. arguments
248 * which don't start with a dash). But note that GOption cannot reliably
249 * determine whether a non-option belongs to a preceding unknown option.
254 g_option_context_set_ignore_unknown_options (GOptionContext *context,
255 gboolean ignore_unknown)
257 g_return_if_fail (context != NULL);
259 context->ignore_unknown = ignore_unknown;
263 * g_option_context_get_ignore_unknown_options:
264 * @context: a #GOptionContext
266 * Returns whether unknown options are ignored or not. See
267 * g_option_context_set_ignore_unknown_options().
269 * Returns: %TRUE if unknown options are ignored.
274 g_option_context_get_ignore_unknown_options (GOptionContext *context)
276 g_return_val_if_fail (context != NULL, FALSE);
278 return context->ignore_unknown;
282 * g_option_context_add_group:
283 * @context: a #GOptionContext
284 * @group: the group to add
286 * Adds a #GOptionGroup to the @context, so that parsing with @context
287 * will recognize the options in the group. Note that the group will
288 * be freed together with the context when g_option_context_free() is
289 * called, so you must not free the group yourself after adding it
295 g_option_context_add_group (GOptionContext *context,
300 g_return_if_fail (context != NULL);
301 g_return_if_fail (group != NULL);
302 g_return_if_fail (group->name != NULL);
303 g_return_if_fail (group->description != NULL);
304 g_return_if_fail (group->help_description != NULL);
306 for (list = context->groups; list; list = list->next)
308 GOptionGroup *g = (GOptionGroup *)list->data;
310 if ((group->name == NULL && g->name == NULL) ||
311 (group->name && g->name && strcmp (group->name, g->name) == 0))
312 g_warning ("A group named \"%s\" is already part of this GOptionContext",
316 context->groups = g_list_append (context->groups, group);
320 * g_option_context_set_main_group:
321 * @context: a #GOptionContext
322 * @group: the group to set as main group
324 * Sets a #GOptionGroup as main group of the @context.
325 * This has the same effect as calling g_option_context_add_group(),
326 * the only difference is that the options in the main group are
327 * treated differently when generating <option>--help</option> output.
332 g_option_context_set_main_group (GOptionContext *context,
335 g_return_if_fail (context != NULL);
336 g_return_if_fail (group != NULL);
338 if (context->main_group)
340 g_warning ("This GOptionContext already has a main group");
345 context->main_group = group;
349 * g_option_context_get_main_group:
350 * @context: a #GOptionContext
352 * Returns a pointer to the main group of @context.
354 * Return value: the main group of @context, or %NULL if @context doesn't
355 * have a main group. Note that group belongs to @context and should
356 * not be modified or freed.
361 g_option_context_get_main_group (GOptionContext *context)
363 g_return_val_if_fail (context != NULL, NULL);
365 return context->main_group;
369 * g_option_context_add_main_entries:
370 * @context: a #GOptionContext
371 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
372 * @translation_domain: a translation domain to use for translating
373 * the <option>--help</option> output for the options in @entries
374 * with gettext(), or %NULL
376 * A convenience function which creates a main group if it doesn't
377 * exist, adds the @entries to it and sets the translation domain.
382 g_option_context_add_main_entries (GOptionContext *context,
383 const GOptionEntry *entries,
384 const gchar *translation_domain)
386 g_return_if_fail (entries != NULL);
388 if (!context->main_group)
389 context->main_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
391 g_option_group_add_entries (context->main_group, entries);
392 g_option_group_set_translation_domain (context->main_group, translation_domain);
396 calculate_max_length (GOptionGroup *group)
399 gint i, len, max_length;
403 for (i = 0; i < group->n_entries; i++)
405 entry = &group->entries[i];
407 if (entry->flags & G_OPTION_FLAG_HIDDEN)
410 len = g_utf8_strlen (entry->long_name, -1);
412 if (entry->short_name)
415 if (!NO_ARG (entry) && entry->arg_description)
416 len += 1 + g_utf8_strlen (TRANSLATE (group, entry->arg_description), -1);
418 max_length = MAX (max_length, len);
425 print_entry (GOptionGroup *group,
427 const GOptionEntry *entry)
431 if (entry->flags & G_OPTION_FLAG_HIDDEN)
434 if (entry->long_name[0] == 0)
437 str = g_string_new (NULL);
439 if (entry->short_name)
440 g_string_append_printf (str, " -%c, --%s", entry->short_name, entry->long_name);
442 g_string_append_printf (str, " --%s", entry->long_name);
444 if (entry->arg_description)
445 g_string_append_printf (str, "=%s", TRANSLATE (group, entry->arg_description));
447 g_print ("%-*s %s\n", max_length + 4, str->str,
448 entry->description ? TRANSLATE (group, entry->description) : "");
449 g_string_free (str, TRUE);
453 print_help (GOptionContext *context,
458 gint max_length, len;
461 GHashTable *shadow_map;
463 const gchar *rest_description;
465 rest_description = NULL;
466 if (context->main_group)
468 for (i = 0; i < context->main_group->n_entries; i++)
470 entry = &context->main_group->entries[i];
471 if (entry->long_name[0] == 0)
473 rest_description = entry->arg_description;
479 g_print ("%s\n %s %s%s%s%s%s\n\n",
480 _("Usage:"), g_get_prgname(), _("[OPTION...]"),
481 rest_description ? " " : "",
482 rest_description ? rest_description : "",
483 context->parameter_string ? " " : "",
484 context->parameter_string ? context->parameter_string : "");
486 memset (seen, 0, sizeof (gboolean) * 256);
487 shadow_map = g_hash_table_new (g_str_hash, g_str_equal);
489 if (context->main_group)
491 for (i = 0; i < context->main_group->n_entries; i++)
493 entry = &context->main_group->entries[i];
494 g_hash_table_insert (shadow_map,
495 (gpointer)entry->long_name,
498 if (seen[(guchar)entry->short_name])
499 entry->short_name = 0;
501 seen[(guchar)entry->short_name] = TRUE;
505 list = context->groups;
508 GOptionGroup *group = list->data;
509 for (i = 0; i < group->n_entries; i++)
511 entry = &group->entries[i];
512 if (g_hash_table_lookup (shadow_map, entry->long_name) &&
513 !(entry->flags && G_OPTION_FLAG_NOALIAS))
514 entry->long_name = g_strdup_printf ("%s-%s", group->name, entry->long_name);
516 g_hash_table_insert (shadow_map, (gpointer)entry->long_name, entry);
518 if (seen[(guchar)entry->short_name] &&
519 !(entry->flags && G_OPTION_FLAG_NOALIAS))
520 entry->short_name = 0;
522 seen[(guchar)entry->short_name] = TRUE;
527 g_hash_table_destroy (shadow_map);
529 list = context->groups;
531 max_length = g_utf8_strlen ("-?, --help", -1);
535 len = g_utf8_strlen ("--help-all", -1);
536 max_length = MAX (max_length, len);
539 if (context->main_group)
541 len = calculate_max_length (context->main_group);
542 max_length = MAX (max_length, len);
547 GOptionGroup *group = list->data;
549 /* First, we check the --help-<groupname> options */
550 len = g_utf8_strlen ("--help-", -1) + g_utf8_strlen (group->name, -1);
551 max_length = MAX (max_length, len);
553 /* Then we go through the entries */
554 len = calculate_max_length (group);
555 max_length = MAX (max_length, len);
560 /* Add a bit of padding */
565 list = context->groups;
567 g_print ("%s\n -%c, --%-*s %s\n",
568 _("Help Options:"), '?', max_length - 4, "help",
569 _("Show help options"));
571 /* We only want --help-all when there are groups */
573 g_print (" --%-*s %s\n", max_length, "help-all",
574 _("Show all help options"));
578 GOptionGroup *group = list->data;
580 g_print (" --help-%-*s %s\n", max_length - 5, group->name,
581 TRANSLATE (group, group->help_description));
591 /* Print a certain group */
593 g_print ("%s\n", TRANSLATE (group, group->description));
594 for (i = 0; i < group->n_entries; i++)
595 print_entry (group, max_length, &group->entries[i]);
600 /* Print all groups */
602 list = context->groups;
606 GOptionGroup *group = list->data;
608 g_print ("%s\n", group->description);
610 for (i = 0; i < group->n_entries; i++)
611 if (!(group->entries[i].flags & G_OPTION_FLAG_IN_MAIN))
612 print_entry (group, max_length, &group->entries[i]);
619 /* Print application options if --help or --help-all has been specified */
620 if (main_help || !group)
622 list = context->groups;
624 g_print ("%s\n", _("Application Options:"));
626 if (context->main_group)
627 for (i = 0; i < context->main_group->n_entries; i++)
628 print_entry (context->main_group, max_length,
629 &context->main_group->entries[i]);
633 GOptionGroup *group = list->data;
635 /* Print main entries from other groups */
636 for (i = 0; i < group->n_entries; i++)
637 if (group->entries[i].flags & G_OPTION_FLAG_IN_MAIN)
638 print_entry (group, max_length, &group->entries[i]);
650 parse_int (const gchar *arg_name,
659 tmp = strtol (arg, &end, 0);
661 if (*arg == '\0' || *end != '\0')
664 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
665 _("Cannot parse integer value '%s' for %s"),
671 if (*result != tmp || errno == ERANGE)
674 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
675 _("Integer value '%s' for %s out of range"),
685 parse_double (const gchar *arg_name,
694 tmp = g_strtod (arg, &end);
696 if (*arg == '\0' || *end != '\0')
699 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
700 _("Cannot parse double value '%s' for %s"),
707 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
708 _("Double value '%s' for %s out of range"),
720 get_change (GOptionContext *context,
725 Change *change = NULL;
727 for (list = context->changes; list != NULL; list = list->next)
731 if (change->arg_data == arg_data)
735 change = g_new0 (Change, 1);
736 change->arg_type = arg_type;
737 change->arg_data = arg_data;
739 context->changes = g_list_prepend (context->changes, change);
747 add_pending_null (GOptionContext *context,
753 n = g_new0 (PendingNull, 1);
757 context->pending_nulls = g_list_prepend (context->pending_nulls, n);
761 parse_arg (GOptionContext *context,
765 const gchar *option_name,
773 case G_OPTION_ARG_NONE:
775 change = get_change (context, G_OPTION_ARG_NONE,
778 *(gboolean *)entry->arg_data = !(entry->flags & G_OPTION_FLAG_REVERSE);
781 case G_OPTION_ARG_STRING:
785 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
790 change = get_change (context, G_OPTION_ARG_STRING,
792 g_free (change->allocated.str);
794 change->prev.str = *(gchar **)entry->arg_data;
795 change->allocated.str = data;
797 *(gchar **)entry->arg_data = data;
800 case G_OPTION_ARG_STRING_ARRAY:
804 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
809 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
812 if (change->allocated.array.len == 0)
814 change->prev.array = *(gchar ***)entry->arg_data;
815 change->allocated.array.data = g_new (gchar *, 2);
818 change->allocated.array.data =
819 g_renew (gchar *, change->allocated.array.data,
820 change->allocated.array.len + 2);
822 change->allocated.array.data[change->allocated.array.len] = data;
823 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
825 change->allocated.array.len ++;
827 *(gchar ***)entry->arg_data = change->allocated.array.data;
832 case G_OPTION_ARG_FILENAME:
837 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
842 data = g_strdup (value);
844 change = get_change (context, G_OPTION_ARG_FILENAME,
846 g_free (change->allocated.str);
848 change->prev.str = *(gchar **)entry->arg_data;
849 change->allocated.str = data;
851 *(gchar **)entry->arg_data = data;
855 case G_OPTION_ARG_FILENAME_ARRAY:
860 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
865 data = g_strdup (value);
867 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
870 if (change->allocated.array.len == 0)
872 change->prev.array = *(gchar ***)entry->arg_data;
873 change->allocated.array.data = g_new (gchar *, 2);
876 change->allocated.array.data =
877 g_renew (gchar *, change->allocated.array.data,
878 change->allocated.array.len + 2);
880 change->allocated.array.data[change->allocated.array.len] = data;
881 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
883 change->allocated.array.len ++;
885 *(gchar ***)entry->arg_data = change->allocated.array.data;
890 case G_OPTION_ARG_INT:
894 if (!parse_int (option_name, value,
899 change = get_change (context, G_OPTION_ARG_INT,
901 change->prev.integer = *(gint *)entry->arg_data;
902 *(gint *)entry->arg_data = data;
905 case G_OPTION_ARG_CALLBACK:
910 if (!value && entry->flags & G_OPTION_FLAG_OPTIONAL_ARG)
912 else if (entry->flags & G_OPTION_FLAG_NO_ARG)
914 else if (entry->flags & G_OPTION_FLAG_FILENAME)
917 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
919 data = g_strdup (value);
923 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
925 if (!(entry->flags & (G_OPTION_FLAG_NO_ARG|G_OPTION_FLAG_OPTIONAL_ARG)) &&
929 retval = (* (GOptionArgFunc) entry->arg_data) (option_name, data, group->user_data, error);
937 case G_OPTION_ARG_DOUBLE:
941 if (!parse_double (option_name, value,
948 change = get_change (context, G_OPTION_ARG_DOUBLE,
950 change->prev.dbl = *(gdouble *)entry->arg_data;
951 *(gdouble *)entry->arg_data = data;
955 g_assert_not_reached ();
962 parse_short_option (GOptionContext *context,
974 for (j = 0; j < group->n_entries; j++)
976 if (arg == group->entries[j].short_name)
981 option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
983 if (NO_ARG (&group->entries[j]))
987 if (*new_index > index)
990 G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
991 _("Error parsing option %s"), option_name);
992 g_free (option_name);
996 if (index < *argc - 1)
998 if (!OPTIONAL_ARG (&group->entries[j]))
1000 value = (*argv)[index + 1];
1001 add_pending_null (context, &((*argv)[index + 1]), NULL);
1002 *new_index = index+1;
1006 if ((*argv)[index + 1][0] == '-')
1010 value = (*argv)[index + 1];
1011 add_pending_null (context, &((*argv)[index + 1]), NULL);
1012 *new_index = index + 1;
1016 else if (index >= *argc - 1 && OPTIONAL_ARG (&group->entries[j]))
1021 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1022 _("Missing argument for %s"), option_name);
1023 g_free (option_name);
1028 if (!parse_arg (context, group, &group->entries[j],
1029 value, option_name, error))
1031 g_free (option_name);
1035 g_free (option_name);
1044 parse_long_option (GOptionContext *context,
1045 GOptionGroup *group,
1056 for (j = 0; j < group->n_entries; j++)
1058 if (*index >= *argc)
1061 if (aliased && (group->entries[j].flags & G_OPTION_FLAG_NOALIAS))
1064 if (NO_ARG (&group->entries[j]) &&
1065 strcmp (arg, group->entries[j].long_name) == 0)
1069 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1070 parse_arg (context, group, &group->entries[j],
1071 NULL, option_name, error);
1072 g_free(option_name);
1074 add_pending_null (context, &((*argv)[*index]), NULL);
1079 gint len = strlen (group->entries[j].long_name);
1081 if (strncmp (arg, group->entries[j].long_name, len) == 0 &&
1082 (arg[len] == '=' || arg[len] == 0))
1084 gchar *value = NULL;
1087 add_pending_null (context, &((*argv)[*index]), NULL);
1088 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1090 if (arg[len] == '=')
1091 value = arg + len + 1;
1092 else if (*index < *argc - 1)
1094 if (!(group->entries[j].flags & G_OPTION_FLAG_OPTIONAL_ARG))
1096 value = (*argv)[*index + 1];
1097 add_pending_null (context, &((*argv)[*index + 1]), NULL);
1102 if ((*argv)[*index + 1][0] == '-')
1105 retval = parse_arg (context, group, &group->entries[j],
1106 NULL, option_name, error);
1108 g_free (option_name);
1113 value = (*argv)[*index + 1];
1114 add_pending_null (context, &((*argv)[*index + 1]), NULL);
1119 else if (*index >= *argc - 1 &&
1120 group->entries[j].flags & G_OPTION_FLAG_OPTIONAL_ARG)
1123 retval = parse_arg (context, group, &group->entries[j],
1124 NULL, option_name, error);
1126 g_free (option_name);
1132 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1133 _("Missing argument for %s"), option_name);
1134 g_free (option_name);
1138 if (!parse_arg (context, group, &group->entries[j],
1139 value, option_name, error))
1141 g_free (option_name);
1145 g_free (option_name);
1155 parse_remaining_arg (GOptionContext *context,
1156 GOptionGroup *group,
1165 for (j = 0; j < group->n_entries; j++)
1167 if (*index >= *argc)
1170 if (group->entries[j].long_name[0])
1173 g_return_val_if_fail (group->entries[j].arg == G_OPTION_ARG_STRING_ARRAY ||
1174 group->entries[j].arg == G_OPTION_ARG_FILENAME_ARRAY, FALSE);
1176 add_pending_null (context, &((*argv)[*index]), NULL);
1178 if (!parse_arg (context, group, &group->entries[j], (*argv)[*index], "", error))
1189 free_changes_list (GOptionContext *context,
1194 for (list = context->changes; list != NULL; list = list->next)
1196 Change *change = list->data;
1200 switch (change->arg_type)
1202 case G_OPTION_ARG_NONE:
1203 *(gboolean *)change->arg_data = change->prev.bool;
1205 case G_OPTION_ARG_INT:
1206 *(gint *)change->arg_data = change->prev.integer;
1208 case G_OPTION_ARG_STRING:
1209 case G_OPTION_ARG_FILENAME:
1210 g_free (change->allocated.str);
1211 *(gchar **)change->arg_data = change->prev.str;
1213 case G_OPTION_ARG_STRING_ARRAY:
1214 case G_OPTION_ARG_FILENAME_ARRAY:
1215 g_strfreev (change->allocated.array.data);
1216 *(gchar ***)change->arg_data = change->prev.array;
1218 case G_OPTION_ARG_DOUBLE:
1219 *(gdouble *)change->arg_data = change->prev.dbl;
1222 g_assert_not_reached ();
1229 g_list_free (context->changes);
1230 context->changes = NULL;
1234 free_pending_nulls (GOptionContext *context,
1235 gboolean perform_nulls)
1239 for (list = context->pending_nulls; list != NULL; list = list->next)
1241 PendingNull *n = list->data;
1247 /* Copy back the short options */
1249 strcpy (*n->ptr + 1, n->value);
1259 g_list_free (context->pending_nulls);
1260 context->pending_nulls = NULL;
1264 * g_option_context_parse:
1265 * @context: a #GOptionContext
1266 * @argc: a pointer to the number of command line arguments.
1267 * @argv: a pointer to the array of command line arguments.
1268 * @error: a return location for errors
1270 * Parses the command line arguments, recognizing options
1271 * which have been added to @context. A side-effect of
1272 * calling this function is that g_set_prgname() will be
1275 * If the parsing is successful, any parsed arguments are
1276 * removed from the array and @argc and @argv are updated
1277 * accordingly. A '--' option is stripped from @argv
1278 * unless there are unparsed options before and after it,
1279 * or some of the options after it start with '-'. In case
1280 * of an error, @argc and @argv are left unmodified.
1282 * If automatic <option>--help</option> support is enabled
1283 * (see g_option_context_set_help_enabled()), and the
1284 * @argv array contains one of the recognized help options,
1285 * this function will produce help output to stdout and
1286 * call <literal>exit (0)</literal>.
1288 * Return value: %TRUE if the parsing was successful,
1289 * %FALSE if an error occurred
1294 g_option_context_parse (GOptionContext *context,
1302 /* Set program name */
1303 if (!g_get_prgname())
1305 if (argc && argv && *argc)
1309 prgname = g_path_get_basename ((*argv)[0]);
1310 g_set_prgname (prgname);
1314 g_set_prgname ("<unknown>");
1317 /* Call pre-parse hooks */
1318 list = context->groups;
1321 GOptionGroup *group = list->data;
1323 if (group->pre_parse_func)
1325 if (!(* group->pre_parse_func) (context, group,
1326 group->user_data, error))
1333 if (context->main_group && context->main_group->pre_parse_func)
1335 if (!(* context->main_group->pre_parse_func) (context, context->main_group,
1336 context->main_group->user_data, error))
1342 gboolean stop_parsing = FALSE;
1343 gboolean has_unknown = FALSE;
1344 gint separator_pos = 0;
1346 for (i = 1; i < *argc; i++)
1349 gboolean parsed = FALSE;
1351 if ((*argv)[i][0] == '-' && (*argv)[i][1] != '\0' && !stop_parsing)
1353 if ((*argv)[i][1] == '-')
1357 arg = (*argv)[i] + 2;
1359 /* '--' terminates list of arguments */
1363 stop_parsing = TRUE;
1367 /* Handle help options */
1368 if (context->help_enabled)
1370 if (strcmp (arg, "help") == 0)
1371 print_help (context, TRUE, NULL);
1372 else if (strcmp (arg, "help-all") == 0)
1373 print_help (context, FALSE, NULL);
1374 else if (strncmp (arg, "help-", 5) == 0)
1378 list = context->groups;
1382 GOptionGroup *group = list->data;
1384 if (strcmp (arg + 5, group->name) == 0)
1385 print_help (context, FALSE, group);
1392 if (context->main_group &&
1393 !parse_long_option (context, context->main_group, &i, arg,
1394 FALSE, argc, argv, error, &parsed))
1400 /* Try the groups */
1401 list = context->groups;
1404 GOptionGroup *group = list->data;
1406 if (!parse_long_option (context, group, &i, arg,
1407 FALSE, argc, argv, error, &parsed))
1419 /* Now look for --<group>-<option> */
1420 dash = strchr (arg, '-');
1423 /* Try the groups */
1424 list = context->groups;
1427 GOptionGroup *group = list->data;
1429 if (strncmp (group->name, arg, dash - arg) == 0)
1431 if (!parse_long_option (context, group, &i, dash + 1,
1432 TRUE, argc, argv, error, &parsed))
1443 if (context->ignore_unknown)
1451 gboolean *nulled_out = NULL;
1453 arg = (*argv)[i] + 1;
1457 if (context->ignore_unknown)
1458 nulled_out = g_new0 (gboolean, strlen (arg));
1460 for (j = 0; j < strlen (arg); j++)
1462 if (context->help_enabled && arg[j] == '?')
1463 print_help (context, TRUE, NULL);
1467 if (context->main_group &&
1468 !parse_short_option (context, context->main_group,
1470 argc, argv, error, &parsed))
1473 g_free (nulled_out);
1479 /* Try the groups */
1480 list = context->groups;
1483 GOptionGroup *group = list->data;
1485 if (!parse_short_option (context, group, i, &new_i, arg[j],
1486 argc, argv, error, &parsed))
1496 if (context->ignore_unknown)
1499 nulled_out[j] = TRUE;
1508 if (context->ignore_unknown)
1510 gchar *new_arg = NULL;
1513 for (j = 0; j < strlen (arg); j++)
1518 new_arg = g_malloc (strlen (arg) + 1);
1519 new_arg[arg_index++] = arg[j];
1523 new_arg[arg_index] = '\0';
1525 add_pending_null (context, &((*argv)[i]), new_arg);
1529 add_pending_null (context, &((*argv)[i]), NULL);
1537 if (!parsed && !context->ignore_unknown)
1540 G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
1541 _("Unknown option %s"), (*argv)[i]);
1547 /* Collect remaining args */
1548 if (context->main_group &&
1549 !parse_remaining_arg (context, context->main_group, &i,
1550 argc, argv, error, &parsed))
1553 if (!parsed && (has_unknown || (*argv)[i][0] == '-'))
1558 if (separator_pos > 0)
1559 add_pending_null (context, &((*argv)[separator_pos]), NULL);
1563 /* Call post-parse hooks */
1564 list = context->groups;
1567 GOptionGroup *group = list->data;
1569 if (group->post_parse_func)
1571 if (!(* group->post_parse_func) (context, group,
1572 group->user_data, error))
1579 if (context->main_group && context->main_group->post_parse_func)
1581 if (!(* context->main_group->post_parse_func) (context, context->main_group,
1582 context->main_group->user_data, error))
1588 free_pending_nulls (context, TRUE);
1590 for (i = 1; i < *argc; i++)
1592 for (k = i; k < *argc; k++)
1593 if ((*argv)[k] != NULL)
1599 for (j = i + k; j < *argc; j++)
1601 (*argv)[j-k] = (*argv)[j];
1613 /* Call error hooks */
1614 list = context->groups;
1617 GOptionGroup *group = list->data;
1619 if (group->error_func)
1620 (* group->error_func) (context, group,
1621 group->user_data, error);
1626 if (context->main_group && context->main_group->error_func)
1627 (* context->main_group->error_func) (context, context->main_group,
1628 context->main_group->user_data, error);
1630 free_changes_list (context, TRUE);
1631 free_pending_nulls (context, FALSE);
1637 * g_option_group_new:
1638 * @name: the name for the option group, this is used to provide
1639 * help for the options in this group with <option>--help-</option>@name
1640 * @description: a description for this group to be shown in
1641 * <option>--help</option>. This string is translated using the translation
1642 * domain or translation function of the group
1643 * @help_description: a description for the <option>--help-</option>@name option.
1644 * This string is translated using the translation domain or translation function
1646 * @user_data: user data that will be passed to the pre- and post-parse hooks,
1647 * the error hook and to callbacks of %G_OPTION_ARG_CALLBACK options, or %NULL
1648 * @destroy: a function that will be called to free @user_data, or %NULL
1650 * Creates a new #GOptionGroup.
1652 * Return value: a newly created option group. It should be added
1653 * to a #GOptionContext or freed with g_option_group_free().
1658 g_option_group_new (const gchar *name,
1659 const gchar *description,
1660 const gchar *help_description,
1662 GDestroyNotify destroy)
1665 GOptionGroup *group;
1667 group = g_new0 (GOptionGroup, 1);
1668 group->name = g_strdup (name);
1669 group->description = g_strdup (description);
1670 group->help_description = g_strdup (help_description);
1671 group->user_data = user_data;
1672 group->destroy_notify = destroy;
1679 * g_option_group_free:
1680 * @group: a #GOptionGroup
1682 * Frees a #GOptionGroup. Note that you must <emphasis>not</emphasis>
1683 * free groups which have been added to a #GOptionContext.
1688 g_option_group_free (GOptionGroup *group)
1690 g_return_if_fail (group != NULL);
1692 g_free (group->name);
1693 g_free (group->description);
1694 g_free (group->help_description);
1696 g_free (group->entries);
1698 if (group->destroy_notify)
1699 (* group->destroy_notify) (group->user_data);
1701 if (group->translate_notify)
1702 (* group->translate_notify) (group->translate_data);
1709 * g_option_group_add_entries:
1710 * @group: a #GOptionGroup
1711 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
1713 * Adds the options specified in @entries to @group.
1718 g_option_group_add_entries (GOptionGroup *group,
1719 const GOptionEntry *entries)
1723 g_return_if_fail (entries != NULL);
1725 for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++) ;
1727 group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
1729 memcpy (group->entries + group->n_entries, entries, sizeof (GOptionEntry) * n_entries);
1731 for (i = group->n_entries; i < group->n_entries + n_entries; i++)
1733 gchar c = group->entries[i].short_name;
1737 if (c == '-' || !g_ascii_isprint (c))
1739 g_warning (G_STRLOC": ignoring invalid short option '%c' (%d)", c, c);
1740 group->entries[i].short_name = 0;
1745 group->n_entries += n_entries;
1749 * g_option_group_set_parse_hooks:
1750 * @group: a #GOptionGroup
1751 * @pre_parse_func: a function to call before parsing, or %NULL
1752 * @post_parse_func: a function to call after parsing, or %NULL
1754 * Associates two functions with @group which will be called
1755 * from g_option_context_parse() before the first option is parsed
1756 * and after the last option has been parsed, respectively.
1758 * Note that the user data to be passed to @pre_parse_func and
1759 * @post_parse_func can be specified when constructing the group
1760 * with g_option_group_new().
1765 g_option_group_set_parse_hooks (GOptionGroup *group,
1766 GOptionParseFunc pre_parse_func,
1767 GOptionParseFunc post_parse_func)
1769 g_return_if_fail (group != NULL);
1771 group->pre_parse_func = pre_parse_func;
1772 group->post_parse_func = post_parse_func;
1776 * g_option_group_set_error_hook:
1777 * @group: a #GOptionGroup
1778 * @error_func: a function to call when an error occurs
1780 * Associates a function with @group which will be called
1781 * from g_option_context_parse() when an error occurs.
1783 * Note that the user data to be passed to @pre_parse_func and
1784 * @post_parse_func can be specified when constructing the group
1785 * with g_option_group_new().
1790 g_option_group_set_error_hook (GOptionGroup *group,
1791 GOptionErrorFunc error_func)
1793 g_return_if_fail (group != NULL);
1795 group->error_func = error_func;
1800 * g_option_group_set_translate_func:
1801 * @group: a #GOptionGroup
1802 * @func: the #GTranslateFunc, or %NULL
1803 * @data: user data to pass to @func, or %NULL
1804 * @destroy_notify: a function which gets called to free @data, or %NULL
1806 * Sets the function which is used to translate user-visible
1807 * strings, for <option>--help</option> output. Different
1808 * groups can use different #GTranslateFunc<!-- -->s. If @func
1809 * is %NULL, strings are not translated.
1811 * If you are using gettext(), you only need to set the translation
1812 * domain, see g_option_group_set_translation_domain().
1817 g_option_group_set_translate_func (GOptionGroup *group,
1818 GTranslateFunc func,
1820 GDestroyNotify destroy_notify)
1822 g_return_if_fail (group != NULL);
1824 if (group->translate_notify)
1825 group->translate_notify (group->translate_data);
1827 group->translate_func = func;
1828 group->translate_data = data;
1829 group->translate_notify = destroy_notify;
1833 dgettext_swapped (const gchar *msgid,
1834 const gchar *domainname)
1836 return dgettext (domainname, msgid);
1840 * g_option_group_set_translation_domain:
1841 * @group: a #GOptionGroup
1842 * @domain: the domain to use
1844 * A convenience function to use gettext() for translating
1845 * user-visible strings.
1850 g_option_group_set_translation_domain (GOptionGroup *group,
1851 const gchar *domain)
1853 g_return_if_fail (group != NULL);
1855 g_option_group_set_translate_func (group,
1856 (GTranslateFunc)dgettext_swapped,
1861 #define __G_OPTION_C__
1862 #include "galiasdef.c"