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)
73 struct _GOptionContext
77 gchar *parameter_string;
81 GTranslateFunc translate_func;
82 GDestroyNotify translate_notify;
83 gpointer translate_data;
85 guint help_enabled : 1;
86 guint ignore_unknown : 1;
88 GOptionGroup *main_group;
90 /* We keep a list of change so we can revert them */
93 /* We also keep track of all argv elements
94 * that should be NULLed or modified.
103 gchar *help_description;
105 GDestroyNotify destroy_notify;
108 GTranslateFunc translate_func;
109 GDestroyNotify translate_notify;
110 gpointer translate_data;
112 GOptionEntry *entries;
115 GOptionParseFunc pre_parse_func;
116 GOptionParseFunc post_parse_func;
117 GOptionErrorFunc error_func;
120 static void free_changes_list (GOptionContext *context,
122 static void free_pending_nulls (GOptionContext *context,
123 gboolean perform_nulls);
126 g_option_error_quark (void)
128 return g_quark_from_static_string ("g-option-context-error-quark");
132 * g_option_context_new:
133 * @parameter_string: a string which is displayed in
134 * the first line of <option>--help</option> output, after the
136 * <literal><replaceable>programname</replaceable> [OPTION...]</literal>
138 * Creates a new option context.
140 * The @parameter_string can serve multiple purposes. It can be used
141 * to add descriptions for "rest" arguments, which are not parsed by
142 * the #GOptionContext, typically something like "FILES" or
143 * "FILE1 FILE2...". If you are using #G_OPTION_REMAINING for
144 * collecting "rest" arguments, GLib handles this automatically by
145 * using the @arg_description of the corresponding #GOptionEntry in
148 * Another usage is to give a short summary of the program
149 * functionality, like " - frob the strings", which will be displayed
150 * in the same line as the usage. For a longer description of the
151 * program functionality that should be displayed as a paragraph
152 * below the usage line, use g_option_context_set_summary().
154 * Note that the @parameter_string is translated (see
155 * g_option_context_set_translate_func()).
157 * Returns: a newly created #GOptionContext, which must be
158 * freed with g_option_context_free() after use.
163 g_option_context_new (const gchar *parameter_string)
166 GOptionContext *context;
168 context = g_new0 (GOptionContext, 1);
170 context->parameter_string = g_strdup (parameter_string);
171 context->help_enabled = TRUE;
172 context->ignore_unknown = FALSE;
178 * g_option_context_free:
179 * @context: a #GOptionContext
181 * Frees context and all the groups which have been
186 void g_option_context_free (GOptionContext *context)
188 g_return_if_fail (context != NULL);
190 g_list_foreach (context->groups, (GFunc)g_option_group_free, NULL);
191 g_list_free (context->groups);
193 if (context->main_group)
194 g_option_group_free (context->main_group);
196 free_changes_list (context, FALSE);
197 free_pending_nulls (context, FALSE);
199 g_free (context->parameter_string);
200 g_free (context->summary);
201 g_free (context->description);
203 if (context->translate_notify)
204 (* context->translate_notify) (context->translate_data);
211 * g_option_context_set_help_enabled:
212 * @context: a #GOptionContext
213 * @help_enabled: %TRUE to enable <option>--help</option>, %FALSE to disable it
215 * Enables or disables automatic generation of <option>--help</option>
216 * output. By default, g_option_context_parse() recognizes
217 * <option>--help</option>, <option>-?</option>, <option>--help-all</option>
218 * and <option>--help-</option><replaceable>groupname</replaceable> and creates
219 * suitable output to stdout.
223 void g_option_context_set_help_enabled (GOptionContext *context,
224 gboolean help_enabled)
227 g_return_if_fail (context != NULL);
229 context->help_enabled = help_enabled;
233 * g_option_context_get_help_enabled:
234 * @context: a #GOptionContext
236 * Returns whether automatic <option>--help</option> generation
237 * is turned on for @context. See g_option_context_set_help_enabled().
239 * Returns: %TRUE if automatic help generation is turned on.
244 g_option_context_get_help_enabled (GOptionContext *context)
246 g_return_val_if_fail (context != NULL, FALSE);
248 return context->help_enabled;
252 * g_option_context_set_ignore_unknown_options:
253 * @context: a #GOptionContext
254 * @ignore_unknown: %TRUE to ignore unknown options, %FALSE to produce
255 * an error when unknown options are met
257 * Sets whether to ignore unknown options or not. If an argument is
258 * ignored, it is left in the @argv array after parsing. By default,
259 * g_option_context_parse() treats unknown options as error.
261 * This setting does not affect non-option arguments (i.e. arguments
262 * which don't start with a dash). But note that GOption cannot reliably
263 * determine whether a non-option belongs to a preceding unknown option.
268 g_option_context_set_ignore_unknown_options (GOptionContext *context,
269 gboolean ignore_unknown)
271 g_return_if_fail (context != NULL);
273 context->ignore_unknown = ignore_unknown;
277 * g_option_context_get_ignore_unknown_options:
278 * @context: a #GOptionContext
280 * Returns whether unknown options are ignored or not. See
281 * g_option_context_set_ignore_unknown_options().
283 * Returns: %TRUE if unknown options are ignored.
288 g_option_context_get_ignore_unknown_options (GOptionContext *context)
290 g_return_val_if_fail (context != NULL, FALSE);
292 return context->ignore_unknown;
296 * g_option_context_add_group:
297 * @context: a #GOptionContext
298 * @group: the group to add
300 * Adds a #GOptionGroup to the @context, so that parsing with @context
301 * will recognize the options in the group. Note that the group will
302 * be freed together with the context when g_option_context_free() is
303 * called, so you must not free the group yourself after adding it
309 g_option_context_add_group (GOptionContext *context,
314 g_return_if_fail (context != NULL);
315 g_return_if_fail (group != NULL);
316 g_return_if_fail (group->name != NULL);
317 g_return_if_fail (group->description != NULL);
318 g_return_if_fail (group->help_description != NULL);
320 for (list = context->groups; list; list = list->next)
322 GOptionGroup *g = (GOptionGroup *)list->data;
324 if ((group->name == NULL && g->name == NULL) ||
325 (group->name && g->name && strcmp (group->name, g->name) == 0))
326 g_warning ("A group named \"%s\" is already part of this GOptionContext",
330 context->groups = g_list_append (context->groups, group);
334 * g_option_context_set_main_group:
335 * @context: a #GOptionContext
336 * @group: the group to set as main group
338 * Sets a #GOptionGroup as main group of the @context.
339 * This has the same effect as calling g_option_context_add_group(),
340 * the only difference is that the options in the main group are
341 * treated differently when generating <option>--help</option> output.
346 g_option_context_set_main_group (GOptionContext *context,
349 g_return_if_fail (context != NULL);
350 g_return_if_fail (group != NULL);
352 if (context->main_group)
354 g_warning ("This GOptionContext already has a main group");
359 context->main_group = group;
363 * g_option_context_get_main_group:
364 * @context: a #GOptionContext
366 * Returns a pointer to the main group of @context.
368 * Return value: the main group of @context, or %NULL if @context doesn't
369 * have a main group. Note that group belongs to @context and should
370 * not be modified or freed.
375 g_option_context_get_main_group (GOptionContext *context)
377 g_return_val_if_fail (context != NULL, NULL);
379 return context->main_group;
383 * g_option_context_add_main_entries:
384 * @context: a #GOptionContext
385 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
386 * @translation_domain: a translation domain to use for translating
387 * the <option>--help</option> output for the options in @entries
388 * with gettext(), or %NULL
390 * A convenience function which creates a main group if it doesn't
391 * exist, adds the @entries to it and sets the translation domain.
396 g_option_context_add_main_entries (GOptionContext *context,
397 const GOptionEntry *entries,
398 const gchar *translation_domain)
400 g_return_if_fail (entries != NULL);
402 if (!context->main_group)
403 context->main_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
405 g_option_group_add_entries (context->main_group, entries);
406 g_option_group_set_translation_domain (context->main_group, translation_domain);
410 calculate_max_length (GOptionGroup *group)
413 gint i, len, max_length;
417 for (i = 0; i < group->n_entries; i++)
419 entry = &group->entries[i];
421 if (entry->flags & G_OPTION_FLAG_HIDDEN)
424 len = g_utf8_strlen (entry->long_name, -1);
426 if (entry->short_name)
429 if (!NO_ARG (entry) && entry->arg_description)
430 len += 1 + g_utf8_strlen (TRANSLATE (group, entry->arg_description), -1);
432 max_length = MAX (max_length, len);
439 print_entry (GOptionGroup *group,
441 const GOptionEntry *entry)
445 if (entry->flags & G_OPTION_FLAG_HIDDEN)
448 if (entry->long_name[0] == 0)
451 str = g_string_new (NULL);
453 if (entry->short_name)
454 g_string_append_printf (str, " -%c, --%s", entry->short_name, entry->long_name);
456 g_string_append_printf (str, " --%s", entry->long_name);
458 if (entry->arg_description)
459 g_string_append_printf (str, "=%s", TRANSLATE (group, entry->arg_description));
461 g_print ("%-*s %s\n", max_length + 4, str->str,
462 entry->description ? TRANSLATE (group, entry->description) : "");
463 g_string_free (str, TRUE);
467 print_help (GOptionContext *context,
472 gint max_length, len;
475 GHashTable *shadow_map;
477 const gchar *rest_description;
479 rest_description = NULL;
480 if (context->main_group)
483 for (i = 0; i < context->main_group->n_entries; i++)
485 entry = &context->main_group->entries[i];
486 if (entry->long_name[0] == 0)
488 rest_description = TRANSLATE (context->main_group, entry->arg_description);
494 g_print ("%s\n %s %s%s%s%s%s\n\n",
495 _("Usage:"), g_get_prgname(), _("[OPTION...]"),
496 rest_description ? " " : "",
497 rest_description ? rest_description : "",
498 context->parameter_string ? " " : "",
499 context->parameter_string ? TRANSLATE (context, context->parameter_string) : "");
501 if (context->summary)
502 g_print ("%s\n\n", TRANSLATE (context, context->summary));
504 memset (seen, 0, sizeof (gboolean) * 256);
505 shadow_map = g_hash_table_new (g_str_hash, g_str_equal);
507 if (context->main_group)
509 for (i = 0; i < context->main_group->n_entries; i++)
511 entry = &context->main_group->entries[i];
512 g_hash_table_insert (shadow_map,
513 (gpointer)entry->long_name,
516 if (seen[(guchar)entry->short_name])
517 entry->short_name = 0;
519 seen[(guchar)entry->short_name] = TRUE;
523 list = context->groups;
526 GOptionGroup *group = list->data;
527 for (i = 0; i < group->n_entries; i++)
529 entry = &group->entries[i];
530 if (g_hash_table_lookup (shadow_map, entry->long_name) &&
531 !(entry->flags && G_OPTION_FLAG_NOALIAS))
532 entry->long_name = g_strdup_printf ("%s-%s", group->name, entry->long_name);
534 g_hash_table_insert (shadow_map, (gpointer)entry->long_name, entry);
536 if (seen[(guchar)entry->short_name] &&
537 !(entry->flags && G_OPTION_FLAG_NOALIAS))
538 entry->short_name = 0;
540 seen[(guchar)entry->short_name] = TRUE;
545 g_hash_table_destroy (shadow_map);
547 list = context->groups;
549 max_length = g_utf8_strlen ("-?, --help", -1);
553 len = g_utf8_strlen ("--help-all", -1);
554 max_length = MAX (max_length, len);
557 if (context->main_group)
559 len = calculate_max_length (context->main_group);
560 max_length = MAX (max_length, len);
565 GOptionGroup *group = list->data;
567 /* First, we check the --help-<groupname> options */
568 len = g_utf8_strlen ("--help-", -1) + g_utf8_strlen (group->name, -1);
569 max_length = MAX (max_length, len);
571 /* Then we go through the entries */
572 len = calculate_max_length (group);
573 max_length = MAX (max_length, len);
578 /* Add a bit of padding */
583 list = context->groups;
585 g_print ("%s\n -%c, --%-*s %s\n",
586 _("Help Options:"), '?', max_length - 4, "help",
587 _("Show help options"));
589 /* We only want --help-all when there are groups */
591 g_print (" --%-*s %s\n", max_length, "help-all",
592 _("Show all help options"));
596 GOptionGroup *group = list->data;
598 g_print (" --help-%-*s %s\n", max_length - 5, group->name,
599 TRANSLATE (group, group->help_description));
609 /* Print a certain group */
611 g_print ("%s\n", TRANSLATE (group, group->description));
612 for (i = 0; i < group->n_entries; i++)
613 print_entry (group, max_length, &group->entries[i]);
618 /* Print all groups */
620 list = context->groups;
624 GOptionGroup *group = list->data;
626 g_print ("%s\n", group->description);
628 for (i = 0; i < group->n_entries; i++)
629 if (!(group->entries[i].flags & G_OPTION_FLAG_IN_MAIN))
630 print_entry (group, max_length, &group->entries[i]);
637 /* Print application options if --help or --help-all has been specified */
638 if (main_help || !group)
640 list = context->groups;
642 g_print ("%s\n", _("Application Options:"));
644 if (context->main_group)
645 for (i = 0; i < context->main_group->n_entries; i++)
646 print_entry (context->main_group, max_length,
647 &context->main_group->entries[i]);
651 GOptionGroup *group = list->data;
653 /* Print main entries from other groups */
654 for (i = 0; i < group->n_entries; i++)
655 if (group->entries[i].flags & G_OPTION_FLAG_IN_MAIN)
656 print_entry (group, max_length, &group->entries[i]);
664 if (context->description)
665 g_print ("%s\n", TRANSLATE (context, context->description));
671 parse_int (const gchar *arg_name,
680 tmp = strtol (arg, &end, 0);
682 if (*arg == '\0' || *end != '\0')
685 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
686 _("Cannot parse integer value '%s' for %s"),
692 if (*result != tmp || errno == ERANGE)
695 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
696 _("Integer value '%s' for %s out of range"),
706 parse_double (const gchar *arg_name,
715 tmp = g_strtod (arg, &end);
717 if (*arg == '\0' || *end != '\0')
720 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
721 _("Cannot parse double value '%s' for %s"),
728 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
729 _("Double value '%s' for %s out of range"),
741 parse_int64 (const gchar *arg_name,
750 tmp = g_ascii_strtoll (arg, &end, 0);
752 if (*arg == '\0' || *end != '\0')
755 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
756 _("Cannot parse integer value '%s' for %s"),
763 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
764 _("Integer value '%s' for %s out of range"),
776 get_change (GOptionContext *context,
781 Change *change = NULL;
783 for (list = context->changes; list != NULL; list = list->next)
787 if (change->arg_data == arg_data)
791 change = g_new0 (Change, 1);
792 change->arg_type = arg_type;
793 change->arg_data = arg_data;
795 context->changes = g_list_prepend (context->changes, change);
803 add_pending_null (GOptionContext *context,
809 n = g_new0 (PendingNull, 1);
813 context->pending_nulls = g_list_prepend (context->pending_nulls, n);
817 parse_arg (GOptionContext *context,
821 const gchar *option_name,
827 g_assert (value || OPTIONAL_ARG (entry) || NO_ARG (entry));
831 case G_OPTION_ARG_NONE:
833 change = get_change (context, G_OPTION_ARG_NONE,
836 *(gboolean *)entry->arg_data = !(entry->flags & G_OPTION_FLAG_REVERSE);
839 case G_OPTION_ARG_STRING:
843 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
848 change = get_change (context, G_OPTION_ARG_STRING,
850 g_free (change->allocated.str);
852 change->prev.str = *(gchar **)entry->arg_data;
853 change->allocated.str = data;
855 *(gchar **)entry->arg_data = data;
858 case G_OPTION_ARG_STRING_ARRAY:
862 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
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_FILENAME:
895 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
900 data = g_strdup (value);
902 change = get_change (context, G_OPTION_ARG_FILENAME,
904 g_free (change->allocated.str);
906 change->prev.str = *(gchar **)entry->arg_data;
907 change->allocated.str = data;
909 *(gchar **)entry->arg_data = data;
913 case G_OPTION_ARG_FILENAME_ARRAY:
918 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
923 data = g_strdup (value);
925 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
928 if (change->allocated.array.len == 0)
930 change->prev.array = *(gchar ***)entry->arg_data;
931 change->allocated.array.data = g_new (gchar *, 2);
934 change->allocated.array.data =
935 g_renew (gchar *, change->allocated.array.data,
936 change->allocated.array.len + 2);
938 change->allocated.array.data[change->allocated.array.len] = data;
939 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
941 change->allocated.array.len ++;
943 *(gchar ***)entry->arg_data = change->allocated.array.data;
948 case G_OPTION_ARG_INT:
952 if (!parse_int (option_name, value,
957 change = get_change (context, G_OPTION_ARG_INT,
959 change->prev.integer = *(gint *)entry->arg_data;
960 *(gint *)entry->arg_data = data;
963 case G_OPTION_ARG_CALLBACK:
968 if (!value && entry->flags & G_OPTION_FLAG_OPTIONAL_ARG)
970 else if (entry->flags & G_OPTION_FLAG_NO_ARG)
972 else if (entry->flags & G_OPTION_FLAG_FILENAME)
975 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
977 data = g_strdup (value);
981 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
983 if (!(entry->flags & (G_OPTION_FLAG_NO_ARG|G_OPTION_FLAG_OPTIONAL_ARG)) &&
987 retval = (* (GOptionArgFunc) entry->arg_data) (option_name, data, group->user_data, error);
995 case G_OPTION_ARG_DOUBLE:
999 if (!parse_double (option_name, value,
1006 change = get_change (context, G_OPTION_ARG_DOUBLE,
1008 change->prev.dbl = *(gdouble *)entry->arg_data;
1009 *(gdouble *)entry->arg_data = data;
1012 case G_OPTION_ARG_INT64:
1016 if (!parse_int64 (option_name, value,
1023 change = get_change (context, G_OPTION_ARG_INT64,
1025 change->prev.int64 = *(gint64 *)entry->arg_data;
1026 *(gint64 *)entry->arg_data = data;
1030 g_assert_not_reached ();
1037 parse_short_option (GOptionContext *context,
1038 GOptionGroup *group,
1049 for (j = 0; j < group->n_entries; j++)
1051 if (arg == group->entries[j].short_name)
1054 gchar *value = NULL;
1056 option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
1058 if (NO_ARG (&group->entries[j]))
1062 if (*new_index > index)
1065 G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
1066 _("Error parsing option %s"), option_name);
1067 g_free (option_name);
1071 if (index < *argc - 1)
1073 if (!OPTIONAL_ARG (&group->entries[j]))
1075 value = (*argv)[index + 1];
1076 add_pending_null (context, &((*argv)[index + 1]), NULL);
1077 *new_index = index+1;
1081 if ((*argv)[index + 1][0] == '-')
1085 value = (*argv)[index + 1];
1086 add_pending_null (context, &((*argv)[index + 1]), NULL);
1087 *new_index = index + 1;
1091 else if (index >= *argc - 1 && OPTIONAL_ARG (&group->entries[j]))
1096 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1097 _("Missing argument for %s"), option_name);
1098 g_free (option_name);
1103 if (!parse_arg (context, group, &group->entries[j],
1104 value, option_name, error))
1106 g_free (option_name);
1110 g_free (option_name);
1119 parse_long_option (GOptionContext *context,
1120 GOptionGroup *group,
1131 for (j = 0; j < group->n_entries; j++)
1133 if (*index >= *argc)
1136 if (aliased && (group->entries[j].flags & G_OPTION_FLAG_NOALIAS))
1139 if (NO_ARG (&group->entries[j]) &&
1140 strcmp (arg, group->entries[j].long_name) == 0)
1144 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1145 parse_arg (context, group, &group->entries[j],
1146 NULL, option_name, error);
1147 g_free(option_name);
1149 add_pending_null (context, &((*argv)[*index]), NULL);
1154 gint len = strlen (group->entries[j].long_name);
1156 if (strncmp (arg, group->entries[j].long_name, len) == 0 &&
1157 (arg[len] == '=' || arg[len] == 0))
1159 gchar *value = NULL;
1162 add_pending_null (context, &((*argv)[*index]), NULL);
1163 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1165 if (arg[len] == '=')
1166 value = arg + len + 1;
1167 else if (*index < *argc - 1)
1169 if (!(group->entries[j].flags & G_OPTION_FLAG_OPTIONAL_ARG))
1171 value = (*argv)[*index + 1];
1172 add_pending_null (context, &((*argv)[*index + 1]), NULL);
1177 if ((*argv)[*index + 1][0] == '-')
1180 retval = parse_arg (context, group, &group->entries[j],
1181 NULL, option_name, error);
1183 g_free (option_name);
1188 value = (*argv)[*index + 1];
1189 add_pending_null (context, &((*argv)[*index + 1]), NULL);
1194 else if (*index >= *argc - 1 &&
1195 group->entries[j].flags & G_OPTION_FLAG_OPTIONAL_ARG)
1198 retval = parse_arg (context, group, &group->entries[j],
1199 NULL, option_name, error);
1201 g_free (option_name);
1207 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1208 _("Missing argument for %s"), option_name);
1209 g_free (option_name);
1213 if (!parse_arg (context, group, &group->entries[j],
1214 value, option_name, error))
1216 g_free (option_name);
1220 g_free (option_name);
1230 parse_remaining_arg (GOptionContext *context,
1231 GOptionGroup *group,
1240 for (j = 0; j < group->n_entries; j++)
1242 if (*index >= *argc)
1245 if (group->entries[j].long_name[0])
1248 g_return_val_if_fail (group->entries[j].arg == G_OPTION_ARG_STRING_ARRAY ||
1249 group->entries[j].arg == G_OPTION_ARG_FILENAME_ARRAY, FALSE);
1251 add_pending_null (context, &((*argv)[*index]), NULL);
1253 if (!parse_arg (context, group, &group->entries[j], (*argv)[*index], "", error))
1264 free_changes_list (GOptionContext *context,
1269 for (list = context->changes; list != NULL; list = list->next)
1271 Change *change = list->data;
1275 switch (change->arg_type)
1277 case G_OPTION_ARG_NONE:
1278 *(gboolean *)change->arg_data = change->prev.bool;
1280 case G_OPTION_ARG_INT:
1281 *(gint *)change->arg_data = change->prev.integer;
1283 case G_OPTION_ARG_STRING:
1284 case G_OPTION_ARG_FILENAME:
1285 g_free (change->allocated.str);
1286 *(gchar **)change->arg_data = change->prev.str;
1288 case G_OPTION_ARG_STRING_ARRAY:
1289 case G_OPTION_ARG_FILENAME_ARRAY:
1290 g_strfreev (change->allocated.array.data);
1291 *(gchar ***)change->arg_data = change->prev.array;
1293 case G_OPTION_ARG_DOUBLE:
1294 *(gdouble *)change->arg_data = change->prev.dbl;
1296 case G_OPTION_ARG_INT64:
1297 *(gint64 *)change->arg_data = change->prev.int64;
1300 g_assert_not_reached ();
1307 g_list_free (context->changes);
1308 context->changes = NULL;
1312 free_pending_nulls (GOptionContext *context,
1313 gboolean perform_nulls)
1317 for (list = context->pending_nulls; list != NULL; list = list->next)
1319 PendingNull *n = list->data;
1325 /* Copy back the short options */
1327 strcpy (*n->ptr + 1, n->value);
1337 g_list_free (context->pending_nulls);
1338 context->pending_nulls = NULL;
1342 * g_option_context_parse:
1343 * @context: a #GOptionContext
1344 * @argc: a pointer to the number of command line arguments
1345 * @argv: a pointer to the array of command line arguments
1346 * @error: a return location for errors
1348 * Parses the command line arguments, recognizing options
1349 * which have been added to @context. A side-effect of
1350 * calling this function is that g_set_prgname() will be
1353 * If the parsing is successful, any parsed arguments are
1354 * removed from the array and @argc and @argv are updated
1355 * accordingly. A '--' option is stripped from @argv
1356 * unless there are unparsed options before and after it,
1357 * or some of the options after it start with '-'. In case
1358 * of an error, @argc and @argv are left unmodified.
1360 * If automatic <option>--help</option> support is enabled
1361 * (see g_option_context_set_help_enabled()), and the
1362 * @argv array contains one of the recognized help options,
1363 * this function will produce help output to stdout and
1364 * call <literal>exit (0)</literal>.
1366 * Note that function depends on the
1367 * <link linkend="setlocale">current locale</link> for
1368 * automatic character set conversion of string and filename
1371 * Return value: %TRUE if the parsing was successful,
1372 * %FALSE if an error occurred
1377 g_option_context_parse (GOptionContext *context,
1385 /* Set program name */
1386 if (!g_get_prgname())
1388 if (argc && argv && *argc)
1392 prgname = g_path_get_basename ((*argv)[0]);
1393 g_set_prgname (prgname);
1397 g_set_prgname ("<unknown>");
1400 /* Call pre-parse hooks */
1401 list = context->groups;
1404 GOptionGroup *group = list->data;
1406 if (group->pre_parse_func)
1408 if (!(* group->pre_parse_func) (context, group,
1409 group->user_data, error))
1416 if (context->main_group && context->main_group->pre_parse_func)
1418 if (!(* context->main_group->pre_parse_func) (context, context->main_group,
1419 context->main_group->user_data, error))
1425 gboolean stop_parsing = FALSE;
1426 gboolean has_unknown = FALSE;
1427 gint separator_pos = 0;
1429 for (i = 1; i < *argc; i++)
1432 gboolean parsed = FALSE;
1434 if ((*argv)[i][0] == '-' && (*argv)[i][1] != '\0' && !stop_parsing)
1436 if ((*argv)[i][1] == '-')
1440 arg = (*argv)[i] + 2;
1442 /* '--' terminates list of arguments */
1446 stop_parsing = TRUE;
1450 /* Handle help options */
1451 if (context->help_enabled)
1453 if (strcmp (arg, "help") == 0)
1454 print_help (context, TRUE, NULL);
1455 else if (strcmp (arg, "help-all") == 0)
1456 print_help (context, FALSE, NULL);
1457 else if (strncmp (arg, "help-", 5) == 0)
1461 list = context->groups;
1465 GOptionGroup *group = list->data;
1467 if (strcmp (arg + 5, group->name) == 0)
1468 print_help (context, FALSE, group);
1475 if (context->main_group &&
1476 !parse_long_option (context, context->main_group, &i, arg,
1477 FALSE, argc, argv, error, &parsed))
1483 /* Try the groups */
1484 list = context->groups;
1487 GOptionGroup *group = list->data;
1489 if (!parse_long_option (context, group, &i, arg,
1490 FALSE, argc, argv, error, &parsed))
1502 /* Now look for --<group>-<option> */
1503 dash = strchr (arg, '-');
1506 /* Try the groups */
1507 list = context->groups;
1510 GOptionGroup *group = list->data;
1512 if (strncmp (group->name, arg, dash - arg) == 0)
1514 if (!parse_long_option (context, group, &i, dash + 1,
1515 TRUE, argc, argv, error, &parsed))
1526 if (context->ignore_unknown)
1530 { /* short option */
1531 gint j, new_i = i, arg_length;
1532 gboolean *nulled_out = NULL;
1533 arg = (*argv)[i] + 1;
1534 arg_length = strlen (arg);
1535 nulled_out = g_newa (gboolean, arg_length);
1536 memset (nulled_out, 0, arg_length * sizeof (gboolean));
1537 for (j = 0; j < arg_length; j++)
1539 if (context->help_enabled && arg[j] == '?')
1540 print_help (context, TRUE, NULL);
1542 if (context->main_group &&
1543 !parse_short_option (context, context->main_group,
1545 argc, argv, error, &parsed))
1549 /* Try the groups */
1550 list = context->groups;
1553 GOptionGroup *group = list->data;
1554 if (!parse_short_option (context, group, i, &new_i, arg[j],
1555 argc, argv, error, &parsed))
1563 if (context->ignore_unknown && parsed)
1564 nulled_out[j] = TRUE;
1565 else if (context->ignore_unknown)
1569 /* !context->ignore_unknown && parsed */
1571 if (context->ignore_unknown)
1573 gchar *new_arg = NULL;
1575 for (j = 0; j < arg_length; j++)
1580 new_arg = g_malloc (arg_length + 1);
1581 new_arg[arg_index++] = arg[j];
1585 new_arg[arg_index] = '\0';
1586 add_pending_null (context, &((*argv)[i]), new_arg);
1590 add_pending_null (context, &((*argv)[i]), NULL);
1598 if (!parsed && !context->ignore_unknown)
1601 G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
1602 _("Unknown option %s"), (*argv)[i]);
1608 /* Collect remaining args */
1609 if (context->main_group &&
1610 !parse_remaining_arg (context, context->main_group, &i,
1611 argc, argv, error, &parsed))
1614 if (!parsed && (has_unknown || (*argv)[i][0] == '-'))
1619 if (separator_pos > 0)
1620 add_pending_null (context, &((*argv)[separator_pos]), NULL);
1624 /* Call post-parse hooks */
1625 list = context->groups;
1628 GOptionGroup *group = list->data;
1630 if (group->post_parse_func)
1632 if (!(* group->post_parse_func) (context, group,
1633 group->user_data, error))
1640 if (context->main_group && context->main_group->post_parse_func)
1642 if (!(* context->main_group->post_parse_func) (context, context->main_group,
1643 context->main_group->user_data, error))
1649 free_pending_nulls (context, TRUE);
1651 for (i = 1; i < *argc; i++)
1653 for (k = i; k < *argc; k++)
1654 if ((*argv)[k] != NULL)
1660 for (j = i + k; j < *argc; j++)
1662 (*argv)[j-k] = (*argv)[j];
1674 /* Call error hooks */
1675 list = context->groups;
1678 GOptionGroup *group = list->data;
1680 if (group->error_func)
1681 (* group->error_func) (context, group,
1682 group->user_data, error);
1687 if (context->main_group && context->main_group->error_func)
1688 (* context->main_group->error_func) (context, context->main_group,
1689 context->main_group->user_data, error);
1691 free_changes_list (context, TRUE);
1692 free_pending_nulls (context, FALSE);
1698 * g_option_group_new:
1699 * @name: the name for the option group, this is used to provide
1700 * help for the options in this group with <option>--help-</option>@name
1701 * @description: a description for this group to be shown in
1702 * <option>--help</option>. This string is translated using the translation
1703 * domain or translation function of the group
1704 * @help_description: a description for the <option>--help-</option>@name option.
1705 * This string is translated using the translation domain or translation function
1707 * @user_data: user data that will be passed to the pre- and post-parse hooks,
1708 * the error hook and to callbacks of %G_OPTION_ARG_CALLBACK options, or %NULL
1709 * @destroy: a function that will be called to free @user_data, or %NULL
1711 * Creates a new #GOptionGroup.
1713 * Return value: a newly created option group. It should be added
1714 * to a #GOptionContext or freed with g_option_group_free().
1719 g_option_group_new (const gchar *name,
1720 const gchar *description,
1721 const gchar *help_description,
1723 GDestroyNotify destroy)
1726 GOptionGroup *group;
1728 group = g_new0 (GOptionGroup, 1);
1729 group->name = g_strdup (name);
1730 group->description = g_strdup (description);
1731 group->help_description = g_strdup (help_description);
1732 group->user_data = user_data;
1733 group->destroy_notify = destroy;
1740 * g_option_group_free:
1741 * @group: a #GOptionGroup
1743 * Frees a #GOptionGroup. Note that you must <emphasis>not</emphasis>
1744 * free groups which have been added to a #GOptionContext.
1749 g_option_group_free (GOptionGroup *group)
1751 g_return_if_fail (group != NULL);
1753 g_free (group->name);
1754 g_free (group->description);
1755 g_free (group->help_description);
1757 g_free (group->entries);
1759 if (group->destroy_notify)
1760 (* group->destroy_notify) (group->user_data);
1762 if (group->translate_notify)
1763 (* group->translate_notify) (group->translate_data);
1770 * g_option_group_add_entries:
1771 * @group: a #GOptionGroup
1772 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
1774 * Adds the options specified in @entries to @group.
1779 g_option_group_add_entries (GOptionGroup *group,
1780 const GOptionEntry *entries)
1784 g_return_if_fail (entries != NULL);
1786 for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++) ;
1788 group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
1790 memcpy (group->entries + group->n_entries, entries, sizeof (GOptionEntry) * n_entries);
1792 for (i = group->n_entries; i < group->n_entries + n_entries; i++)
1794 gchar c = group->entries[i].short_name;
1798 if (c == '-' || !g_ascii_isprint (c))
1800 g_warning (G_STRLOC": ignoring invalid short option '%c' (%d)", c, c);
1801 group->entries[i].short_name = 0;
1806 group->n_entries += n_entries;
1810 * g_option_group_set_parse_hooks:
1811 * @group: a #GOptionGroup
1812 * @pre_parse_func: a function to call before parsing, or %NULL
1813 * @post_parse_func: a function to call after parsing, or %NULL
1815 * Associates two functions with @group which will be called
1816 * from g_option_context_parse() before the first option is parsed
1817 * and after the last option has been parsed, respectively.
1819 * Note that the user data to be passed to @pre_parse_func and
1820 * @post_parse_func can be specified when constructing the group
1821 * with g_option_group_new().
1826 g_option_group_set_parse_hooks (GOptionGroup *group,
1827 GOptionParseFunc pre_parse_func,
1828 GOptionParseFunc post_parse_func)
1830 g_return_if_fail (group != NULL);
1832 group->pre_parse_func = pre_parse_func;
1833 group->post_parse_func = post_parse_func;
1837 * g_option_group_set_error_hook:
1838 * @group: a #GOptionGroup
1839 * @error_func: a function to call when an error occurs
1841 * Associates a function with @group which will be called
1842 * from g_option_context_parse() when an error occurs.
1844 * Note that the user data to be passed to @pre_parse_func and
1845 * @post_parse_func can be specified when constructing the group
1846 * with g_option_group_new().
1851 g_option_group_set_error_hook (GOptionGroup *group,
1852 GOptionErrorFunc error_func)
1854 g_return_if_fail (group != NULL);
1856 group->error_func = error_func;
1861 * g_option_group_set_translate_func:
1862 * @group: a #GOptionGroup
1863 * @func: the #GTranslateFunc, or %NULL
1864 * @data: user data to pass to @func, or %NULL
1865 * @destroy_notify: a function which gets called to free @data, or %NULL
1867 * Sets the function which is used to translate user-visible
1868 * strings, for <option>--help</option> output. Different
1869 * groups can use different #GTranslateFunc<!-- -->s. If @func
1870 * is %NULL, strings are not translated.
1872 * If you are using gettext(), you only need to set the translation
1873 * domain, see g_option_group_set_translation_domain().
1878 g_option_group_set_translate_func (GOptionGroup *group,
1879 GTranslateFunc func,
1881 GDestroyNotify destroy_notify)
1883 g_return_if_fail (group != NULL);
1885 if (group->translate_notify)
1886 group->translate_notify (group->translate_data);
1888 group->translate_func = func;
1889 group->translate_data = data;
1890 group->translate_notify = destroy_notify;
1894 dgettext_swapped (const gchar *msgid,
1895 const gchar *domainname)
1897 return dgettext (domainname, msgid);
1901 * g_option_group_set_translation_domain:
1902 * @group: a #GOptionGroup
1903 * @domain: the domain to use
1905 * A convenience function to use gettext() for translating
1906 * user-visible strings.
1911 g_option_group_set_translation_domain (GOptionGroup *group,
1912 const gchar *domain)
1914 g_return_if_fail (group != NULL);
1916 g_option_group_set_translate_func (group,
1917 (GTranslateFunc)dgettext_swapped,
1923 * g_option_context_set_translate_func:
1924 * @context: a #GOptionContext
1925 * @func: the #GTranslateFunc, or %NULL
1926 * @data: user data to pass to @func, or %NULL
1927 * @destroy_notify: a function which gets called to free @data, or %NULL
1929 * Sets the function which is used to translate the contexts
1930 * user-visible strings, for <option>--help</option> output.
1931 * If @func is %NULL, strings are not translated.
1933 * Note that option groups have their own translation functions,
1934 * this function only affects the @parameter_string (see g_option_context_nex()),
1935 * the summary (see g_option_context_set_summary()) and the description
1936 * (see g_option_context_set_description()).
1938 * If you are using gettext(), you only need to set the translation
1939 * domain, see g_context_group_set_translation_domain().
1944 g_option_context_set_translate_func (GOptionContext *context,
1945 GTranslateFunc func,
1947 GDestroyNotify destroy_notify)
1949 g_return_if_fail (context != NULL);
1951 if (context->translate_notify)
1952 context->translate_notify (context->translate_data);
1954 context->translate_func = func;
1955 context->translate_data = data;
1956 context->translate_notify = destroy_notify;
1960 * g_option_context_set_translation_domain:
1961 * @context: a #GOptionContext
1962 * @domain: the domain to use
1964 * A convenience function to use gettext() for translating
1965 * user-visible strings.
1970 g_option_context_set_translation_domain (GOptionContext *context,
1971 const gchar *domain)
1973 g_return_if_fail (context != NULL);
1975 g_option_context_set_translate_func (context,
1976 (GTranslateFunc)dgettext_swapped,
1982 * g_option_context_set_summary:
1983 * @context: a #GOptionContext
1984 * @summary: a string to be shown in <option>--help</option> output
1985 * before the list of options, or %NULL
1987 * Adds a string to be displayed in <option>--help</option> output
1988 * before the list of options. This is typically a summary of the
1989 * program functionality.
1991 * Note that the summary is translated (see
1992 * g_option_context_set_translate_func()).
1997 g_option_context_set_summary (GOptionContext *context,
1998 const gchar *summary)
2000 g_return_if_fail (context != NULL);
2002 g_free (context->summary);
2003 context->summary = g_strdup (summary);
2008 * g_option_context_get_summary:
2009 * @context: a #GOptionContext
2011 * Returns the summary. See g_option_context_set_summary().
2013 * Returns: the summary
2017 G_CONST_RETURN gchar *
2018 g_option_context_get_summary (GOptionContext *context)
2020 g_return_val_if_fail (context != NULL, NULL);
2022 return context->summary;
2026 * g_option_context_set_description:
2027 * @context: a #GOptionContext
2028 * @description: a string to be shown in <option>--help</option> output
2029 * after the list of options, or %NULL
2031 * Adds a string to be displayed in <option>--help</option> output
2032 * after the list of options. This text often includes a bug reporting
2035 * Note that the summary is translated (see
2036 * g_option_context_set_translate_func()).
2041 g_option_context_set_description (GOptionContext *context,
2042 const gchar *description)
2044 g_return_if_fail (context != NULL);
2046 g_free (context->description);
2047 context->description = g_strdup (description);
2052 * g_option_context_get_description:
2053 * @context: a #GOptionContext
2055 * Returns the description. See g_option_context_set_description().
2057 * Returns: the description
2061 G_CONST_RETURN gchar *
2062 g_option_context_get_description (GOptionContext *context)
2064 g_return_val_if_fail (context != NULL, NULL);
2066 return context->description;
2070 #define __G_OPTION_C__
2071 #include "galiasdef.c"