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 context->main_group = group;
319 * g_option_context_get_main_group:
320 * @context: a #GOptionContext
322 * Returns a pointer to the main group of @context.
324 * Return value: the main group of @context, or %NULL if @context doesn't
325 * have a main group. Note that group belongs to @context and should
326 * not be modified or freed.
331 g_option_context_get_main_group (GOptionContext *context)
333 g_return_val_if_fail (context != NULL, NULL);
335 return context->main_group;
339 * g_option_context_add_main_entries:
340 * @context: a #GOptionContext
341 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
342 * @translation_domain: a translation domain to use for translating
343 * the <option>--help</option> output for the options in @entries
344 * with gettext(), or %NULL
346 * A convenience function which creates a main group if it doesn't
347 * exist, adds the @entries to it and sets the translation domain.
352 g_option_context_add_main_entries (GOptionContext *context,
353 const GOptionEntry *entries,
354 const gchar *translation_domain)
356 g_return_if_fail (entries != NULL);
358 if (!context->main_group)
359 context->main_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
361 g_option_group_add_entries (context->main_group, entries);
362 g_option_group_set_translation_domain (context->main_group, translation_domain);
366 print_entry (GOptionGroup *group,
368 const GOptionEntry *entry)
372 if (entry->flags & G_OPTION_FLAG_HIDDEN)
375 str = g_string_new (NULL);
377 if (entry->short_name)
378 g_string_append_printf (str, " -%c, --%s", entry->short_name, entry->long_name);
380 g_string_append_printf (str, " --%s", entry->long_name);
382 if (entry->arg_description)
383 g_string_append_printf (str, "=%s", TRANSLATE (group, entry->arg_description));
385 g_print ("%-*s %s\n", max_length + 4, str->str,
386 entry->description ? TRANSLATE (group, entry->description) : "");
387 g_string_free (str, TRUE);
391 print_help (GOptionContext *context,
396 gint max_length, len;
398 GHashTable *shadow_map;
401 g_print ("%s\n %s %s %s\n\n",
402 _("Usage:"), g_get_prgname(), _("[OPTION...]"),
403 context->parameter_string ? context->parameter_string : "");
405 memset (seen, 0, sizeof (gboolean) * 256);
406 shadow_map = g_hash_table_new (g_str_hash, g_str_equal);
408 if (context->main_group)
410 for (i = 0; i < context->main_group->n_entries; i++)
412 g_hash_table_insert (shadow_map,
413 (gpointer)context->main_group->entries[i].long_name,
414 context->main_group->entries + i);
416 if (seen[(guchar)context->main_group->entries[i].short_name])
417 context->main_group->entries[i].short_name = 0;
419 seen[(guchar)context->main_group->entries[i].short_name] = TRUE;
423 list = context->groups;
426 GOptionGroup *group = list->data;
427 for (i = 0; i < group->n_entries; i++)
429 if (g_hash_table_lookup (shadow_map, group->entries[i].long_name))
430 group->entries[i].long_name = g_strdup_printf ("%s-%s", group->name, group->entries[i].long_name);
432 g_hash_table_insert (shadow_map, (gpointer)group->entries[i].long_name, group->entries + i);
434 if (seen[(guchar)group->entries[i].short_name])
435 group->entries[i].short_name = 0;
437 seen[(guchar)group->entries[i].short_name] = TRUE;
442 g_hash_table_destroy (shadow_map);
444 list = context->groups;
446 max_length = g_utf8_strlen ("--help, -?", -1);
450 len = g_utf8_strlen ("--help-all", -1);
451 max_length = MAX (max_length, len);
456 GOptionGroup *group = list->data;
458 /* First, we check the --help-<groupname> options */
459 len = g_utf8_strlen ("--help-", -1) + g_utf8_strlen (group->name, -1);
460 max_length = MAX (max_length, len);
462 /* Then we go through the entries */
463 for (i = 0; i < group->n_entries; i++)
465 if (group->entries[i].flags & G_OPTION_FLAG_HIDDEN)
468 len = g_utf8_strlen (group->entries[i].long_name, -1);
470 if (group->entries[i].short_name)
473 if (group->entries[i].arg != G_OPTION_ARG_NONE &&
474 group->entries[i].arg_description)
475 len += 1 + g_utf8_strlen (TRANSLATE (group, group->entries[i].arg_description), -1);
477 max_length = MAX (max_length, len);
483 /* Add a bit of padding */
486 list = context->groups;
488 g_print ("%s\n --%-*s %s\n",
489 _("Help Options:"), max_length, "help", _("Show help options"));
491 /* We only want --help-all when there are groups */
493 g_print (" --%-*s %s\n", max_length, "help-all", _("Show all help options"));
497 GOptionGroup *group = list->data;
499 g_print (" --help-%-*s %s\n", max_length - 5, group->name, TRANSLATE (group, group->help_description));
508 /* Print a certain group */
510 g_print ("%s\n", TRANSLATE (group, group->description));
511 for (i = 0; i < group->n_entries; i++)
512 print_entry (group, max_length, &group->entries[i]);
517 /* Print all groups */
519 list = context->groups;
523 GOptionGroup *group = list->data;
525 g_print ("%s\n", group->description);
527 for (i = 0; i < group->n_entries; i++)
528 if (!(group->entries[i].flags & G_OPTION_FLAG_IN_MAIN))
529 print_entry (group, max_length, &group->entries[i]);
536 /* Print application options if --help or --help-all has been specified */
537 if (main_help || !group)
539 list = context->groups;
541 g_print ("%s\n", _("Application Options:"));
543 if (context->main_group)
544 for (i = 0; i < context->main_group->n_entries; i++)
545 print_entry (context->main_group, max_length, &context->main_group->entries[i]);
549 GOptionGroup *group = list->data;
551 /* Print main entries from other groups */
552 for (i = 0; i < group->n_entries; i++)
553 if (group->entries[i].flags & G_OPTION_FLAG_IN_MAIN)
554 print_entry (group, max_length, &group->entries[i]);
566 parse_int (const gchar *arg_name,
572 glong tmp = strtol (arg, &end, 0);
576 if (*arg == '\0' || *end != '\0')
579 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
580 _("Cannot parse integer value '%s' for --%s"),
586 if (*result != tmp || errno == ERANGE)
589 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
590 _("Integer value '%s' for %s out of range"),
599 get_change (GOptionContext *context,
604 Change *change = NULL;
606 for (list = context->changes; list != NULL; list = list->next)
610 if (change->arg_data == arg_data)
614 change = g_new0 (Change, 1);
615 change->arg_type = arg_type;
616 change->arg_data = arg_data;
618 context->changes = g_list_prepend (context->changes, change);
626 add_pending_null (GOptionContext *context,
632 n = g_new0 (PendingNull, 1);
636 context->pending_nulls = g_list_prepend (context->pending_nulls, n);
640 parse_arg (GOptionContext *context,
644 const gchar *option_name,
652 case G_OPTION_ARG_NONE:
654 change = get_change (context, G_OPTION_ARG_NONE,
657 *(gboolean *)entry->arg_data = !(entry->flags & G_OPTION_FLAG_REVERSE);
660 case G_OPTION_ARG_STRING:
664 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
669 change = get_change (context, G_OPTION_ARG_STRING,
671 g_free (change->allocated.str);
673 change->prev.str = *(gchar **)entry->arg_data;
674 change->allocated.str = data;
676 *(gchar **)entry->arg_data = data;
679 case G_OPTION_ARG_STRING_ARRAY:
683 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
688 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
691 if (change->allocated.array.len == 0)
693 change->prev.array = entry->arg_data;
694 change->allocated.array.data = g_new (gchar *, 2);
697 change->allocated.array.data =
698 g_renew (gchar *, change->allocated.array.data,
699 change->allocated.array.len + 2);
701 change->allocated.array.data[change->allocated.array.len] = data;
702 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
704 change->allocated.array.len ++;
706 *(gchar ***)entry->arg_data = change->allocated.array.data;
711 case G_OPTION_ARG_FILENAME:
716 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
721 data = g_strdup (value);
723 change = get_change (context, G_OPTION_ARG_FILENAME,
725 g_free (change->allocated.str);
727 change->prev.str = *(gchar **)entry->arg_data;
728 change->allocated.str = data;
730 *(gchar **)entry->arg_data = data;
734 case G_OPTION_ARG_FILENAME_ARRAY:
739 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
744 data = g_strdup (value);
746 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
749 if (change->allocated.array.len == 0)
751 change->prev.array = entry->arg_data;
752 change->allocated.array.data = g_new (gchar *, 2);
755 change->allocated.array.data =
756 g_renew (gchar *, change->allocated.array.data,
757 change->allocated.array.len + 2);
759 change->allocated.array.data[change->allocated.array.len] = data;
760 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
762 change->allocated.array.len ++;
764 *(gchar ***)entry->arg_data = change->allocated.array.data;
769 case G_OPTION_ARG_INT:
773 if (!parse_int (option_name, value,
778 change = get_change (context, G_OPTION_ARG_INT,
780 change->prev.integer = *(gint *)entry->arg_data;
781 *(gint *)entry->arg_data = data;
784 case G_OPTION_ARG_CALLBACK:
789 tmp = g_locale_to_utf8 (value, -1, NULL, NULL, error);
794 retval = (* (GOptionArgFunc) entry->arg_data) (option_name, tmp, group->user_data, error);
803 g_assert_not_reached ();
810 parse_short_option (GOptionContext *context,
822 for (j = 0; j < group->n_entries; j++)
824 if (arg == group->entries[j].short_name)
826 if (group->entries[j].arg == G_OPTION_ARG_NONE)
828 parse_arg (context, group, &group->entries[j],
837 if (*new_index > index)
839 g_warning ("FIXME: figure out the correct error here");
844 if (index < *argc - 1)
846 value = (*argv)[index + 1];
847 add_pending_null (context, &((*argv)[index + 1]), NULL);
848 *new_index = index + 1;
854 option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
856 if (!parse_arg (context, group, &group->entries[j], value, option_name, error))
858 g_free (option_name);
862 g_free (option_name);
872 parse_long_option (GOptionContext *context,
883 for (j = 0; j < group->n_entries; j++)
888 if (group->entries[j].arg == G_OPTION_ARG_NONE &&
889 strcmp (arg, group->entries[j].long_name) == 0)
891 parse_arg (context, group, &group->entries[j],
894 add_pending_null (context, &((*argv)[*index]), NULL);
899 gint len = strlen (group->entries[j].long_name);
901 if (strncmp (arg, group->entries[j].long_name, len) == 0 &&
902 (arg[len] == '=' || arg[len] == 0))
907 add_pending_null (context, &((*argv)[*index]), NULL);
910 value = arg + len + 1;
911 else if (*index < *argc - 1)
913 value = (*argv)[*index + 1];
914 add_pending_null (context, &((*argv)[*index + 1]), NULL);
920 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
922 if (!parse_arg (context, group, &group->entries[j], value, option_name, error))
924 g_free (option_name);
928 g_free (option_name);
938 parse_remaining_arg (GOptionContext *context,
948 for (j = 0; j < group->n_entries; j++)
953 if (group->entries[j].long_name[0])
956 g_return_val_if_fail (group->entries[j].arg == G_OPTION_ARG_STRING_ARRAY ||
957 group->entries[j].arg == G_OPTION_ARG_FILENAME_ARRAY, FALSE);
959 add_pending_null (context, &((*argv)[*index]), NULL);
961 if (!parse_arg (context, group, &group->entries[j], (*argv)[*index], "", error))
972 free_changes_list (GOptionContext *context,
977 for (list = context->changes; list != NULL; list = list->next)
979 Change *change = list->data;
983 switch (change->arg_type)
985 case G_OPTION_ARG_NONE:
986 *(gboolean *)change->arg_data = change->prev.bool;
988 case G_OPTION_ARG_INT:
989 *(gint *)change->arg_data = change->prev.integer;
991 case G_OPTION_ARG_STRING:
992 case G_OPTION_ARG_FILENAME:
993 g_free (change->allocated.str);
994 *(gchar **)change->arg_data = change->prev.str;
996 case G_OPTION_ARG_STRING_ARRAY:
997 case G_OPTION_ARG_FILENAME_ARRAY:
998 g_strfreev (change->allocated.array.data);
999 *(gchar ***)change->arg_data = change->prev.array;
1002 g_assert_not_reached ();
1009 g_list_free (context->changes);
1010 context->changes = NULL;
1014 free_pending_nulls (GOptionContext *context,
1015 gboolean perform_nulls)
1019 for (list = context->pending_nulls; list != NULL; list = list->next)
1021 PendingNull *n = list->data;
1027 /* Copy back the short options */
1029 strcpy (*n->ptr + 1, n->value);
1039 g_list_free (context->pending_nulls);
1040 context->pending_nulls = NULL;
1044 * g_option_context_parse:
1045 * @context: a #GOptionContext
1046 * @argc: a pointer to the number of command line arguments.
1047 * @argv: a pointer to the array of command line arguments.
1048 * @error: a return location for errors
1050 * Parses the command line arguments, recognizing options
1051 * which have been added to @context. A side-effect of
1052 * calling this function is that g_set_prgname() will be
1055 * If the parsing is successful, any parsed arguments are
1056 * removed from the array and @argc and @argv are updated
1057 * accordingly. In case of an error, @argc and @argv are
1060 * If automatic <option>--help</option> support is enabled
1061 * (see g_option_context_set_help_enabled()), and the
1062 * @argv array contains one of the recognized help options,
1063 * this function will produce help output to stdout and
1064 * call <literal>exit (0)</literal>.
1066 * Return value: %TRUE if the parsing was successful,
1067 * %FALSE if an error occurred
1072 g_option_context_parse (GOptionContext *context,
1080 /* Set program name */
1081 if (argc && argv && *argc)
1085 prgname = g_path_get_basename ((*argv)[0]);
1086 g_set_prgname (prgname);
1091 g_set_prgname ("<unknown>");
1094 /* Call pre-parse hooks */
1095 list = context->groups;
1098 GOptionGroup *group = list->data;
1100 if (group->pre_parse_func)
1102 if (!(* group->pre_parse_func) (context, group,
1103 group->user_data, error))
1110 if (context->main_group && context->main_group->pre_parse_func)
1112 if (!(* context->main_group->pre_parse_func) (context, context->main_group,
1113 context->main_group->user_data, error))
1119 gboolean stop_parsing = FALSE;
1121 for (i = 1; i < *argc; i++)
1124 gboolean parsed = FALSE;
1126 if ((*argv)[i][0] == '-' && !stop_parsing)
1128 if ((*argv)[i][1] == '-')
1132 arg = (*argv)[i] + 2;
1134 /* '--' terminates list of arguments */
1137 add_pending_null (context, &((*argv)[i]), NULL);
1138 stop_parsing = TRUE;
1142 /* Handle help options */
1143 if (context->help_enabled)
1145 if (strcmp (arg, "help") == 0)
1146 print_help (context, TRUE, NULL);
1147 else if (strcmp (arg, "help-all") == 0)
1148 print_help (context, FALSE, NULL);
1149 else if (strncmp (arg, "help-", 5) == 0)
1153 list = context->groups;
1157 GOptionGroup *group = list->data;
1159 if (strcmp (arg + 5, group->name) == 0)
1160 print_help (context, FALSE, group);
1167 if (context->main_group &&
1168 !parse_long_option (context, context->main_group, &i, arg,
1169 argc, argv, error, &parsed))
1175 /* Try the groups */
1176 list = context->groups;
1179 GOptionGroup *group = list->data;
1181 if (!parse_long_option (context, group, &i, arg,
1182 argc, argv, error, &parsed))
1194 /* Now look for --<group>-<option> */
1195 dash = strchr (arg, '-');
1198 /* Try the groups */
1199 list = context->groups;
1202 GOptionGroup *group = list->data;
1204 if (strncmp (group->name, arg, dash - arg) == 0)
1206 if (!parse_long_option (context, group, &i, dash + 1,
1207 argc, argv, error, &parsed))
1218 if (context->ignore_unknown)
1226 gboolean *nulled_out = NULL;
1228 arg = (*argv)[i] + 1;
1232 if (context->ignore_unknown)
1233 nulled_out = g_new0 (gboolean, strlen (arg));
1235 for (j = 0; j < strlen (arg); j++)
1239 if (context->main_group &&
1240 !parse_short_option (context, context->main_group,
1242 argc, argv, error, &parsed))
1245 g_free (nulled_out);
1251 /* Try the groups */
1252 list = context->groups;
1255 GOptionGroup *group = list->data;
1257 if (!parse_short_option (context, group, i, &new_i, arg[j],
1258 argc, argv, error, &parsed))
1268 if (context->ignore_unknown)
1271 nulled_out[j] = TRUE;
1280 if (context->ignore_unknown)
1282 gchar *new_arg = NULL;
1285 for (j = 0; j < strlen (arg); j++)
1290 new_arg = g_malloc (strlen (arg));
1291 new_arg[arg_index++] = arg[j];
1295 new_arg[arg_index] = '\0';
1297 add_pending_null (context, &((*argv)[i]), new_arg);
1301 add_pending_null (context, &((*argv)[i]), NULL);
1306 if (!parsed && !context->ignore_unknown)
1309 G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
1310 _("Unknown option %s"), (*argv)[i]);
1316 /* Collect remaining args */
1317 if (context->main_group &&
1318 !parse_remaining_arg (context, context->main_group, &i,
1319 argc, argv, error, &parsed))
1325 /* Call post-parse hooks */
1326 list = context->groups;
1329 GOptionGroup *group = list->data;
1331 if (group->post_parse_func)
1333 if (!(* group->post_parse_func) (context, group,
1334 group->user_data, error))
1341 if (context->main_group && context->main_group->post_parse_func)
1343 if (!(* context->main_group->post_parse_func) (context, context->main_group,
1344 context->main_group->user_data, error))
1348 free_pending_nulls (context, TRUE);
1350 for (i = 1; i < *argc; i++)
1352 for (k = i; k < *argc; k++)
1353 if ((*argv)[k] != NULL)
1359 for (j = i + k; j < *argc; j++)
1361 (*argv)[j-k] = (*argv)[j];
1373 /* Call error hooks */
1374 list = context->groups;
1377 GOptionGroup *group = list->data;
1379 if (group->error_func)
1380 (* group->error_func) (context, group,
1381 group->user_data, error);
1386 if (context->main_group && context->main_group->error_func)
1387 (* context->main_group->error_func) (context, context->main_group,
1388 context->main_group->user_data, error);
1390 free_changes_list (context, TRUE);
1391 free_pending_nulls (context, FALSE);
1397 * g_option_group_new:
1398 * @name: the name for the option group, this is used to provide
1399 * help for the options in this group with <option>--help-</option>@name
1400 * @description: a description for this group to be shown in
1401 * <option>--help</option>. This string is translated using the translation
1402 * domain or translation function of the group
1403 * @help_description: a description for the <option>--help-</option>@name option.
1404 * This string is translated using the translation domain or translation function
1406 * @user_data: user data that will be passed to the pre- and post-parse hooks,
1407 * the error hook and to callbacks of %G_OPTION_ARG_CALLBACK options, or %NULL
1408 * @destroy: a function that will be called to free @user_data, or %NULL
1410 * Creates a new #GOptionGroup.
1412 * Return value: a newly created option group. It should be added
1413 * to a #GOptionContext or freed with g_option_group_free().
1418 g_option_group_new (const gchar *name,
1419 const gchar *description,
1420 const gchar *help_description,
1422 GDestroyNotify destroy)
1425 GOptionGroup *group;
1427 group = g_new0 (GOptionGroup, 1);
1428 group->name = g_strdup (name);
1429 group->description = g_strdup (description);
1430 group->help_description = g_strdup (help_description);
1431 group->user_data = user_data;
1432 group->destroy_notify = destroy;
1439 * g_option_group_free:
1440 * @group: a #GOptionGroup
1442 * Frees a #GOptionGroup. Note that you must <emphasis>not</emphasis>
1443 * free groups which have been added to a #GOptionContext.
1448 g_option_group_free (GOptionGroup *group)
1450 g_return_if_fail (group != NULL);
1452 g_free (group->name);
1453 g_free (group->description);
1454 g_free (group->help_description);
1456 g_free (group->entries);
1458 if (group->destroy_notify)
1459 (* group->destroy_notify) (group->user_data);
1461 if (group->translate_notify)
1462 (* group->translate_notify) (group->translate_data);
1469 * g_option_group_add_entries:
1470 * @group: a #GOptionGroup
1471 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
1473 * Adds the options specified in @entries to @group.
1478 g_option_group_add_entries (GOptionGroup *group,
1479 const GOptionEntry *entries)
1483 g_return_if_fail (entries != NULL);
1485 for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++);
1487 group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
1489 memcpy (group->entries + group->n_entries, entries, sizeof (GOptionEntry) * n_entries);
1491 group->n_entries += n_entries;
1495 * g_option_group_set_parse_hooks:
1496 * @group: a #GOptionGroup
1497 * @pre_parse_func: a function to call before parsing, or %NULL
1498 * @post_parse_func: a function to call after parsing, or %NULL
1500 * Associates two functions with @group which will be called
1501 * from g_option_context_parse() before the first option is parsed
1502 * and after the last option has been parsed, respectively.
1504 * Note that the user data to be passed to @pre_parse_func and
1505 * @post_parse_func can be specified when constructing the group
1506 * with g_option_group_new().
1511 g_option_group_set_parse_hooks (GOptionGroup *group,
1512 GOptionParseFunc pre_parse_func,
1513 GOptionParseFunc post_parse_func)
1515 g_return_if_fail (group != NULL);
1517 group->pre_parse_func = pre_parse_func;
1518 group->post_parse_func = post_parse_func;
1522 * g_option_group_set_error_hook:
1523 * @group: a #GOptionGroup
1524 * @error_func: a function to call when an error occurs
1526 * Associates a function with @group which will be called
1527 * from g_option_context_parse() when an error occurs.
1529 * Note that the user data to be passed to @pre_parse_func and
1530 * @post_parse_func can be specified when constructing the group
1531 * with g_option_group_new().
1536 g_option_group_set_error_hook (GOptionGroup *group,
1537 GOptionErrorFunc error_func)
1539 g_return_if_fail (group != NULL);
1541 group->error_func = error_func;
1546 * g_option_group_set_translate_func:
1547 * @group: a #GOptionGroup
1548 * @func: the #GTranslateFunc, or %NULL
1549 * @data: user data to pass to @func, or %NULL
1550 * @destroy_notify: a function which gets called to free @data, or %NULL
1552 * Sets the function which is used to translate user-visible
1553 * strings, for <option>--help</option> output. Different
1554 * groups can use different #GTranslateFunc<!-- -->s. If @func
1555 * is %NULL, strings are not translated.
1557 * If you are using gettext(), you only need to set the translation
1558 * domain, see g_option_group_set_translation_domain().
1563 g_option_group_set_translate_func (GOptionGroup *group,
1564 GTranslateFunc func,
1566 GDestroyNotify destroy_notify)
1568 g_return_if_fail (group != NULL);
1570 if (group->translate_notify)
1571 group->translate_notify (group->translate_data);
1573 group->translate_func = func;
1574 group->translate_data = data;
1575 group->translate_notify = destroy_notify;
1579 dgettext_swapped (const gchar *msgid,
1580 const gchar *domainname)
1582 return dgettext (domainname, msgid);
1586 * g_option_group_set_translation_domain:
1587 * @group: a #GOptionGroup
1588 * @domain: the domain to use
1590 * A convenience function to use gettext() for translating
1591 * user-visible strings.
1596 g_option_group_set_translation_domain (GOptionGroup *group,
1597 const gchar *domain)
1599 g_return_if_fail (group != NULL);
1601 g_option_group_set_translate_func (group,
1602 (GTranslateFunc)dgettext_swapped,