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)))
60 struct _GOptionContext
64 gchar *parameter_string;
66 gboolean help_enabled;
67 gboolean ignore_unknown;
69 GOptionGroup *main_group;
71 /* We keep a list of change so we can revert them */
74 /* We also keep track of all argv elements that should be NULLed or
84 gchar *help_description;
86 GDestroyNotify destroy_notify;
89 GTranslateFunc translate_func;
90 GDestroyNotify translate_notify;
91 gpointer translate_data;
93 GOptionEntry *entries;
96 GOptionParseFunc pre_parse_func;
97 GOptionParseFunc post_parse_func;
98 GOptionErrorFunc error_func;
101 static void free_changes_list (GOptionContext *context,
103 static void free_pending_nulls (GOptionContext *context,
104 gboolean perform_nulls);
107 g_option_error_quark (void)
112 q = g_quark_from_static_string ("g-option-context-error-quark");
118 * g_option_context_new:
119 * @parameter_string: a string which is displayed in
120 * the first line of <option>--help</option> output, after
121 * <literal><replaceable>programname</replaceable> [OPTION...]</literal>
123 * Creates a new option context.
125 * Returns: a newly created #GOptionContext, which must be
126 * freed with g_option_context_free() after use.
131 g_option_context_new (const gchar *parameter_string)
134 GOptionContext *context;
136 context = g_new0 (GOptionContext, 1);
138 context->parameter_string = g_strdup (parameter_string);
139 context->help_enabled = TRUE;
140 context->ignore_unknown = FALSE;
146 * g_option_context_free:
147 * @context: a #GOptionContext
149 * Frees context and all the groups which have been
154 void g_option_context_free (GOptionContext *context)
156 g_return_if_fail (context != NULL);
158 g_list_foreach (context->groups, (GFunc)g_option_group_free, NULL);
159 g_list_free (context->groups);
161 if (context->main_group)
162 g_option_group_free (context->main_group);
164 free_changes_list (context, FALSE);
165 free_pending_nulls (context, FALSE);
167 g_free (context->parameter_string);
174 * g_option_context_set_help_enabled:
175 * @context: a #GOptionContext
176 * @help_enabled: %TRUE to enable <option>--help</option>, %FALSE to disable it
178 * Enables or disables automatic generation of <option>--help</option>
179 * output. By default, g_option_context_parse() recognizes
180 * <option>--help</option>, <option>-?</option>, <option>--help-all</option>
181 * and <option>--help-</option><replaceable>groupname</replaceable> and creates
182 * suitable output to stdout.
186 void g_option_context_set_help_enabled (GOptionContext *context,
187 gboolean help_enabled)
190 g_return_if_fail (context != NULL);
192 context->help_enabled = help_enabled;
196 * g_option_context_get_help_enabled:
197 * @context: a #GOptionContext
199 * Returns whether automatic <option>--help</option> generation
200 * is turned on for @context. See g_option_context_set_help_enabled().
202 * Returns: %TRUE if automatic help generation is turned on.
207 g_option_context_get_help_enabled (GOptionContext *context)
209 g_return_val_if_fail (context != NULL, FALSE);
211 return context->help_enabled;
215 * g_option_context_set_ignore_unknown_options:
216 * @context: a #GOptionContext
217 * @ignore_unknown: %TRUE to ignore unknown options, %FALSE to produce
218 * an error when unknown options are met
220 * Sets whether to ignore unknown options or not. If an argument is
221 * ignored, it is left in the @argv array after parsing. By default,
222 * g_option_context_parse() treats unknown options as error.
224 * This setting does not affect non-option arguments (i.e. arguments
225 * which don't start with a dash). But note that GOption cannot reliably
226 * determine whether a non-option belongs to a preceding unknown option.
231 g_option_context_set_ignore_unknown_options (GOptionContext *context,
232 gboolean ignore_unknown)
234 g_return_if_fail (context != NULL);
236 context->ignore_unknown = ignore_unknown;
240 * g_option_context_get_ignore_unknown_options:
241 * @context: a #GOptionContext
243 * Returns whether unknown options are ignored or not. See
244 * g_option_context_set_ignore_unknown_options().
246 * Returns: %TRUE if unknown options are ignored.
251 g_option_context_get_ignore_unknown_options (GOptionContext *context)
253 g_return_val_if_fail (context != NULL, FALSE);
255 return context->ignore_unknown;
259 * g_option_context_add_group:
260 * @context: a #GOptionContext
261 * @group: the group to add
263 * Adds a #GOptionGroup to the @context, so that parsing with @context
264 * will recognize the options in the group. Note that the group will
265 * be freed together with the context when g_option_context_free() is
266 * called, so you must not free the group yourself after adding it
272 g_option_context_add_group (GOptionContext *context,
277 g_return_if_fail (context != NULL);
278 g_return_if_fail (group != NULL);
279 g_return_if_fail (group->name != NULL);
280 g_return_if_fail (group->description != NULL);
281 g_return_if_fail (group->help_description != NULL);
283 for (list = context->groups; list; list = list->next)
285 GOptionGroup *g = (GOptionGroup *)list->data;
287 if ((group->name == NULL && g->name == NULL) ||
288 (group->name && g->name && strcmp (group->name, g->name) == 0))
289 g_warning ("A group named \"%s\" is already part of this GOptionContext",
293 context->groups = g_list_append (context->groups, group);
297 * g_option_context_set_main_group:
298 * @context: a #GOptionContext
299 * @group: the group to set as main group
301 * Sets a #GOptionGroup as main group of the @context.
302 * This has the same effect as calling g_option_context_add_group(),
303 * the only difference is that the options in the main group are
304 * treated differently when generating <option>--help</option> output.
309 g_option_context_set_main_group (GOptionContext *context,
312 g_return_if_fail (context != NULL);
313 g_return_if_fail (group != NULL);
315 if (context->main_group)
317 g_warning ("This GOptionContext already has a main group");
322 context->main_group = group;
326 * g_option_context_get_main_group:
327 * @context: a #GOptionContext
329 * Returns a pointer to the main group of @context.
331 * Return value: the main group of @context, or %NULL if @context doesn't
332 * have a main group. Note that group belongs to @context and should
333 * not be modified or freed.
338 g_option_context_get_main_group (GOptionContext *context)
340 g_return_val_if_fail (context != NULL, NULL);
342 return context->main_group;
346 * g_option_context_add_main_entries:
347 * @context: a #GOptionContext
348 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
349 * @translation_domain: a translation domain to use for translating
350 * the <option>--help</option> output for the options in @entries
351 * with gettext(), or %NULL
353 * A convenience function which creates a main group if it doesn't
354 * exist, adds the @entries to it and sets the translation domain.
359 g_option_context_add_main_entries (GOptionContext *context,
360 const GOptionEntry *entries,
361 const gchar *translation_domain)
363 g_return_if_fail (entries != NULL);
365 if (!context->main_group)
366 context->main_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
368 g_option_group_add_entries (context->main_group, entries);
369 g_option_group_set_translation_domain (context->main_group, translation_domain);
373 calculate_max_length (GOptionGroup *group)
376 gint i, len, max_length;
380 for (i = 0; i < group->n_entries; i++)
382 entry = &group->entries[i];
384 if (entry->flags & G_OPTION_FLAG_HIDDEN)
387 len = g_utf8_strlen (entry->long_name, -1);
389 if (entry->short_name)
392 if (entry->arg != G_OPTION_ARG_NONE && entry->arg_description)
393 len += 1 + g_utf8_strlen (TRANSLATE (group, entry->arg_description), -1);
395 max_length = MAX (max_length, len);
402 print_entry (GOptionGroup *group,
404 const GOptionEntry *entry)
408 if (entry->flags & G_OPTION_FLAG_HIDDEN)
411 if (entry->long_name[0] == 0)
414 str = g_string_new (NULL);
416 if (entry->short_name)
417 g_string_append_printf (str, " -%c, --%s", entry->short_name, entry->long_name);
419 g_string_append_printf (str, " --%s", entry->long_name);
421 if (entry->arg_description)
422 g_string_append_printf (str, "=%s", TRANSLATE (group, entry->arg_description));
424 g_print ("%-*s %s\n", max_length + 4, str->str,
425 entry->description ? TRANSLATE (group, entry->description) : "");
426 g_string_free (str, TRUE);
430 print_help (GOptionContext *context,
435 gint max_length, len;
438 GHashTable *shadow_map;
440 const gchar *rest_description;
442 rest_description = NULL;
443 if (context->main_group)
445 for (i = 0; i < context->main_group->n_entries; i++)
447 entry = &context->main_group->entries[i];
448 if (entry->long_name[0] == 0)
450 rest_description = entry->arg_description;
456 g_print ("%s\n %s %s%s%s%s%s\n\n",
457 _("Usage:"), g_get_prgname(), _("[OPTION...]"),
458 rest_description ? " " : "",
459 rest_description ? rest_description : "",
460 context->parameter_string ? " " : "",
461 context->parameter_string ? context->parameter_string : "");
463 memset (seen, 0, sizeof (gboolean) * 256);
464 shadow_map = g_hash_table_new (g_str_hash, g_str_equal);
466 if (context->main_group)
468 for (i = 0; i < context->main_group->n_entries; i++)
470 entry = &context->main_group->entries[i];
471 g_hash_table_insert (shadow_map,
472 (gpointer)entry->long_name,
475 if (seen[(guchar)entry->short_name])
476 entry->short_name = 0;
478 seen[(guchar)entry->short_name] = TRUE;
482 list = context->groups;
485 GOptionGroup *group = list->data;
486 for (i = 0; i < group->n_entries; i++)
488 entry = &group->entries[i];
489 if (g_hash_table_lookup (shadow_map, entry->long_name))
490 entry->long_name = g_strdup_printf ("%s-%s", group->name, entry->long_name);
492 g_hash_table_insert (shadow_map, (gpointer)entry->long_name, entry);
494 if (seen[(guchar)entry->short_name])
495 entry->short_name = 0;
497 seen[(guchar)entry->short_name] = TRUE;
502 g_hash_table_destroy (shadow_map);
504 list = context->groups;
506 max_length = g_utf8_strlen ("-?, --help", -1);
510 len = g_utf8_strlen ("--help-all", -1);
511 max_length = MAX (max_length, len);
514 if (context->main_group)
516 len = calculate_max_length (context->main_group);
517 max_length = MAX (max_length, len);
522 GOptionGroup *group = list->data;
524 /* First, we check the --help-<groupname> options */
525 len = g_utf8_strlen ("--help-", -1) + g_utf8_strlen (group->name, -1);
526 max_length = MAX (max_length, len);
528 /* Then we go through the entries */
529 len = calculate_max_length (group);
530 max_length = MAX (max_length, len);
535 /* Add a bit of padding */
540 list = context->groups;
542 g_print ("%s\n -%c, --%-*s %s\n",
543 _("Help Options:"), '?', max_length - 4, "help",
544 _("Show help options"));
546 /* We only want --help-all when there are groups */
548 g_print (" --%-*s %s\n", max_length, "help-all",
549 _("Show all help options"));
553 GOptionGroup *group = list->data;
555 g_print (" --help-%-*s %s\n", max_length - 5, group->name,
556 TRANSLATE (group, group->help_description));
566 /* Print a certain group */
568 g_print ("%s\n", TRANSLATE (group, group->description));
569 for (i = 0; i < group->n_entries; i++)
570 print_entry (group, max_length, &group->entries[i]);
575 /* Print all groups */
577 list = context->groups;
581 GOptionGroup *group = list->data;
583 g_print ("%s\n", group->description);
585 for (i = 0; i < group->n_entries; i++)
586 if (!(group->entries[i].flags & G_OPTION_FLAG_IN_MAIN))
587 print_entry (group, max_length, &group->entries[i]);
594 /* Print application options if --help or --help-all has been specified */
595 if (main_help || !group)
597 list = context->groups;
599 g_print ("%s\n", _("Application Options:"));
601 if (context->main_group)
602 for (i = 0; i < context->main_group->n_entries; i++)
603 print_entry (context->main_group, max_length,
604 &context->main_group->entries[i]);
608 GOptionGroup *group = list->data;
610 /* Print main entries from other groups */
611 for (i = 0; i < group->n_entries; i++)
612 if (group->entries[i].flags & G_OPTION_FLAG_IN_MAIN)
613 print_entry (group, max_length, &group->entries[i]);
625 parse_int (const gchar *arg_name,
631 glong tmp = strtol (arg, &end, 0);
635 if (*arg == '\0' || *end != '\0')
638 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
639 _("Cannot parse integer value '%s' for %s"),
645 if (*result != tmp || errno == ERANGE)
648 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
649 _("Integer value '%s' for %s out of range"),
658 get_change (GOptionContext *context,
663 Change *change = NULL;
665 for (list = context->changes; list != NULL; list = list->next)
669 if (change->arg_data == arg_data)
673 change = g_new0 (Change, 1);
674 change->arg_type = arg_type;
675 change->arg_data = arg_data;
677 context->changes = g_list_prepend (context->changes, change);
685 add_pending_null (GOptionContext *context,
691 n = g_new0 (PendingNull, 1);
695 context->pending_nulls = g_list_prepend (context->pending_nulls, n);
699 parse_arg (GOptionContext *context,
703 const gchar *option_name,
711 case G_OPTION_ARG_NONE:
713 change = get_change (context, G_OPTION_ARG_NONE,
716 *(gboolean *)entry->arg_data = !(entry->flags & G_OPTION_FLAG_REVERSE);
719 case G_OPTION_ARG_STRING:
723 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
728 change = get_change (context, G_OPTION_ARG_STRING,
730 g_free (change->allocated.str);
732 change->prev.str = *(gchar **)entry->arg_data;
733 change->allocated.str = data;
735 *(gchar **)entry->arg_data = data;
738 case G_OPTION_ARG_STRING_ARRAY:
742 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
747 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
750 if (change->allocated.array.len == 0)
752 change->prev.array = entry->arg_data;
753 change->allocated.array.data = g_new (gchar *, 2);
756 change->allocated.array.data =
757 g_renew (gchar *, change->allocated.array.data,
758 change->allocated.array.len + 2);
760 change->allocated.array.data[change->allocated.array.len] = data;
761 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
763 change->allocated.array.len ++;
765 *(gchar ***)entry->arg_data = change->allocated.array.data;
770 case G_OPTION_ARG_FILENAME:
775 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
780 data = g_strdup (value);
782 change = get_change (context, G_OPTION_ARG_FILENAME,
784 g_free (change->allocated.str);
786 change->prev.str = *(gchar **)entry->arg_data;
787 change->allocated.str = data;
789 *(gchar **)entry->arg_data = data;
793 case G_OPTION_ARG_FILENAME_ARRAY:
798 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
803 data = g_strdup (value);
805 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
808 if (change->allocated.array.len == 0)
810 change->prev.array = entry->arg_data;
811 change->allocated.array.data = g_new (gchar *, 2);
814 change->allocated.array.data =
815 g_renew (gchar *, change->allocated.array.data,
816 change->allocated.array.len + 2);
818 change->allocated.array.data[change->allocated.array.len] = data;
819 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
821 change->allocated.array.len ++;
823 *(gchar ***)entry->arg_data = change->allocated.array.data;
828 case G_OPTION_ARG_INT:
832 if (!parse_int (option_name, value,
837 change = get_change (context, G_OPTION_ARG_INT,
839 change->prev.integer = *(gint *)entry->arg_data;
840 *(gint *)entry->arg_data = data;
843 case G_OPTION_ARG_CALLBACK:
848 tmp = g_locale_to_utf8 (value, -1, NULL, NULL, error);
853 retval = (* (GOptionArgFunc) entry->arg_data) (option_name, tmp, group->user_data, error);
862 g_assert_not_reached ();
869 parse_short_option (GOptionContext *context,
881 for (j = 0; j < group->n_entries; j++)
883 if (arg == group->entries[j].short_name)
885 if (group->entries[j].arg == G_OPTION_ARG_NONE)
887 parse_arg (context, group, &group->entries[j],
896 if (*new_index > index)
898 g_warning ("FIXME: figure out the correct error here");
903 option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
905 if (index < *argc - 1)
907 value = (*argv)[index + 1];
908 add_pending_null (context, &((*argv)[index + 1]), NULL);
909 *new_index = index + 1;
914 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
915 _("Missing argument for %s"), option_name);
916 g_free (option_name);
920 if (!parse_arg (context, group, &group->entries[j], value, option_name, error))
922 g_free (option_name);
926 g_free (option_name);
936 parse_long_option (GOptionContext *context,
947 for (j = 0; j < group->n_entries; j++)
952 if (group->entries[j].arg == G_OPTION_ARG_NONE &&
953 strcmp (arg, group->entries[j].long_name) == 0)
955 parse_arg (context, group, &group->entries[j],
958 add_pending_null (context, &((*argv)[*index]), NULL);
963 gint len = strlen (group->entries[j].long_name);
965 if (strncmp (arg, group->entries[j].long_name, len) == 0 &&
966 (arg[len] == '=' || arg[len] == 0))
971 add_pending_null (context, &((*argv)[*index]), NULL);
972 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
975 value = arg + len + 1;
976 else if (*index < *argc - 1)
978 value = (*argv)[*index + 1];
979 add_pending_null (context, &((*argv)[*index + 1]), NULL);
985 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
986 _("Missing argument for %s"), option_name);
987 g_free (option_name);
991 if (!parse_arg (context, group, &group->entries[j], value, option_name, error))
993 g_free (option_name);
997 g_free (option_name);
1007 parse_remaining_arg (GOptionContext *context,
1008 GOptionGroup *group,
1017 for (j = 0; j < group->n_entries; j++)
1019 if (*index >= *argc)
1022 if (group->entries[j].long_name[0])
1025 g_return_val_if_fail (group->entries[j].arg == G_OPTION_ARG_STRING_ARRAY ||
1026 group->entries[j].arg == G_OPTION_ARG_FILENAME_ARRAY, FALSE);
1028 add_pending_null (context, &((*argv)[*index]), NULL);
1030 if (!parse_arg (context, group, &group->entries[j], (*argv)[*index], "", error))
1041 free_changes_list (GOptionContext *context,
1046 for (list = context->changes; list != NULL; list = list->next)
1048 Change *change = list->data;
1052 switch (change->arg_type)
1054 case G_OPTION_ARG_NONE:
1055 *(gboolean *)change->arg_data = change->prev.bool;
1057 case G_OPTION_ARG_INT:
1058 *(gint *)change->arg_data = change->prev.integer;
1060 case G_OPTION_ARG_STRING:
1061 case G_OPTION_ARG_FILENAME:
1062 g_free (change->allocated.str);
1063 *(gchar **)change->arg_data = change->prev.str;
1065 case G_OPTION_ARG_STRING_ARRAY:
1066 case G_OPTION_ARG_FILENAME_ARRAY:
1067 g_strfreev (change->allocated.array.data);
1068 *(gchar ***)change->arg_data = change->prev.array;
1071 g_assert_not_reached ();
1078 g_list_free (context->changes);
1079 context->changes = NULL;
1083 free_pending_nulls (GOptionContext *context,
1084 gboolean perform_nulls)
1088 for (list = context->pending_nulls; list != NULL; list = list->next)
1090 PendingNull *n = list->data;
1096 /* Copy back the short options */
1098 strcpy (*n->ptr + 1, n->value);
1108 g_list_free (context->pending_nulls);
1109 context->pending_nulls = NULL;
1113 * g_option_context_parse:
1114 * @context: a #GOptionContext
1115 * @argc: a pointer to the number of command line arguments.
1116 * @argv: a pointer to the array of command line arguments.
1117 * @error: a return location for errors
1119 * Parses the command line arguments, recognizing options
1120 * which have been added to @context. A side-effect of
1121 * calling this function is that g_set_prgname() will be
1124 * If the parsing is successful, any parsed arguments are
1125 * removed from the array and @argc and @argv are updated
1126 * accordingly. A '--' option is stripped from @argv
1127 * unless there are unparsed options before and after it,
1128 * or some of the options after it start with '-'. In case
1129 * of an error, @argc and @argv are left unmodified.
1131 * If automatic <option>--help</option> support is enabled
1132 * (see g_option_context_set_help_enabled()), and the
1133 * @argv array contains one of the recognized help options,
1134 * this function will produce help output to stdout and
1135 * call <literal>exit (0)</literal>.
1137 * Return value: %TRUE if the parsing was successful,
1138 * %FALSE if an error occurred
1143 g_option_context_parse (GOptionContext *context,
1151 /* Set program name */
1152 if (argc && argv && *argc)
1156 prgname = g_path_get_basename ((*argv)[0]);
1157 g_set_prgname (prgname);
1162 g_set_prgname ("<unknown>");
1165 /* Call pre-parse hooks */
1166 list = context->groups;
1169 GOptionGroup *group = list->data;
1171 if (group->pre_parse_func)
1173 if (!(* group->pre_parse_func) (context, group,
1174 group->user_data, error))
1181 if (context->main_group && context->main_group->pre_parse_func)
1183 if (!(* context->main_group->pre_parse_func) (context, context->main_group,
1184 context->main_group->user_data, error))
1190 gboolean stop_parsing = FALSE;
1191 gboolean has_unknown = FALSE;
1192 gint separator_pos = 0;
1194 for (i = 1; i < *argc; i++)
1197 gboolean parsed = FALSE;
1199 if ((*argv)[i][0] == '-' && (*argv)[i][1] != '\0' && !stop_parsing)
1201 if ((*argv)[i][1] == '-')
1205 arg = (*argv)[i] + 2;
1207 /* '--' terminates list of arguments */
1211 stop_parsing = TRUE;
1215 /* Handle help options */
1216 if (context->help_enabled)
1218 if (strcmp (arg, "help") == 0)
1219 print_help (context, TRUE, NULL);
1220 else if (strcmp (arg, "help-all") == 0)
1221 print_help (context, FALSE, NULL);
1222 else if (strncmp (arg, "help-", 5) == 0)
1226 list = context->groups;
1230 GOptionGroup *group = list->data;
1232 if (strcmp (arg + 5, group->name) == 0)
1233 print_help (context, FALSE, group);
1240 if (context->main_group &&
1241 !parse_long_option (context, context->main_group, &i, arg,
1242 argc, argv, error, &parsed))
1248 /* Try the groups */
1249 list = context->groups;
1252 GOptionGroup *group = list->data;
1254 if (!parse_long_option (context, group, &i, arg,
1255 argc, argv, error, &parsed))
1267 /* Now look for --<group>-<option> */
1268 dash = strchr (arg, '-');
1271 /* Try the groups */
1272 list = context->groups;
1275 GOptionGroup *group = list->data;
1277 if (strncmp (group->name, arg, dash - arg) == 0)
1279 if (!parse_long_option (context, group, &i, dash + 1,
1280 argc, argv, error, &parsed))
1291 if (context->ignore_unknown)
1299 gboolean *nulled_out = NULL;
1301 arg = (*argv)[i] + 1;
1305 if (context->ignore_unknown)
1306 nulled_out = g_new0 (gboolean, strlen (arg));
1308 for (j = 0; j < strlen (arg); j++)
1310 if (context->help_enabled && arg[j] == '?')
1311 print_help (context, TRUE, NULL);
1315 if (context->main_group &&
1316 !parse_short_option (context, context->main_group,
1318 argc, argv, error, &parsed))
1321 g_free (nulled_out);
1327 /* Try the groups */
1328 list = context->groups;
1331 GOptionGroup *group = list->data;
1333 if (!parse_short_option (context, group, i, &new_i, arg[j],
1334 argc, argv, error, &parsed))
1344 if (context->ignore_unknown)
1347 nulled_out[j] = TRUE;
1356 if (context->ignore_unknown)
1358 gchar *new_arg = NULL;
1361 for (j = 0; j < strlen (arg); j++)
1366 new_arg = g_malloc (strlen (arg) + 1);
1367 new_arg[arg_index++] = arg[j];
1371 new_arg[arg_index] = '\0';
1373 add_pending_null (context, &((*argv)[i]), new_arg);
1377 add_pending_null (context, &((*argv)[i]), NULL);
1385 if (!parsed && !context->ignore_unknown)
1388 G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
1389 _("Unknown option %s"), (*argv)[i]);
1395 /* Collect remaining args */
1396 if (context->main_group &&
1397 !parse_remaining_arg (context, context->main_group, &i,
1398 argc, argv, error, &parsed))
1401 if (!parsed && (has_unknown || (*argv)[i][0] == '-'))
1406 if (separator_pos > 0)
1407 add_pending_null (context, &((*argv)[separator_pos]), NULL);
1411 /* Call post-parse hooks */
1412 list = context->groups;
1415 GOptionGroup *group = list->data;
1417 if (group->post_parse_func)
1419 if (!(* group->post_parse_func) (context, group,
1420 group->user_data, error))
1427 if (context->main_group && context->main_group->post_parse_func)
1429 if (!(* context->main_group->post_parse_func) (context, context->main_group,
1430 context->main_group->user_data, error))
1436 free_pending_nulls (context, TRUE);
1438 for (i = 1; i < *argc; i++)
1440 for (k = i; k < *argc; k++)
1441 if ((*argv)[k] != NULL)
1447 for (j = i + k; j < *argc; j++)
1449 (*argv)[j-k] = (*argv)[j];
1461 /* Call error hooks */
1462 list = context->groups;
1465 GOptionGroup *group = list->data;
1467 if (group->error_func)
1468 (* group->error_func) (context, group,
1469 group->user_data, error);
1474 if (context->main_group && context->main_group->error_func)
1475 (* context->main_group->error_func) (context, context->main_group,
1476 context->main_group->user_data, error);
1478 free_changes_list (context, TRUE);
1479 free_pending_nulls (context, FALSE);
1485 * g_option_group_new:
1486 * @name: the name for the option group, this is used to provide
1487 * help for the options in this group with <option>--help-</option>@name
1488 * @description: a description for this group to be shown in
1489 * <option>--help</option>. This string is translated using the translation
1490 * domain or translation function of the group
1491 * @help_description: a description for the <option>--help-</option>@name option.
1492 * This string is translated using the translation domain or translation function
1494 * @user_data: user data that will be passed to the pre- and post-parse hooks,
1495 * the error hook and to callbacks of %G_OPTION_ARG_CALLBACK options, or %NULL
1496 * @destroy: a function that will be called to free @user_data, or %NULL
1498 * Creates a new #GOptionGroup.
1500 * Return value: a newly created option group. It should be added
1501 * to a #GOptionContext or freed with g_option_group_free().
1506 g_option_group_new (const gchar *name,
1507 const gchar *description,
1508 const gchar *help_description,
1510 GDestroyNotify destroy)
1513 GOptionGroup *group;
1515 group = g_new0 (GOptionGroup, 1);
1516 group->name = g_strdup (name);
1517 group->description = g_strdup (description);
1518 group->help_description = g_strdup (help_description);
1519 group->user_data = user_data;
1520 group->destroy_notify = destroy;
1527 * g_option_group_free:
1528 * @group: a #GOptionGroup
1530 * Frees a #GOptionGroup. Note that you must <emphasis>not</emphasis>
1531 * free groups which have been added to a #GOptionContext.
1536 g_option_group_free (GOptionGroup *group)
1538 g_return_if_fail (group != NULL);
1540 g_free (group->name);
1541 g_free (group->description);
1542 g_free (group->help_description);
1544 g_free (group->entries);
1546 if (group->destroy_notify)
1547 (* group->destroy_notify) (group->user_data);
1549 if (group->translate_notify)
1550 (* group->translate_notify) (group->translate_data);
1557 * g_option_group_add_entries:
1558 * @group: a #GOptionGroup
1559 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
1561 * Adds the options specified in @entries to @group.
1566 g_option_group_add_entries (GOptionGroup *group,
1567 const GOptionEntry *entries)
1571 g_return_if_fail (entries != NULL);
1573 for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++);
1575 group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
1577 memcpy (group->entries + group->n_entries, entries, sizeof (GOptionEntry) * n_entries);
1579 group->n_entries += n_entries;
1583 * g_option_group_set_parse_hooks:
1584 * @group: a #GOptionGroup
1585 * @pre_parse_func: a function to call before parsing, or %NULL
1586 * @post_parse_func: a function to call after parsing, or %NULL
1588 * Associates two functions with @group which will be called
1589 * from g_option_context_parse() before the first option is parsed
1590 * and after the last option has been parsed, respectively.
1592 * Note that the user data to be passed to @pre_parse_func and
1593 * @post_parse_func can be specified when constructing the group
1594 * with g_option_group_new().
1599 g_option_group_set_parse_hooks (GOptionGroup *group,
1600 GOptionParseFunc pre_parse_func,
1601 GOptionParseFunc post_parse_func)
1603 g_return_if_fail (group != NULL);
1605 group->pre_parse_func = pre_parse_func;
1606 group->post_parse_func = post_parse_func;
1610 * g_option_group_set_error_hook:
1611 * @group: a #GOptionGroup
1612 * @error_func: a function to call when an error occurs
1614 * Associates a function with @group which will be called
1615 * from g_option_context_parse() when an error occurs.
1617 * Note that the user data to be passed to @pre_parse_func and
1618 * @post_parse_func can be specified when constructing the group
1619 * with g_option_group_new().
1624 g_option_group_set_error_hook (GOptionGroup *group,
1625 GOptionErrorFunc error_func)
1627 g_return_if_fail (group != NULL);
1629 group->error_func = error_func;
1634 * g_option_group_set_translate_func:
1635 * @group: a #GOptionGroup
1636 * @func: the #GTranslateFunc, or %NULL
1637 * @data: user data to pass to @func, or %NULL
1638 * @destroy_notify: a function which gets called to free @data, or %NULL
1640 * Sets the function which is used to translate user-visible
1641 * strings, for <option>--help</option> output. Different
1642 * groups can use different #GTranslateFunc<!-- -->s. If @func
1643 * is %NULL, strings are not translated.
1645 * If you are using gettext(), you only need to set the translation
1646 * domain, see g_option_group_set_translation_domain().
1651 g_option_group_set_translate_func (GOptionGroup *group,
1652 GTranslateFunc func,
1654 GDestroyNotify destroy_notify)
1656 g_return_if_fail (group != NULL);
1658 if (group->translate_notify)
1659 group->translate_notify (group->translate_data);
1661 group->translate_func = func;
1662 group->translate_data = data;
1663 group->translate_notify = destroy_notify;
1667 dgettext_swapped (const gchar *msgid,
1668 const gchar *domainname)
1670 return dgettext (domainname, msgid);
1674 * g_option_group_set_translation_domain:
1675 * @group: a #GOptionGroup
1676 * @domain: the domain to use
1678 * A convenience function to use gettext() for translating
1679 * user-visible strings.
1684 g_option_group_set_translation_domain (GOptionGroup *group,
1685 const gchar *domain)
1687 g_return_if_fail (group != NULL);
1689 g_option_group_set_translate_func (group,
1690 (GTranslateFunc)dgettext_swapped,
1695 #define __G_OPTION_C__
1696 #include "galiasdef.c"