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)
71 struct _GOptionContext
75 gchar *parameter_string;
77 gboolean help_enabled;
78 gboolean ignore_unknown;
80 GOptionGroup *main_group;
82 /* We keep a list of change so we can revert them */
85 /* We also keep track of all argv elements that should be NULLed or
95 gchar *help_description;
97 GDestroyNotify destroy_notify;
100 GTranslateFunc translate_func;
101 GDestroyNotify translate_notify;
102 gpointer translate_data;
104 GOptionEntry *entries;
107 GOptionParseFunc pre_parse_func;
108 GOptionParseFunc post_parse_func;
109 GOptionErrorFunc error_func;
112 static void free_changes_list (GOptionContext *context,
114 static void free_pending_nulls (GOptionContext *context,
115 gboolean perform_nulls);
118 g_option_error_quark (void)
123 q = g_quark_from_static_string ("g-option-context-error-quark");
129 * g_option_context_new:
130 * @parameter_string: a string which is displayed in
131 * the first line of <option>--help</option> output, after the
133 * <literal><replaceable>programname</replaceable> [OPTION...]</literal>
135 * Creates a new option context.
137 * The @parameter_text can serve multiple purposes. It can be used
138 * to add descriptions for "rest" arguments, which are not parsed by
139 * the #GOptionContext, typically something like "FILES" or
140 * "FILE1 FILE2...". (If you are using #G_OPTION_REMAINING for
141 * collecting "rest" arguments, GLib handles this automatically by
142 * using the @arg_description of the corresponding #GOptionEntry in
143 * the usage summary.)
145 * Another common usage is to give a summary of the program
146 * functionality. This can be a short summary on the same line,
147 * like " - frob the strings", or a longer description in a paragraph
148 * below the usage summary. In this case, @parameter_string should start
149 * with two newlines, to separate the description from the usage summary:
150 * "\n\nA program to frob strings, which will..."
152 * Returns: a newly created #GOptionContext, which must be
153 * freed with g_option_context_free() after use.
158 g_option_context_new (const gchar *parameter_string)
161 GOptionContext *context;
163 context = g_new0 (GOptionContext, 1);
165 context->parameter_string = g_strdup (parameter_string);
166 context->help_enabled = TRUE;
167 context->ignore_unknown = FALSE;
173 * g_option_context_free:
174 * @context: a #GOptionContext
176 * Frees context and all the groups which have been
181 void g_option_context_free (GOptionContext *context)
183 g_return_if_fail (context != NULL);
185 g_list_foreach (context->groups, (GFunc)g_option_group_free, NULL);
186 g_list_free (context->groups);
188 if (context->main_group)
189 g_option_group_free (context->main_group);
191 free_changes_list (context, FALSE);
192 free_pending_nulls (context, FALSE);
194 g_free (context->parameter_string);
201 * g_option_context_set_help_enabled:
202 * @context: a #GOptionContext
203 * @help_enabled: %TRUE to enable <option>--help</option>, %FALSE to disable it
205 * Enables or disables automatic generation of <option>--help</option>
206 * output. By default, g_option_context_parse() recognizes
207 * <option>--help</option>, <option>-?</option>, <option>--help-all</option>
208 * and <option>--help-</option><replaceable>groupname</replaceable> and creates
209 * suitable output to stdout.
213 void g_option_context_set_help_enabled (GOptionContext *context,
214 gboolean help_enabled)
217 g_return_if_fail (context != NULL);
219 context->help_enabled = help_enabled;
223 * g_option_context_get_help_enabled:
224 * @context: a #GOptionContext
226 * Returns whether automatic <option>--help</option> generation
227 * is turned on for @context. See g_option_context_set_help_enabled().
229 * Returns: %TRUE if automatic help generation is turned on.
234 g_option_context_get_help_enabled (GOptionContext *context)
236 g_return_val_if_fail (context != NULL, FALSE);
238 return context->help_enabled;
242 * g_option_context_set_ignore_unknown_options:
243 * @context: a #GOptionContext
244 * @ignore_unknown: %TRUE to ignore unknown options, %FALSE to produce
245 * an error when unknown options are met
247 * Sets whether to ignore unknown options or not. If an argument is
248 * ignored, it is left in the @argv array after parsing. By default,
249 * g_option_context_parse() treats unknown options as error.
251 * This setting does not affect non-option arguments (i.e. arguments
252 * which don't start with a dash). But note that GOption cannot reliably
253 * determine whether a non-option belongs to a preceding unknown option.
258 g_option_context_set_ignore_unknown_options (GOptionContext *context,
259 gboolean ignore_unknown)
261 g_return_if_fail (context != NULL);
263 context->ignore_unknown = ignore_unknown;
267 * g_option_context_get_ignore_unknown_options:
268 * @context: a #GOptionContext
270 * Returns whether unknown options are ignored or not. See
271 * g_option_context_set_ignore_unknown_options().
273 * Returns: %TRUE if unknown options are ignored.
278 g_option_context_get_ignore_unknown_options (GOptionContext *context)
280 g_return_val_if_fail (context != NULL, FALSE);
282 return context->ignore_unknown;
286 * g_option_context_add_group:
287 * @context: a #GOptionContext
288 * @group: the group to add
290 * Adds a #GOptionGroup to the @context, so that parsing with @context
291 * will recognize the options in the group. Note that the group will
292 * be freed together with the context when g_option_context_free() is
293 * called, so you must not free the group yourself after adding it
299 g_option_context_add_group (GOptionContext *context,
304 g_return_if_fail (context != NULL);
305 g_return_if_fail (group != NULL);
306 g_return_if_fail (group->name != NULL);
307 g_return_if_fail (group->description != NULL);
308 g_return_if_fail (group->help_description != NULL);
310 for (list = context->groups; list; list = list->next)
312 GOptionGroup *g = (GOptionGroup *)list->data;
314 if ((group->name == NULL && g->name == NULL) ||
315 (group->name && g->name && strcmp (group->name, g->name) == 0))
316 g_warning ("A group named \"%s\" is already part of this GOptionContext",
320 context->groups = g_list_append (context->groups, group);
324 * g_option_context_set_main_group:
325 * @context: a #GOptionContext
326 * @group: the group to set as main group
328 * Sets a #GOptionGroup as main group of the @context.
329 * This has the same effect as calling g_option_context_add_group(),
330 * the only difference is that the options in the main group are
331 * treated differently when generating <option>--help</option> output.
336 g_option_context_set_main_group (GOptionContext *context,
339 g_return_if_fail (context != NULL);
340 g_return_if_fail (group != NULL);
342 if (context->main_group)
344 g_warning ("This GOptionContext already has a main group");
349 context->main_group = group;
353 * g_option_context_get_main_group:
354 * @context: a #GOptionContext
356 * Returns a pointer to the main group of @context.
358 * Return value: the main group of @context, or %NULL if @context doesn't
359 * have a main group. Note that group belongs to @context and should
360 * not be modified or freed.
365 g_option_context_get_main_group (GOptionContext *context)
367 g_return_val_if_fail (context != NULL, NULL);
369 return context->main_group;
373 * g_option_context_add_main_entries:
374 * @context: a #GOptionContext
375 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
376 * @translation_domain: a translation domain to use for translating
377 * the <option>--help</option> output for the options in @entries
378 * with gettext(), or %NULL
380 * A convenience function which creates a main group if it doesn't
381 * exist, adds the @entries to it and sets the translation domain.
386 g_option_context_add_main_entries (GOptionContext *context,
387 const GOptionEntry *entries,
388 const gchar *translation_domain)
390 g_return_if_fail (entries != NULL);
392 if (!context->main_group)
393 context->main_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
395 g_option_group_add_entries (context->main_group, entries);
396 g_option_group_set_translation_domain (context->main_group, translation_domain);
400 calculate_max_length (GOptionGroup *group)
403 gint i, len, max_length;
407 for (i = 0; i < group->n_entries; i++)
409 entry = &group->entries[i];
411 if (entry->flags & G_OPTION_FLAG_HIDDEN)
414 len = g_utf8_strlen (entry->long_name, -1);
416 if (entry->short_name)
419 if (!NO_ARG (entry) && entry->arg_description)
420 len += 1 + g_utf8_strlen (TRANSLATE (group, entry->arg_description), -1);
422 max_length = MAX (max_length, len);
429 print_entry (GOptionGroup *group,
431 const GOptionEntry *entry)
435 if (entry->flags & G_OPTION_FLAG_HIDDEN)
438 if (entry->long_name[0] == 0)
441 str = g_string_new (NULL);
443 if (entry->short_name)
444 g_string_append_printf (str, " -%c, --%s", entry->short_name, entry->long_name);
446 g_string_append_printf (str, " --%s", entry->long_name);
448 if (entry->arg_description)
449 g_string_append_printf (str, "=%s", TRANSLATE (group, entry->arg_description));
451 g_print ("%-*s %s\n", max_length + 4, str->str,
452 entry->description ? TRANSLATE (group, entry->description) : "");
453 g_string_free (str, TRUE);
457 print_help (GOptionContext *context,
462 gint max_length, len;
465 GHashTable *shadow_map;
467 const gchar *rest_description;
469 rest_description = NULL;
470 if (context->main_group)
472 for (i = 0; i < context->main_group->n_entries; i++)
474 entry = &context->main_group->entries[i];
475 if (entry->long_name[0] == 0)
477 rest_description = entry->arg_description;
483 g_print ("%s\n %s %s%s%s%s%s\n\n",
484 _("Usage:"), g_get_prgname(), _("[OPTION...]"),
485 rest_description ? " " : "",
486 rest_description ? rest_description : "",
487 context->parameter_string ? " " : "",
488 context->parameter_string ? context->parameter_string : "");
490 memset (seen, 0, sizeof (gboolean) * 256);
491 shadow_map = g_hash_table_new (g_str_hash, g_str_equal);
493 if (context->main_group)
495 for (i = 0; i < context->main_group->n_entries; i++)
497 entry = &context->main_group->entries[i];
498 g_hash_table_insert (shadow_map,
499 (gpointer)entry->long_name,
502 if (seen[(guchar)entry->short_name])
503 entry->short_name = 0;
505 seen[(guchar)entry->short_name] = TRUE;
509 list = context->groups;
512 GOptionGroup *group = list->data;
513 for (i = 0; i < group->n_entries; i++)
515 entry = &group->entries[i];
516 if (g_hash_table_lookup (shadow_map, entry->long_name) &&
517 !(entry->flags && G_OPTION_FLAG_NOALIAS))
518 entry->long_name = g_strdup_printf ("%s-%s", group->name, entry->long_name);
520 g_hash_table_insert (shadow_map, (gpointer)entry->long_name, entry);
522 if (seen[(guchar)entry->short_name] &&
523 !(entry->flags && G_OPTION_FLAG_NOALIAS))
524 entry->short_name = 0;
526 seen[(guchar)entry->short_name] = TRUE;
531 g_hash_table_destroy (shadow_map);
533 list = context->groups;
535 max_length = g_utf8_strlen ("-?, --help", -1);
539 len = g_utf8_strlen ("--help-all", -1);
540 max_length = MAX (max_length, len);
543 if (context->main_group)
545 len = calculate_max_length (context->main_group);
546 max_length = MAX (max_length, len);
551 GOptionGroup *group = list->data;
553 /* First, we check the --help-<groupname> options */
554 len = g_utf8_strlen ("--help-", -1) + g_utf8_strlen (group->name, -1);
555 max_length = MAX (max_length, len);
557 /* Then we go through the entries */
558 len = calculate_max_length (group);
559 max_length = MAX (max_length, len);
564 /* Add a bit of padding */
569 list = context->groups;
571 g_print ("%s\n -%c, --%-*s %s\n",
572 _("Help Options:"), '?', max_length - 4, "help",
573 _("Show help options"));
575 /* We only want --help-all when there are groups */
577 g_print (" --%-*s %s\n", max_length, "help-all",
578 _("Show all help options"));
582 GOptionGroup *group = list->data;
584 g_print (" --help-%-*s %s\n", max_length - 5, group->name,
585 TRANSLATE (group, group->help_description));
595 /* Print a certain group */
597 g_print ("%s\n", TRANSLATE (group, group->description));
598 for (i = 0; i < group->n_entries; i++)
599 print_entry (group, max_length, &group->entries[i]);
604 /* Print all groups */
606 list = context->groups;
610 GOptionGroup *group = list->data;
612 g_print ("%s\n", group->description);
614 for (i = 0; i < group->n_entries; i++)
615 if (!(group->entries[i].flags & G_OPTION_FLAG_IN_MAIN))
616 print_entry (group, max_length, &group->entries[i]);
623 /* Print application options if --help or --help-all has been specified */
624 if (main_help || !group)
626 list = context->groups;
628 g_print ("%s\n", _("Application Options:"));
630 if (context->main_group)
631 for (i = 0; i < context->main_group->n_entries; i++)
632 print_entry (context->main_group, max_length,
633 &context->main_group->entries[i]);
637 GOptionGroup *group = list->data;
639 /* Print main entries from other groups */
640 for (i = 0; i < group->n_entries; i++)
641 if (group->entries[i].flags & G_OPTION_FLAG_IN_MAIN)
642 print_entry (group, max_length, &group->entries[i]);
654 parse_int (const gchar *arg_name,
663 tmp = strtol (arg, &end, 0);
665 if (*arg == '\0' || *end != '\0')
668 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
669 _("Cannot parse integer value '%s' for %s"),
675 if (*result != tmp || errno == ERANGE)
678 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
679 _("Integer value '%s' for %s out of range"),
688 get_change (GOptionContext *context,
693 Change *change = NULL;
695 for (list = context->changes; list != NULL; list = list->next)
699 if (change->arg_data == arg_data)
703 change = g_new0 (Change, 1);
704 change->arg_type = arg_type;
705 change->arg_data = arg_data;
707 context->changes = g_list_prepend (context->changes, change);
715 add_pending_null (GOptionContext *context,
721 n = g_new0 (PendingNull, 1);
725 context->pending_nulls = g_list_prepend (context->pending_nulls, n);
729 parse_arg (GOptionContext *context,
733 const gchar *option_name,
741 case G_OPTION_ARG_NONE:
743 change = get_change (context, G_OPTION_ARG_NONE,
746 *(gboolean *)entry->arg_data = !(entry->flags & G_OPTION_FLAG_REVERSE);
749 case G_OPTION_ARG_STRING:
753 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
758 change = get_change (context, G_OPTION_ARG_STRING,
760 g_free (change->allocated.str);
762 change->prev.str = *(gchar **)entry->arg_data;
763 change->allocated.str = data;
765 *(gchar **)entry->arg_data = data;
768 case G_OPTION_ARG_STRING_ARRAY:
772 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
777 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
780 if (change->allocated.array.len == 0)
782 change->prev.array = *(gchar ***)entry->arg_data;
783 change->allocated.array.data = g_new (gchar *, 2);
786 change->allocated.array.data =
787 g_renew (gchar *, change->allocated.array.data,
788 change->allocated.array.len + 2);
790 change->allocated.array.data[change->allocated.array.len] = data;
791 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
793 change->allocated.array.len ++;
795 *(gchar ***)entry->arg_data = change->allocated.array.data;
800 case G_OPTION_ARG_FILENAME:
805 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
810 data = g_strdup (value);
812 change = get_change (context, G_OPTION_ARG_FILENAME,
814 g_free (change->allocated.str);
816 change->prev.str = *(gchar **)entry->arg_data;
817 change->allocated.str = data;
819 *(gchar **)entry->arg_data = data;
823 case G_OPTION_ARG_FILENAME_ARRAY:
828 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
833 data = g_strdup (value);
835 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
838 if (change->allocated.array.len == 0)
840 change->prev.array = *(gchar ***)entry->arg_data;
841 change->allocated.array.data = g_new (gchar *, 2);
844 change->allocated.array.data =
845 g_renew (gchar *, change->allocated.array.data,
846 change->allocated.array.len + 2);
848 change->allocated.array.data[change->allocated.array.len] = data;
849 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
851 change->allocated.array.len ++;
853 *(gchar ***)entry->arg_data = change->allocated.array.data;
858 case G_OPTION_ARG_INT:
862 if (!parse_int (option_name, value,
867 change = get_change (context, G_OPTION_ARG_INT,
869 change->prev.integer = *(gint *)entry->arg_data;
870 *(gint *)entry->arg_data = data;
873 case G_OPTION_ARG_CALLBACK:
878 if (!value && entry->flags & G_OPTION_FLAG_OPTIONAL_ARG)
880 else if (entry->flags & G_OPTION_FLAG_NO_ARG)
882 else if (entry->flags & G_OPTION_FLAG_FILENAME)
885 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
887 data = g_strdup (value);
891 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
893 if (!(entry->flags & (G_OPTION_FLAG_NO_ARG|G_OPTION_FLAG_OPTIONAL_ARG)) &&
897 retval = (* (GOptionArgFunc) entry->arg_data) (option_name, data, group->user_data, error);
906 g_assert_not_reached ();
913 parse_short_option (GOptionContext *context,
925 for (j = 0; j < group->n_entries; j++)
927 if (arg == group->entries[j].short_name)
932 option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
934 if (NO_ARG (&group->entries[j]))
938 if (*new_index > index)
941 G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
942 _("Error parsing option %s"), option_name);
943 g_free (option_name);
947 if (index < *argc - 1)
949 if (!OPTIONAL_ARG (&group->entries[j]))
951 value = (*argv)[index + 1];
952 add_pending_null (context, &((*argv)[index + 1]), NULL);
953 *new_index = index+1;
957 if ((*argv)[index + 1][0] == '-')
961 value = (*argv)[index + 1];
962 add_pending_null (context, &((*argv)[index + 1]), NULL);
963 *new_index = index + 1;
967 else if (index >= *argc - 1 && OPTIONAL_ARG (&group->entries[j]))
972 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
973 _("Missing argument for %s"), option_name);
974 g_free (option_name);
979 if (!parse_arg (context, group, &group->entries[j],
980 value, option_name, error))
982 g_free (option_name);
986 g_free (option_name);
995 parse_long_option (GOptionContext *context,
1007 for (j = 0; j < group->n_entries; j++)
1009 if (*index >= *argc)
1012 if (aliased && (group->entries[j].flags & G_OPTION_FLAG_NOALIAS))
1015 if (NO_ARG (&group->entries[j]) &&
1016 strcmp (arg, group->entries[j].long_name) == 0)
1020 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1021 parse_arg (context, group, &group->entries[j],
1022 NULL, option_name, error);
1023 g_free(option_name);
1025 add_pending_null (context, &((*argv)[*index]), NULL);
1030 gint len = strlen (group->entries[j].long_name);
1032 if (strncmp (arg, group->entries[j].long_name, len) == 0 &&
1033 (arg[len] == '=' || arg[len] == 0))
1035 gchar *value = NULL;
1038 add_pending_null (context, &((*argv)[*index]), NULL);
1039 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1041 if (arg[len] == '=')
1042 value = arg + len + 1;
1043 else if (*index < *argc - 1)
1045 if (!(group->entries[j].flags & G_OPTION_FLAG_OPTIONAL_ARG))
1047 value = (*argv)[*index + 1];
1048 add_pending_null (context, &((*argv)[*index + 1]), NULL);
1053 if ((*argv)[*index + 1][0] == '-')
1056 retval = parse_arg (context, group, &group->entries[j],
1057 NULL, option_name, error);
1059 g_free (option_name);
1064 value = (*argv)[*index + 1];
1065 add_pending_null (context, &((*argv)[*index + 1]), NULL);
1070 else if (*index >= *argc - 1 &&
1071 group->entries[j].flags & G_OPTION_FLAG_OPTIONAL_ARG)
1074 retval = parse_arg (context, group, &group->entries[j],
1075 NULL, option_name, error);
1077 g_free (option_name);
1083 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1084 _("Missing argument for %s"), option_name);
1085 g_free (option_name);
1089 if (!parse_arg (context, group, &group->entries[j],
1090 value, option_name, error))
1092 g_free (option_name);
1096 g_free (option_name);
1106 parse_remaining_arg (GOptionContext *context,
1107 GOptionGroup *group,
1116 for (j = 0; j < group->n_entries; j++)
1118 if (*index >= *argc)
1121 if (group->entries[j].long_name[0])
1124 g_return_val_if_fail (group->entries[j].arg == G_OPTION_ARG_STRING_ARRAY ||
1125 group->entries[j].arg == G_OPTION_ARG_FILENAME_ARRAY, FALSE);
1127 add_pending_null (context, &((*argv)[*index]), NULL);
1129 if (!parse_arg (context, group, &group->entries[j], (*argv)[*index], "", error))
1140 free_changes_list (GOptionContext *context,
1145 for (list = context->changes; list != NULL; list = list->next)
1147 Change *change = list->data;
1151 switch (change->arg_type)
1153 case G_OPTION_ARG_NONE:
1154 *(gboolean *)change->arg_data = change->prev.bool;
1156 case G_OPTION_ARG_INT:
1157 *(gint *)change->arg_data = change->prev.integer;
1159 case G_OPTION_ARG_STRING:
1160 case G_OPTION_ARG_FILENAME:
1161 g_free (change->allocated.str);
1162 *(gchar **)change->arg_data = change->prev.str;
1164 case G_OPTION_ARG_STRING_ARRAY:
1165 case G_OPTION_ARG_FILENAME_ARRAY:
1166 g_strfreev (change->allocated.array.data);
1167 *(gchar ***)change->arg_data = change->prev.array;
1170 g_assert_not_reached ();
1177 g_list_free (context->changes);
1178 context->changes = NULL;
1182 free_pending_nulls (GOptionContext *context,
1183 gboolean perform_nulls)
1187 for (list = context->pending_nulls; list != NULL; list = list->next)
1189 PendingNull *n = list->data;
1195 /* Copy back the short options */
1197 strcpy (*n->ptr + 1, n->value);
1207 g_list_free (context->pending_nulls);
1208 context->pending_nulls = NULL;
1212 * g_option_context_parse:
1213 * @context: a #GOptionContext
1214 * @argc: a pointer to the number of command line arguments.
1215 * @argv: a pointer to the array of command line arguments.
1216 * @error: a return location for errors
1218 * Parses the command line arguments, recognizing options
1219 * which have been added to @context. A side-effect of
1220 * calling this function is that g_set_prgname() will be
1223 * If the parsing is successful, any parsed arguments are
1224 * removed from the array and @argc and @argv are updated
1225 * accordingly. A '--' option is stripped from @argv
1226 * unless there are unparsed options before and after it,
1227 * or some of the options after it start with '-'. In case
1228 * of an error, @argc and @argv are left unmodified.
1230 * If automatic <option>--help</option> support is enabled
1231 * (see g_option_context_set_help_enabled()), and the
1232 * @argv array contains one of the recognized help options,
1233 * this function will produce help output to stdout and
1234 * call <literal>exit (0)</literal>.
1236 * Return value: %TRUE if the parsing was successful,
1237 * %FALSE if an error occurred
1242 g_option_context_parse (GOptionContext *context,
1250 /* Set program name */
1251 if (!g_get_prgname())
1253 if (argc && argv && *argc)
1257 prgname = g_path_get_basename ((*argv)[0]);
1258 g_set_prgname (prgname);
1262 g_set_prgname ("<unknown>");
1265 /* Call pre-parse hooks */
1266 list = context->groups;
1269 GOptionGroup *group = list->data;
1271 if (group->pre_parse_func)
1273 if (!(* group->pre_parse_func) (context, group,
1274 group->user_data, error))
1281 if (context->main_group && context->main_group->pre_parse_func)
1283 if (!(* context->main_group->pre_parse_func) (context, context->main_group,
1284 context->main_group->user_data, error))
1290 gboolean stop_parsing = FALSE;
1291 gboolean has_unknown = FALSE;
1292 gint separator_pos = 0;
1294 for (i = 1; i < *argc; i++)
1297 gboolean parsed = FALSE;
1299 if ((*argv)[i][0] == '-' && (*argv)[i][1] != '\0' && !stop_parsing)
1301 if ((*argv)[i][1] == '-')
1305 arg = (*argv)[i] + 2;
1307 /* '--' terminates list of arguments */
1311 stop_parsing = TRUE;
1315 /* Handle help options */
1316 if (context->help_enabled)
1318 if (strcmp (arg, "help") == 0)
1319 print_help (context, TRUE, NULL);
1320 else if (strcmp (arg, "help-all") == 0)
1321 print_help (context, FALSE, NULL);
1322 else if (strncmp (arg, "help-", 5) == 0)
1326 list = context->groups;
1330 GOptionGroup *group = list->data;
1332 if (strcmp (arg + 5, group->name) == 0)
1333 print_help (context, FALSE, group);
1340 if (context->main_group &&
1341 !parse_long_option (context, context->main_group, &i, arg,
1342 FALSE, argc, argv, error, &parsed))
1348 /* Try the groups */
1349 list = context->groups;
1352 GOptionGroup *group = list->data;
1354 if (!parse_long_option (context, group, &i, arg,
1355 FALSE, argc, argv, error, &parsed))
1367 /* Now look for --<group>-<option> */
1368 dash = strchr (arg, '-');
1371 /* Try the groups */
1372 list = context->groups;
1375 GOptionGroup *group = list->data;
1377 if (strncmp (group->name, arg, dash - arg) == 0)
1379 if (!parse_long_option (context, group, &i, dash + 1,
1380 TRUE, argc, argv, error, &parsed))
1391 if (context->ignore_unknown)
1399 gboolean *nulled_out = NULL;
1401 arg = (*argv)[i] + 1;
1405 if (context->ignore_unknown)
1406 nulled_out = g_new0 (gboolean, strlen (arg));
1408 for (j = 0; j < strlen (arg); j++)
1410 if (context->help_enabled && arg[j] == '?')
1411 print_help (context, TRUE, NULL);
1415 if (context->main_group &&
1416 !parse_short_option (context, context->main_group,
1418 argc, argv, error, &parsed))
1421 g_free (nulled_out);
1427 /* Try the groups */
1428 list = context->groups;
1431 GOptionGroup *group = list->data;
1433 if (!parse_short_option (context, group, i, &new_i, arg[j],
1434 argc, argv, error, &parsed))
1444 if (context->ignore_unknown)
1447 nulled_out[j] = TRUE;
1456 if (context->ignore_unknown)
1458 gchar *new_arg = NULL;
1461 for (j = 0; j < strlen (arg); j++)
1466 new_arg = g_malloc (strlen (arg) + 1);
1467 new_arg[arg_index++] = arg[j];
1471 new_arg[arg_index] = '\0';
1473 add_pending_null (context, &((*argv)[i]), new_arg);
1477 add_pending_null (context, &((*argv)[i]), NULL);
1485 if (!parsed && !context->ignore_unknown)
1488 G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
1489 _("Unknown option %s"), (*argv)[i]);
1495 /* Collect remaining args */
1496 if (context->main_group &&
1497 !parse_remaining_arg (context, context->main_group, &i,
1498 argc, argv, error, &parsed))
1501 if (!parsed && (has_unknown || (*argv)[i][0] == '-'))
1506 if (separator_pos > 0)
1507 add_pending_null (context, &((*argv)[separator_pos]), NULL);
1511 /* Call post-parse hooks */
1512 list = context->groups;
1515 GOptionGroup *group = list->data;
1517 if (group->post_parse_func)
1519 if (!(* group->post_parse_func) (context, group,
1520 group->user_data, error))
1527 if (context->main_group && context->main_group->post_parse_func)
1529 if (!(* context->main_group->post_parse_func) (context, context->main_group,
1530 context->main_group->user_data, error))
1536 free_pending_nulls (context, TRUE);
1538 for (i = 1; i < *argc; i++)
1540 for (k = i; k < *argc; k++)
1541 if ((*argv)[k] != NULL)
1547 for (j = i + k; j < *argc; j++)
1549 (*argv)[j-k] = (*argv)[j];
1561 /* Call error hooks */
1562 list = context->groups;
1565 GOptionGroup *group = list->data;
1567 if (group->error_func)
1568 (* group->error_func) (context, group,
1569 group->user_data, error);
1574 if (context->main_group && context->main_group->error_func)
1575 (* context->main_group->error_func) (context, context->main_group,
1576 context->main_group->user_data, error);
1578 free_changes_list (context, TRUE);
1579 free_pending_nulls (context, FALSE);
1585 * g_option_group_new:
1586 * @name: the name for the option group, this is used to provide
1587 * help for the options in this group with <option>--help-</option>@name
1588 * @description: a description for this group to be shown in
1589 * <option>--help</option>. This string is translated using the translation
1590 * domain or translation function of the group
1591 * @help_description: a description for the <option>--help-</option>@name option.
1592 * This string is translated using the translation domain or translation function
1594 * @user_data: user data that will be passed to the pre- and post-parse hooks,
1595 * the error hook and to callbacks of %G_OPTION_ARG_CALLBACK options, or %NULL
1596 * @destroy: a function that will be called to free @user_data, or %NULL
1598 * Creates a new #GOptionGroup.
1600 * Return value: a newly created option group. It should be added
1601 * to a #GOptionContext or freed with g_option_group_free().
1606 g_option_group_new (const gchar *name,
1607 const gchar *description,
1608 const gchar *help_description,
1610 GDestroyNotify destroy)
1613 GOptionGroup *group;
1615 group = g_new0 (GOptionGroup, 1);
1616 group->name = g_strdup (name);
1617 group->description = g_strdup (description);
1618 group->help_description = g_strdup (help_description);
1619 group->user_data = user_data;
1620 group->destroy_notify = destroy;
1627 * g_option_group_free:
1628 * @group: a #GOptionGroup
1630 * Frees a #GOptionGroup. Note that you must <emphasis>not</emphasis>
1631 * free groups which have been added to a #GOptionContext.
1636 g_option_group_free (GOptionGroup *group)
1638 g_return_if_fail (group != NULL);
1640 g_free (group->name);
1641 g_free (group->description);
1642 g_free (group->help_description);
1644 g_free (group->entries);
1646 if (group->destroy_notify)
1647 (* group->destroy_notify) (group->user_data);
1649 if (group->translate_notify)
1650 (* group->translate_notify) (group->translate_data);
1657 * g_option_group_add_entries:
1658 * @group: a #GOptionGroup
1659 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
1661 * Adds the options specified in @entries to @group.
1666 g_option_group_add_entries (GOptionGroup *group,
1667 const GOptionEntry *entries)
1671 g_return_if_fail (entries != NULL);
1673 for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++) ;
1675 group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
1677 memcpy (group->entries + group->n_entries, entries, sizeof (GOptionEntry) * n_entries);
1679 for (i = group->n_entries; i < group->n_entries + n_entries; i++)
1681 gchar c = group->entries[i].short_name;
1685 if (c == '-' || !g_ascii_isprint (c))
1687 g_warning (G_STRLOC": ignoring invalid short option '%c' (%d)", c, c);
1688 group->entries[i].short_name = 0;
1693 group->n_entries += n_entries;
1697 * g_option_group_set_parse_hooks:
1698 * @group: a #GOptionGroup
1699 * @pre_parse_func: a function to call before parsing, or %NULL
1700 * @post_parse_func: a function to call after parsing, or %NULL
1702 * Associates two functions with @group which will be called
1703 * from g_option_context_parse() before the first option is parsed
1704 * and after the last option has been parsed, respectively.
1706 * Note that the user data to be passed to @pre_parse_func and
1707 * @post_parse_func can be specified when constructing the group
1708 * with g_option_group_new().
1713 g_option_group_set_parse_hooks (GOptionGroup *group,
1714 GOptionParseFunc pre_parse_func,
1715 GOptionParseFunc post_parse_func)
1717 g_return_if_fail (group != NULL);
1719 group->pre_parse_func = pre_parse_func;
1720 group->post_parse_func = post_parse_func;
1724 * g_option_group_set_error_hook:
1725 * @group: a #GOptionGroup
1726 * @error_func: a function to call when an error occurs
1728 * Associates a function with @group which will be called
1729 * from g_option_context_parse() when an error occurs.
1731 * Note that the user data to be passed to @pre_parse_func and
1732 * @post_parse_func can be specified when constructing the group
1733 * with g_option_group_new().
1738 g_option_group_set_error_hook (GOptionGroup *group,
1739 GOptionErrorFunc error_func)
1741 g_return_if_fail (group != NULL);
1743 group->error_func = error_func;
1748 * g_option_group_set_translate_func:
1749 * @group: a #GOptionGroup
1750 * @func: the #GTranslateFunc, or %NULL
1751 * @data: user data to pass to @func, or %NULL
1752 * @destroy_notify: a function which gets called to free @data, or %NULL
1754 * Sets the function which is used to translate user-visible
1755 * strings, for <option>--help</option> output. Different
1756 * groups can use different #GTranslateFunc<!-- -->s. If @func
1757 * is %NULL, strings are not translated.
1759 * If you are using gettext(), you only need to set the translation
1760 * domain, see g_option_group_set_translation_domain().
1765 g_option_group_set_translate_func (GOptionGroup *group,
1766 GTranslateFunc func,
1768 GDestroyNotify destroy_notify)
1770 g_return_if_fail (group != NULL);
1772 if (group->translate_notify)
1773 group->translate_notify (group->translate_data);
1775 group->translate_func = func;
1776 group->translate_data = data;
1777 group->translate_notify = destroy_notify;
1781 dgettext_swapped (const gchar *msgid,
1782 const gchar *domainname)
1784 return dgettext (domainname, msgid);
1788 * g_option_group_set_translation_domain:
1789 * @group: a #GOptionGroup
1790 * @domain: the domain to use
1792 * A convenience function to use gettext() for translating
1793 * user-visible strings.
1798 g_option_group_set_translation_domain (GOptionGroup *group,
1799 const gchar *domain)
1801 g_return_if_fail (group != NULL);
1803 g_option_group_set_translate_func (group,
1804 (GTranslateFunc)dgettext_swapped,
1809 #define __G_OPTION_C__
1810 #include "galiasdef.c"