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)))
64 struct _GOptionContext
68 gchar *parameter_string;
70 gboolean help_enabled;
71 gboolean ignore_unknown;
73 GOptionGroup *main_group;
75 /* We keep a list of change so we can revert them */
78 /* We also keep track of all argv elements that should be NULLed or
88 gchar *help_description;
90 GDestroyNotify destroy_notify;
93 GTranslateFunc translate_func;
94 GDestroyNotify translate_notify;
95 gpointer translate_data;
97 GOptionEntry *entries;
100 GOptionParseFunc pre_parse_func;
101 GOptionParseFunc post_parse_func;
102 GOptionErrorFunc error_func;
105 static void free_changes_list (GOptionContext *context,
107 static void free_pending_nulls (GOptionContext *context,
108 gboolean perform_nulls);
111 g_option_error_quark (void)
116 q = g_quark_from_static_string ("g-option-context-error-quark");
122 * g_option_context_new:
123 * @parameter_string: a string which is displayed in
124 * the first line of <option>--help</option> output, after
125 * <literal><replaceable>programname</replaceable> [OPTION...]</literal>
127 * Creates a new option context.
129 * Returns: a newly created #GOptionContext, which must be
130 * freed with g_option_context_free() after use.
135 g_option_context_new (const gchar *parameter_string)
138 GOptionContext *context;
140 context = g_new0 (GOptionContext, 1);
142 context->parameter_string = g_strdup (parameter_string);
143 context->help_enabled = TRUE;
144 context->ignore_unknown = FALSE;
150 * g_option_context_free:
151 * @context: a #GOptionContext
153 * Frees context and all the groups which have been
158 void g_option_context_free (GOptionContext *context)
160 g_return_if_fail (context != NULL);
162 g_list_foreach (context->groups, (GFunc)g_option_group_free, NULL);
163 g_list_free (context->groups);
165 if (context->main_group)
166 g_option_group_free (context->main_group);
168 free_changes_list (context, FALSE);
169 free_pending_nulls (context, FALSE);
171 g_free (context->parameter_string);
178 * g_option_context_set_help_enabled:
179 * @context: a #GOptionContext
180 * @help_enabled: %TRUE to enable <option>--help</option>, %FALSE to disable it
182 * Enables or disables automatic generation of <option>--help</option>
183 * output. By default, g_option_context_parse() recognizes
184 * <option>--help</option>, <option>-?</option>, <option>--help-all</option>
185 * and <option>--help-</option><replaceable>groupname</replaceable> and creates
186 * suitable output to stdout.
190 void g_option_context_set_help_enabled (GOptionContext *context,
191 gboolean help_enabled)
194 g_return_if_fail (context != NULL);
196 context->help_enabled = help_enabled;
200 * g_option_context_get_help_enabled:
201 * @context: a #GOptionContext
203 * Returns whether automatic <option>--help</option> generation
204 * is turned on for @context. See g_option_context_set_help_enabled().
206 * Returns: %TRUE if automatic help generation is turned on.
211 g_option_context_get_help_enabled (GOptionContext *context)
213 g_return_val_if_fail (context != NULL, FALSE);
215 return context->help_enabled;
219 * g_option_context_set_ignore_unknown_options:
220 * @context: a #GOptionContext
221 * @ignore_unknown: %TRUE to ignore unknown options, %FALSE to produce
222 * an error when unknown options are met
224 * Sets whether to ignore unknown options or not. If an argument is
225 * ignored, it is left in the @argv array after parsing. By default,
226 * g_option_context_parse() treats unknown options as error.
228 * This setting does not affect non-option arguments (i.e. arguments
229 * which don't start with a dash). But note that GOption cannot reliably
230 * determine whether a non-option belongs to a preceding unknown option.
235 g_option_context_set_ignore_unknown_options (GOptionContext *context,
236 gboolean ignore_unknown)
238 g_return_if_fail (context != NULL);
240 context->ignore_unknown = ignore_unknown;
244 * g_option_context_get_ignore_unknown_options:
245 * @context: a #GOptionContext
247 * Returns whether unknown options are ignored or not. See
248 * g_option_context_set_ignore_unknown_options().
250 * Returns: %TRUE if unknown options are ignored.
255 g_option_context_get_ignore_unknown_options (GOptionContext *context)
257 g_return_val_if_fail (context != NULL, FALSE);
259 return context->ignore_unknown;
263 * g_option_context_add_group:
264 * @context: a #GOptionContext
265 * @group: the group to add
267 * Adds a #GOptionGroup to the @context, so that parsing with @context
268 * will recognize the options in the group. Note that the group will
269 * be freed together with the context when g_option_context_free() is
270 * called, so you must not free the group yourself after adding it
276 g_option_context_add_group (GOptionContext *context,
281 g_return_if_fail (context != NULL);
282 g_return_if_fail (group != NULL);
283 g_return_if_fail (group->name != NULL);
284 g_return_if_fail (group->description != NULL);
285 g_return_if_fail (group->help_description != NULL);
287 for (list = context->groups; list; list = list->next)
289 GOptionGroup *g = (GOptionGroup *)list->data;
291 if ((group->name == NULL && g->name == NULL) ||
292 (group->name && g->name && strcmp (group->name, g->name) == 0))
293 g_warning ("A group named \"%s\" is already part of this GOptionContext",
297 context->groups = g_list_append (context->groups, group);
301 * g_option_context_set_main_group:
302 * @context: a #GOptionContext
303 * @group: the group to set as main group
305 * Sets a #GOptionGroup as main group of the @context.
306 * This has the same effect as calling g_option_context_add_group(),
307 * the only difference is that the options in the main group are
308 * treated differently when generating <option>--help</option> output.
313 g_option_context_set_main_group (GOptionContext *context,
316 g_return_if_fail (context != NULL);
317 g_return_if_fail (group != NULL);
319 if (context->main_group)
321 g_warning ("This GOptionContext already has a main group");
326 context->main_group = group;
330 * g_option_context_get_main_group:
331 * @context: a #GOptionContext
333 * Returns a pointer to the main group of @context.
335 * Return value: the main group of @context, or %NULL if @context doesn't
336 * have a main group. Note that group belongs to @context and should
337 * not be modified or freed.
342 g_option_context_get_main_group (GOptionContext *context)
344 g_return_val_if_fail (context != NULL, NULL);
346 return context->main_group;
350 * g_option_context_add_main_entries:
351 * @context: a #GOptionContext
352 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
353 * @translation_domain: a translation domain to use for translating
354 * the <option>--help</option> output for the options in @entries
355 * with gettext(), or %NULL
357 * A convenience function which creates a main group if it doesn't
358 * exist, adds the @entries to it and sets the translation domain.
363 g_option_context_add_main_entries (GOptionContext *context,
364 const GOptionEntry *entries,
365 const gchar *translation_domain)
367 g_return_if_fail (entries != NULL);
369 if (!context->main_group)
370 context->main_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
372 g_option_group_add_entries (context->main_group, entries);
373 g_option_group_set_translation_domain (context->main_group, translation_domain);
377 calculate_max_length (GOptionGroup *group)
380 gint i, len, max_length;
384 for (i = 0; i < group->n_entries; i++)
386 entry = &group->entries[i];
388 if (entry->flags & G_OPTION_FLAG_HIDDEN)
391 len = g_utf8_strlen (entry->long_name, -1);
393 if (entry->short_name)
396 if (entry->arg != G_OPTION_ARG_NONE && entry->arg_description)
397 len += 1 + g_utf8_strlen (TRANSLATE (group, entry->arg_description), -1);
399 max_length = MAX (max_length, len);
406 print_entry (GOptionGroup *group,
408 const GOptionEntry *entry)
412 if (entry->flags & G_OPTION_FLAG_HIDDEN)
415 if (entry->long_name[0] == 0)
418 str = g_string_new (NULL);
420 if (entry->short_name)
421 g_string_append_printf (str, " -%c, --%s", entry->short_name, entry->long_name);
423 g_string_append_printf (str, " --%s", entry->long_name);
425 if (entry->arg_description)
426 g_string_append_printf (str, "=%s", TRANSLATE (group, entry->arg_description));
428 g_print ("%-*s %s\n", max_length + 4, str->str,
429 entry->description ? TRANSLATE (group, entry->description) : "");
430 g_string_free (str, TRUE);
434 print_help (GOptionContext *context,
439 gint max_length, len;
442 GHashTable *shadow_map;
444 const gchar *rest_description;
446 rest_description = NULL;
447 if (context->main_group)
449 for (i = 0; i < context->main_group->n_entries; i++)
451 entry = &context->main_group->entries[i];
452 if (entry->long_name[0] == 0)
454 rest_description = entry->arg_description;
460 g_print ("%s\n %s %s%s%s%s%s\n\n",
461 _("Usage:"), g_get_prgname(), _("[OPTION...]"),
462 rest_description ? " " : "",
463 rest_description ? rest_description : "",
464 context->parameter_string ? " " : "",
465 context->parameter_string ? context->parameter_string : "");
467 memset (seen, 0, sizeof (gboolean) * 256);
468 shadow_map = g_hash_table_new (g_str_hash, g_str_equal);
470 if (context->main_group)
472 for (i = 0; i < context->main_group->n_entries; i++)
474 entry = &context->main_group->entries[i];
475 g_hash_table_insert (shadow_map,
476 (gpointer)entry->long_name,
479 if (seen[(guchar)entry->short_name])
480 entry->short_name = 0;
482 seen[(guchar)entry->short_name] = TRUE;
486 list = context->groups;
489 GOptionGroup *group = list->data;
490 for (i = 0; i < group->n_entries; i++)
492 entry = &group->entries[i];
493 if (g_hash_table_lookup (shadow_map, entry->long_name))
494 entry->long_name = g_strdup_printf ("%s-%s", group->name, entry->long_name);
496 g_hash_table_insert (shadow_map, (gpointer)entry->long_name, entry);
498 if (seen[(guchar)entry->short_name])
499 entry->short_name = 0;
501 seen[(guchar)entry->short_name] = TRUE;
506 g_hash_table_destroy (shadow_map);
508 list = context->groups;
510 max_length = g_utf8_strlen ("-?, --help", -1);
514 len = g_utf8_strlen ("--help-all", -1);
515 max_length = MAX (max_length, len);
518 if (context->main_group)
520 len = calculate_max_length (context->main_group);
521 max_length = MAX (max_length, len);
526 GOptionGroup *group = list->data;
528 /* First, we check the --help-<groupname> options */
529 len = g_utf8_strlen ("--help-", -1) + g_utf8_strlen (group->name, -1);
530 max_length = MAX (max_length, len);
532 /* Then we go through the entries */
533 len = calculate_max_length (group);
534 max_length = MAX (max_length, len);
539 /* Add a bit of padding */
544 list = context->groups;
546 g_print ("%s\n -%c, --%-*s %s\n",
547 _("Help Options:"), '?', max_length - 4, "help",
548 _("Show help options"));
550 /* We only want --help-all when there are groups */
552 g_print (" --%-*s %s\n", max_length, "help-all",
553 _("Show all help options"));
557 GOptionGroup *group = list->data;
559 g_print (" --help-%-*s %s\n", max_length - 5, group->name,
560 TRANSLATE (group, group->help_description));
570 /* Print a certain group */
572 g_print ("%s\n", TRANSLATE (group, group->description));
573 for (i = 0; i < group->n_entries; i++)
574 print_entry (group, max_length, &group->entries[i]);
579 /* Print all groups */
581 list = context->groups;
585 GOptionGroup *group = list->data;
587 g_print ("%s\n", group->description);
589 for (i = 0; i < group->n_entries; i++)
590 if (!(group->entries[i].flags & G_OPTION_FLAG_IN_MAIN))
591 print_entry (group, max_length, &group->entries[i]);
598 /* Print application options if --help or --help-all has been specified */
599 if (main_help || !group)
601 list = context->groups;
603 g_print ("%s\n", _("Application Options:"));
605 if (context->main_group)
606 for (i = 0; i < context->main_group->n_entries; i++)
607 print_entry (context->main_group, max_length,
608 &context->main_group->entries[i]);
612 GOptionGroup *group = list->data;
614 /* Print main entries from other groups */
615 for (i = 0; i < group->n_entries; i++)
616 if (group->entries[i].flags & G_OPTION_FLAG_IN_MAIN)
617 print_entry (group, max_length, &group->entries[i]);
629 parse_int (const gchar *arg_name,
638 tmp = strtol (arg, &end, 0);
640 if (*arg == '\0' || *end != '\0')
643 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
644 _("Cannot parse integer value '%s' for %s"),
650 if (*result != tmp || errno == ERANGE)
653 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
654 _("Integer value '%s' for %s out of range"),
663 get_change (GOptionContext *context,
668 Change *change = NULL;
670 for (list = context->changes; list != NULL; list = list->next)
674 if (change->arg_data == arg_data)
678 change = g_new0 (Change, 1);
679 change->arg_type = arg_type;
680 change->arg_data = arg_data;
682 context->changes = g_list_prepend (context->changes, change);
690 add_pending_null (GOptionContext *context,
696 n = g_new0 (PendingNull, 1);
700 context->pending_nulls = g_list_prepend (context->pending_nulls, n);
704 parse_arg (GOptionContext *context,
708 const gchar *option_name,
716 case G_OPTION_ARG_NONE:
718 change = get_change (context, G_OPTION_ARG_NONE,
721 *(gboolean *)entry->arg_data = !(entry->flags & G_OPTION_FLAG_REVERSE);
724 case G_OPTION_ARG_STRING:
728 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
733 change = get_change (context, G_OPTION_ARG_STRING,
735 g_free (change->allocated.str);
737 change->prev.str = *(gchar **)entry->arg_data;
738 change->allocated.str = data;
740 *(gchar **)entry->arg_data = data;
743 case G_OPTION_ARG_STRING_ARRAY:
747 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
752 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
755 if (change->allocated.array.len == 0)
757 change->prev.array = entry->arg_data;
758 change->allocated.array.data = g_new (gchar *, 2);
761 change->allocated.array.data =
762 g_renew (gchar *, change->allocated.array.data,
763 change->allocated.array.len + 2);
765 change->allocated.array.data[change->allocated.array.len] = data;
766 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
768 change->allocated.array.len ++;
770 *(gchar ***)entry->arg_data = change->allocated.array.data;
775 case G_OPTION_ARG_FILENAME:
780 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
785 data = g_strdup (value);
787 change = get_change (context, G_OPTION_ARG_FILENAME,
789 g_free (change->allocated.str);
791 change->prev.str = *(gchar **)entry->arg_data;
792 change->allocated.str = data;
794 *(gchar **)entry->arg_data = data;
798 case G_OPTION_ARG_FILENAME_ARRAY:
803 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
808 data = g_strdup (value);
810 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
813 if (change->allocated.array.len == 0)
815 change->prev.array = entry->arg_data;
816 change->allocated.array.data = g_new (gchar *, 2);
819 change->allocated.array.data =
820 g_renew (gchar *, change->allocated.array.data,
821 change->allocated.array.len + 2);
823 change->allocated.array.data[change->allocated.array.len] = data;
824 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
826 change->allocated.array.len ++;
828 *(gchar ***)entry->arg_data = change->allocated.array.data;
833 case G_OPTION_ARG_INT:
837 if (!parse_int (option_name, value,
842 change = get_change (context, G_OPTION_ARG_INT,
844 change->prev.integer = *(gint *)entry->arg_data;
845 *(gint *)entry->arg_data = data;
848 case G_OPTION_ARG_CALLBACK:
853 tmp = g_locale_to_utf8 (value, -1, NULL, NULL, error);
858 retval = (* (GOptionArgFunc) entry->arg_data) (option_name, tmp, group->user_data, error);
867 g_assert_not_reached ();
874 parse_short_option (GOptionContext *context,
886 for (j = 0; j < group->n_entries; j++)
888 if (arg == group->entries[j].short_name)
890 if (group->entries[j].arg == G_OPTION_ARG_NONE)
892 parse_arg (context, group, &group->entries[j],
901 if (*new_index > index)
903 g_warning ("FIXME: figure out the correct error here");
908 option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
910 if (index < *argc - 1)
912 value = (*argv)[index + 1];
913 add_pending_null (context, &((*argv)[index + 1]), NULL);
914 *new_index = index + 1;
919 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
920 _("Missing argument for %s"), option_name);
921 g_free (option_name);
925 if (!parse_arg (context, group, &group->entries[j], value, option_name, error))
927 g_free (option_name);
931 g_free (option_name);
941 parse_long_option (GOptionContext *context,
952 for (j = 0; j < group->n_entries; j++)
957 if (group->entries[j].arg == G_OPTION_ARG_NONE &&
958 strcmp (arg, group->entries[j].long_name) == 0)
960 parse_arg (context, group, &group->entries[j],
963 add_pending_null (context, &((*argv)[*index]), NULL);
968 gint len = strlen (group->entries[j].long_name);
970 if (strncmp (arg, group->entries[j].long_name, len) == 0 &&
971 (arg[len] == '=' || arg[len] == 0))
976 add_pending_null (context, &((*argv)[*index]), NULL);
977 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
980 value = arg + len + 1;
981 else if (*index < *argc - 1)
983 value = (*argv)[*index + 1];
984 add_pending_null (context, &((*argv)[*index + 1]), NULL);
990 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
991 _("Missing argument for %s"), option_name);
992 g_free (option_name);
996 if (!parse_arg (context, group, &group->entries[j], value, option_name, error))
998 g_free (option_name);
1002 g_free (option_name);
1012 parse_remaining_arg (GOptionContext *context,
1013 GOptionGroup *group,
1022 for (j = 0; j < group->n_entries; j++)
1024 if (*index >= *argc)
1027 if (group->entries[j].long_name[0])
1030 g_return_val_if_fail (group->entries[j].arg == G_OPTION_ARG_STRING_ARRAY ||
1031 group->entries[j].arg == G_OPTION_ARG_FILENAME_ARRAY, FALSE);
1033 add_pending_null (context, &((*argv)[*index]), NULL);
1035 if (!parse_arg (context, group, &group->entries[j], (*argv)[*index], "", error))
1046 free_changes_list (GOptionContext *context,
1051 for (list = context->changes; list != NULL; list = list->next)
1053 Change *change = list->data;
1057 switch (change->arg_type)
1059 case G_OPTION_ARG_NONE:
1060 *(gboolean *)change->arg_data = change->prev.bool;
1062 case G_OPTION_ARG_INT:
1063 *(gint *)change->arg_data = change->prev.integer;
1065 case G_OPTION_ARG_STRING:
1066 case G_OPTION_ARG_FILENAME:
1067 g_free (change->allocated.str);
1068 *(gchar **)change->arg_data = change->prev.str;
1070 case G_OPTION_ARG_STRING_ARRAY:
1071 case G_OPTION_ARG_FILENAME_ARRAY:
1072 g_strfreev (change->allocated.array.data);
1073 *(gchar ***)change->arg_data = change->prev.array;
1076 g_assert_not_reached ();
1083 g_list_free (context->changes);
1084 context->changes = NULL;
1088 free_pending_nulls (GOptionContext *context,
1089 gboolean perform_nulls)
1093 for (list = context->pending_nulls; list != NULL; list = list->next)
1095 PendingNull *n = list->data;
1101 /* Copy back the short options */
1103 strcpy (*n->ptr + 1, n->value);
1113 g_list_free (context->pending_nulls);
1114 context->pending_nulls = NULL;
1118 * g_option_context_parse:
1119 * @context: a #GOptionContext
1120 * @argc: a pointer to the number of command line arguments.
1121 * @argv: a pointer to the array of command line arguments.
1122 * @error: a return location for errors
1124 * Parses the command line arguments, recognizing options
1125 * which have been added to @context. A side-effect of
1126 * calling this function is that g_set_prgname() will be
1129 * If the parsing is successful, any parsed arguments are
1130 * removed from the array and @argc and @argv are updated
1131 * accordingly. A '--' option is stripped from @argv
1132 * unless there are unparsed options before and after it,
1133 * or some of the options after it start with '-'. In case
1134 * of an error, @argc and @argv are left unmodified.
1136 * If automatic <option>--help</option> support is enabled
1137 * (see g_option_context_set_help_enabled()), and the
1138 * @argv array contains one of the recognized help options,
1139 * this function will produce help output to stdout and
1140 * call <literal>exit (0)</literal>.
1142 * Return value: %TRUE if the parsing was successful,
1143 * %FALSE if an error occurred
1148 g_option_context_parse (GOptionContext *context,
1156 /* Set program name */
1157 if (argc && argv && *argc)
1161 prgname = g_path_get_basename ((*argv)[0]);
1162 g_set_prgname (prgname);
1167 g_set_prgname ("<unknown>");
1170 /* Call pre-parse hooks */
1171 list = context->groups;
1174 GOptionGroup *group = list->data;
1176 if (group->pre_parse_func)
1178 if (!(* group->pre_parse_func) (context, group,
1179 group->user_data, error))
1186 if (context->main_group && context->main_group->pre_parse_func)
1188 if (!(* context->main_group->pre_parse_func) (context, context->main_group,
1189 context->main_group->user_data, error))
1195 gboolean stop_parsing = FALSE;
1196 gboolean has_unknown = FALSE;
1197 gint separator_pos = 0;
1199 for (i = 1; i < *argc; i++)
1202 gboolean parsed = FALSE;
1204 if ((*argv)[i][0] == '-' && (*argv)[i][1] != '\0' && !stop_parsing)
1206 if ((*argv)[i][1] == '-')
1210 arg = (*argv)[i] + 2;
1212 /* '--' terminates list of arguments */
1216 stop_parsing = TRUE;
1220 /* Handle help options */
1221 if (context->help_enabled)
1223 if (strcmp (arg, "help") == 0)
1224 print_help (context, TRUE, NULL);
1225 else if (strcmp (arg, "help-all") == 0)
1226 print_help (context, FALSE, NULL);
1227 else if (strncmp (arg, "help-", 5) == 0)
1231 list = context->groups;
1235 GOptionGroup *group = list->data;
1237 if (strcmp (arg + 5, group->name) == 0)
1238 print_help (context, FALSE, group);
1245 if (context->main_group &&
1246 !parse_long_option (context, context->main_group, &i, arg,
1247 argc, argv, error, &parsed))
1253 /* Try the groups */
1254 list = context->groups;
1257 GOptionGroup *group = list->data;
1259 if (!parse_long_option (context, group, &i, arg,
1260 argc, argv, error, &parsed))
1272 /* Now look for --<group>-<option> */
1273 dash = strchr (arg, '-');
1276 /* Try the groups */
1277 list = context->groups;
1280 GOptionGroup *group = list->data;
1282 if (strncmp (group->name, arg, dash - arg) == 0)
1284 if (!parse_long_option (context, group, &i, dash + 1,
1285 argc, argv, error, &parsed))
1296 if (context->ignore_unknown)
1304 gboolean *nulled_out = NULL;
1306 arg = (*argv)[i] + 1;
1310 if (context->ignore_unknown)
1311 nulled_out = g_new0 (gboolean, strlen (arg));
1313 for (j = 0; j < strlen (arg); j++)
1315 if (context->help_enabled && arg[j] == '?')
1316 print_help (context, TRUE, NULL);
1320 if (context->main_group &&
1321 !parse_short_option (context, context->main_group,
1323 argc, argv, error, &parsed))
1326 g_free (nulled_out);
1332 /* Try the groups */
1333 list = context->groups;
1336 GOptionGroup *group = list->data;
1338 if (!parse_short_option (context, group, i, &new_i, arg[j],
1339 argc, argv, error, &parsed))
1349 if (context->ignore_unknown)
1352 nulled_out[j] = TRUE;
1361 if (context->ignore_unknown)
1363 gchar *new_arg = NULL;
1366 for (j = 0; j < strlen (arg); j++)
1371 new_arg = g_malloc (strlen (arg) + 1);
1372 new_arg[arg_index++] = arg[j];
1376 new_arg[arg_index] = '\0';
1378 add_pending_null (context, &((*argv)[i]), new_arg);
1382 add_pending_null (context, &((*argv)[i]), NULL);
1390 if (!parsed && !context->ignore_unknown)
1393 G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
1394 _("Unknown option %s"), (*argv)[i]);
1400 /* Collect remaining args */
1401 if (context->main_group &&
1402 !parse_remaining_arg (context, context->main_group, &i,
1403 argc, argv, error, &parsed))
1406 if (!parsed && (has_unknown || (*argv)[i][0] == '-'))
1411 if (separator_pos > 0)
1412 add_pending_null (context, &((*argv)[separator_pos]), NULL);
1416 /* Call post-parse hooks */
1417 list = context->groups;
1420 GOptionGroup *group = list->data;
1422 if (group->post_parse_func)
1424 if (!(* group->post_parse_func) (context, group,
1425 group->user_data, error))
1432 if (context->main_group && context->main_group->post_parse_func)
1434 if (!(* context->main_group->post_parse_func) (context, context->main_group,
1435 context->main_group->user_data, error))
1441 free_pending_nulls (context, TRUE);
1443 for (i = 1; i < *argc; i++)
1445 for (k = i; k < *argc; k++)
1446 if ((*argv)[k] != NULL)
1452 for (j = i + k; j < *argc; j++)
1454 (*argv)[j-k] = (*argv)[j];
1466 /* Call error hooks */
1467 list = context->groups;
1470 GOptionGroup *group = list->data;
1472 if (group->error_func)
1473 (* group->error_func) (context, group,
1474 group->user_data, error);
1479 if (context->main_group && context->main_group->error_func)
1480 (* context->main_group->error_func) (context, context->main_group,
1481 context->main_group->user_data, error);
1483 free_changes_list (context, TRUE);
1484 free_pending_nulls (context, FALSE);
1490 * g_option_group_new:
1491 * @name: the name for the option group, this is used to provide
1492 * help for the options in this group with <option>--help-</option>@name
1493 * @description: a description for this group to be shown in
1494 * <option>--help</option>. This string is translated using the translation
1495 * domain or translation function of the group
1496 * @help_description: a description for the <option>--help-</option>@name option.
1497 * This string is translated using the translation domain or translation function
1499 * @user_data: user data that will be passed to the pre- and post-parse hooks,
1500 * the error hook and to callbacks of %G_OPTION_ARG_CALLBACK options, or %NULL
1501 * @destroy: a function that will be called to free @user_data, or %NULL
1503 * Creates a new #GOptionGroup.
1505 * Return value: a newly created option group. It should be added
1506 * to a #GOptionContext or freed with g_option_group_free().
1511 g_option_group_new (const gchar *name,
1512 const gchar *description,
1513 const gchar *help_description,
1515 GDestroyNotify destroy)
1518 GOptionGroup *group;
1520 group = g_new0 (GOptionGroup, 1);
1521 group->name = g_strdup (name);
1522 group->description = g_strdup (description);
1523 group->help_description = g_strdup (help_description);
1524 group->user_data = user_data;
1525 group->destroy_notify = destroy;
1532 * g_option_group_free:
1533 * @group: a #GOptionGroup
1535 * Frees a #GOptionGroup. Note that you must <emphasis>not</emphasis>
1536 * free groups which have been added to a #GOptionContext.
1541 g_option_group_free (GOptionGroup *group)
1543 g_return_if_fail (group != NULL);
1545 g_free (group->name);
1546 g_free (group->description);
1547 g_free (group->help_description);
1549 g_free (group->entries);
1551 if (group->destroy_notify)
1552 (* group->destroy_notify) (group->user_data);
1554 if (group->translate_notify)
1555 (* group->translate_notify) (group->translate_data);
1562 * g_option_group_add_entries:
1563 * @group: a #GOptionGroup
1564 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
1566 * Adds the options specified in @entries to @group.
1571 g_option_group_add_entries (GOptionGroup *group,
1572 const GOptionEntry *entries)
1576 g_return_if_fail (entries != NULL);
1578 for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++);
1580 group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
1582 memcpy (group->entries + group->n_entries, entries, sizeof (GOptionEntry) * n_entries);
1584 group->n_entries += n_entries;
1588 * g_option_group_set_parse_hooks:
1589 * @group: a #GOptionGroup
1590 * @pre_parse_func: a function to call before parsing, or %NULL
1591 * @post_parse_func: a function to call after parsing, or %NULL
1593 * Associates two functions with @group which will be called
1594 * from g_option_context_parse() before the first option is parsed
1595 * and after the last option has been parsed, respectively.
1597 * Note that the user data to be passed to @pre_parse_func and
1598 * @post_parse_func can be specified when constructing the group
1599 * with g_option_group_new().
1604 g_option_group_set_parse_hooks (GOptionGroup *group,
1605 GOptionParseFunc pre_parse_func,
1606 GOptionParseFunc post_parse_func)
1608 g_return_if_fail (group != NULL);
1610 group->pre_parse_func = pre_parse_func;
1611 group->post_parse_func = post_parse_func;
1615 * g_option_group_set_error_hook:
1616 * @group: a #GOptionGroup
1617 * @error_func: a function to call when an error occurs
1619 * Associates a function with @group which will be called
1620 * from g_option_context_parse() when an error occurs.
1622 * Note that the user data to be passed to @pre_parse_func and
1623 * @post_parse_func can be specified when constructing the group
1624 * with g_option_group_new().
1629 g_option_group_set_error_hook (GOptionGroup *group,
1630 GOptionErrorFunc error_func)
1632 g_return_if_fail (group != NULL);
1634 group->error_func = error_func;
1639 * g_option_group_set_translate_func:
1640 * @group: a #GOptionGroup
1641 * @func: the #GTranslateFunc, or %NULL
1642 * @data: user data to pass to @func, or %NULL
1643 * @destroy_notify: a function which gets called to free @data, or %NULL
1645 * Sets the function which is used to translate user-visible
1646 * strings, for <option>--help</option> output. Different
1647 * groups can use different #GTranslateFunc<!-- -->s. If @func
1648 * is %NULL, strings are not translated.
1650 * If you are using gettext(), you only need to set the translation
1651 * domain, see g_option_group_set_translation_domain().
1656 g_option_group_set_translate_func (GOptionGroup *group,
1657 GTranslateFunc func,
1659 GDestroyNotify destroy_notify)
1661 g_return_if_fail (group != NULL);
1663 if (group->translate_notify)
1664 group->translate_notify (group->translate_data);
1666 group->translate_func = func;
1667 group->translate_data = data;
1668 group->translate_notify = destroy_notify;
1672 dgettext_swapped (const gchar *msgid,
1673 const gchar *domainname)
1675 return dgettext (domainname, msgid);
1679 * g_option_group_set_translation_domain:
1680 * @group: a #GOptionGroup
1681 * @domain: the domain to use
1683 * A convenience function to use gettext() for translating
1684 * user-visible strings.
1689 g_option_group_set_translation_domain (GOptionGroup *group,
1690 const gchar *domain)
1692 g_return_if_fail (group != NULL);
1694 g_option_group_set_translate_func (group,
1695 (GTranslateFunc)dgettext_swapped,
1700 #define __G_OPTION_C__
1701 #include "galiasdef.c"