1 /* goption.c - Option parser
3 * Copyright (C) 1999, 2003 Red Hat Software
4 * Copyright (C) 2004 Anders Carlsson <andersca@gnome.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
34 #define TRANSLATE(group, str) (((group)->translate_func ? (* (group)->translate_func) ((str), (group)->translate_data) : (str)))
36 #define NO_ARG(entry) ((entry)->arg == G_OPTION_ARG_NONE || \
37 ((entry)->arg == G_OPTION_ARG_CALLBACK && \
38 ((entry)->flags & G_OPTION_FLAG_NO_ARG)))
68 struct _GOptionContext
72 gchar *parameter_string;
74 gboolean help_enabled;
75 gboolean ignore_unknown;
77 GOptionGroup *main_group;
79 /* We keep a list of change so we can revert them */
82 /* We also keep track of all argv elements that should be NULLed or
92 gchar *help_description;
94 GDestroyNotify destroy_notify;
97 GTranslateFunc translate_func;
98 GDestroyNotify translate_notify;
99 gpointer translate_data;
101 GOptionEntry *entries;
104 GOptionParseFunc pre_parse_func;
105 GOptionParseFunc post_parse_func;
106 GOptionErrorFunc error_func;
109 static void free_changes_list (GOptionContext *context,
111 static void free_pending_nulls (GOptionContext *context,
112 gboolean perform_nulls);
115 g_option_error_quark (void)
120 q = g_quark_from_static_string ("g-option-context-error-quark");
126 * g_option_context_new:
127 * @parameter_string: a string which is displayed in
128 * the first line of <option>--help</option> output, after
129 * <literal><replaceable>programname</replaceable> [OPTION...]</literal>
131 * Creates a new option context.
133 * Returns: a newly created #GOptionContext, which must be
134 * freed with g_option_context_free() after use.
139 g_option_context_new (const gchar *parameter_string)
142 GOptionContext *context;
144 context = g_new0 (GOptionContext, 1);
146 context->parameter_string = g_strdup (parameter_string);
147 context->help_enabled = TRUE;
148 context->ignore_unknown = FALSE;
154 * g_option_context_free:
155 * @context: a #GOptionContext
157 * Frees context and all the groups which have been
162 void g_option_context_free (GOptionContext *context)
164 g_return_if_fail (context != NULL);
166 g_list_foreach (context->groups, (GFunc)g_option_group_free, NULL);
167 g_list_free (context->groups);
169 if (context->main_group)
170 g_option_group_free (context->main_group);
172 free_changes_list (context, FALSE);
173 free_pending_nulls (context, FALSE);
175 g_free (context->parameter_string);
182 * g_option_context_set_help_enabled:
183 * @context: a #GOptionContext
184 * @help_enabled: %TRUE to enable <option>--help</option>, %FALSE to disable it
186 * Enables or disables automatic generation of <option>--help</option>
187 * output. By default, g_option_context_parse() recognizes
188 * <option>--help</option>, <option>-?</option>, <option>--help-all</option>
189 * and <option>--help-</option><replaceable>groupname</replaceable> and creates
190 * suitable output to stdout.
194 void g_option_context_set_help_enabled (GOptionContext *context,
195 gboolean help_enabled)
198 g_return_if_fail (context != NULL);
200 context->help_enabled = help_enabled;
204 * g_option_context_get_help_enabled:
205 * @context: a #GOptionContext
207 * Returns whether automatic <option>--help</option> generation
208 * is turned on for @context. See g_option_context_set_help_enabled().
210 * Returns: %TRUE if automatic help generation is turned on.
215 g_option_context_get_help_enabled (GOptionContext *context)
217 g_return_val_if_fail (context != NULL, FALSE);
219 return context->help_enabled;
223 * g_option_context_set_ignore_unknown_options:
224 * @context: a #GOptionContext
225 * @ignore_unknown: %TRUE to ignore unknown options, %FALSE to produce
226 * an error when unknown options are met
228 * Sets whether to ignore unknown options or not. If an argument is
229 * ignored, it is left in the @argv array after parsing. By default,
230 * g_option_context_parse() treats unknown options as error.
232 * This setting does not affect non-option arguments (i.e. arguments
233 * which don't start with a dash). But note that GOption cannot reliably
234 * determine whether a non-option belongs to a preceding unknown option.
239 g_option_context_set_ignore_unknown_options (GOptionContext *context,
240 gboolean ignore_unknown)
242 g_return_if_fail (context != NULL);
244 context->ignore_unknown = ignore_unknown;
248 * g_option_context_get_ignore_unknown_options:
249 * @context: a #GOptionContext
251 * Returns whether unknown options are ignored or not. See
252 * g_option_context_set_ignore_unknown_options().
254 * Returns: %TRUE if unknown options are ignored.
259 g_option_context_get_ignore_unknown_options (GOptionContext *context)
261 g_return_val_if_fail (context != NULL, FALSE);
263 return context->ignore_unknown;
267 * g_option_context_add_group:
268 * @context: a #GOptionContext
269 * @group: the group to add
271 * Adds a #GOptionGroup to the @context, so that parsing with @context
272 * will recognize the options in the group. Note that the group will
273 * be freed together with the context when g_option_context_free() is
274 * called, so you must not free the group yourself after adding it
280 g_option_context_add_group (GOptionContext *context,
285 g_return_if_fail (context != NULL);
286 g_return_if_fail (group != NULL);
287 g_return_if_fail (group->name != NULL);
288 g_return_if_fail (group->description != NULL);
289 g_return_if_fail (group->help_description != NULL);
291 for (list = context->groups; list; list = list->next)
293 GOptionGroup *g = (GOptionGroup *)list->data;
295 if ((group->name == NULL && g->name == NULL) ||
296 (group->name && g->name && strcmp (group->name, g->name) == 0))
297 g_warning ("A group named \"%s\" is already part of this GOptionContext",
301 context->groups = g_list_append (context->groups, group);
305 * g_option_context_set_main_group:
306 * @context: a #GOptionContext
307 * @group: the group to set as main group
309 * Sets a #GOptionGroup as main group of the @context.
310 * This has the same effect as calling g_option_context_add_group(),
311 * the only difference is that the options in the main group are
312 * treated differently when generating <option>--help</option> output.
317 g_option_context_set_main_group (GOptionContext *context,
320 g_return_if_fail (context != NULL);
321 g_return_if_fail (group != NULL);
323 if (context->main_group)
325 g_warning ("This GOptionContext already has a main group");
330 context->main_group = group;
334 * g_option_context_get_main_group:
335 * @context: a #GOptionContext
337 * Returns a pointer to the main group of @context.
339 * Return value: the main group of @context, or %NULL if @context doesn't
340 * have a main group. Note that group belongs to @context and should
341 * not be modified or freed.
346 g_option_context_get_main_group (GOptionContext *context)
348 g_return_val_if_fail (context != NULL, NULL);
350 return context->main_group;
354 * g_option_context_add_main_entries:
355 * @context: a #GOptionContext
356 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
357 * @translation_domain: a translation domain to use for translating
358 * the <option>--help</option> output for the options in @entries
359 * with gettext(), or %NULL
361 * A convenience function which creates a main group if it doesn't
362 * exist, adds the @entries to it and sets the translation domain.
367 g_option_context_add_main_entries (GOptionContext *context,
368 const GOptionEntry *entries,
369 const gchar *translation_domain)
371 g_return_if_fail (entries != NULL);
373 if (!context->main_group)
374 context->main_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
376 g_option_group_add_entries (context->main_group, entries);
377 g_option_group_set_translation_domain (context->main_group, translation_domain);
381 calculate_max_length (GOptionGroup *group)
384 gint i, len, max_length;
388 for (i = 0; i < group->n_entries; i++)
390 entry = &group->entries[i];
392 if (entry->flags & G_OPTION_FLAG_HIDDEN)
395 len = g_utf8_strlen (entry->long_name, -1);
397 if (entry->short_name)
400 if (!NO_ARG (entry) && entry->arg_description)
401 len += 1 + g_utf8_strlen (TRANSLATE (group, entry->arg_description), -1);
403 max_length = MAX (max_length, len);
410 print_entry (GOptionGroup *group,
412 const GOptionEntry *entry)
416 if (entry->flags & G_OPTION_FLAG_HIDDEN)
419 if (entry->long_name[0] == 0)
422 str = g_string_new (NULL);
424 if (entry->short_name)
425 g_string_append_printf (str, " -%c, --%s", entry->short_name, entry->long_name);
427 g_string_append_printf (str, " --%s", entry->long_name);
429 if (entry->arg_description)
430 g_string_append_printf (str, "=%s", TRANSLATE (group, entry->arg_description));
432 g_print ("%-*s %s\n", max_length + 4, str->str,
433 entry->description ? TRANSLATE (group, entry->description) : "");
434 g_string_free (str, TRUE);
438 print_help (GOptionContext *context,
443 gint max_length, len;
446 GHashTable *shadow_map;
448 const gchar *rest_description;
450 rest_description = NULL;
451 if (context->main_group)
453 for (i = 0; i < context->main_group->n_entries; i++)
455 entry = &context->main_group->entries[i];
456 if (entry->long_name[0] == 0)
458 rest_description = entry->arg_description;
464 g_print ("%s\n %s %s%s%s%s%s\n\n",
465 _("Usage:"), g_get_prgname(), _("[OPTION...]"),
466 rest_description ? " " : "",
467 rest_description ? rest_description : "",
468 context->parameter_string ? " " : "",
469 context->parameter_string ? context->parameter_string : "");
471 memset (seen, 0, sizeof (gboolean) * 256);
472 shadow_map = g_hash_table_new (g_str_hash, g_str_equal);
474 if (context->main_group)
476 for (i = 0; i < context->main_group->n_entries; i++)
478 entry = &context->main_group->entries[i];
479 g_hash_table_insert (shadow_map,
480 (gpointer)entry->long_name,
483 if (seen[(guchar)entry->short_name])
484 entry->short_name = 0;
486 seen[(guchar)entry->short_name] = TRUE;
490 list = context->groups;
493 GOptionGroup *group = list->data;
494 for (i = 0; i < group->n_entries; i++)
496 entry = &group->entries[i];
497 if (g_hash_table_lookup (shadow_map, entry->long_name))
498 entry->long_name = g_strdup_printf ("%s-%s", group->name, entry->long_name);
500 g_hash_table_insert (shadow_map, (gpointer)entry->long_name, entry);
502 if (seen[(guchar)entry->short_name])
503 entry->short_name = 0;
505 seen[(guchar)entry->short_name] = TRUE;
510 g_hash_table_destroy (shadow_map);
512 list = context->groups;
514 max_length = g_utf8_strlen ("-?, --help", -1);
518 len = g_utf8_strlen ("--help-all", -1);
519 max_length = MAX (max_length, len);
522 if (context->main_group)
524 len = calculate_max_length (context->main_group);
525 max_length = MAX (max_length, len);
530 GOptionGroup *group = list->data;
532 /* First, we check the --help-<groupname> options */
533 len = g_utf8_strlen ("--help-", -1) + g_utf8_strlen (group->name, -1);
534 max_length = MAX (max_length, len);
536 /* Then we go through the entries */
537 len = calculate_max_length (group);
538 max_length = MAX (max_length, len);
543 /* Add a bit of padding */
548 list = context->groups;
550 g_print ("%s\n -%c, --%-*s %s\n",
551 _("Help Options:"), '?', max_length - 4, "help",
552 _("Show help options"));
554 /* We only want --help-all when there are groups */
556 g_print (" --%-*s %s\n", max_length, "help-all",
557 _("Show all help options"));
561 GOptionGroup *group = list->data;
563 g_print (" --help-%-*s %s\n", max_length - 5, group->name,
564 TRANSLATE (group, group->help_description));
574 /* Print a certain group */
576 g_print ("%s\n", TRANSLATE (group, group->description));
577 for (i = 0; i < group->n_entries; i++)
578 print_entry (group, max_length, &group->entries[i]);
583 /* Print all groups */
585 list = context->groups;
589 GOptionGroup *group = list->data;
591 g_print ("%s\n", group->description);
593 for (i = 0; i < group->n_entries; i++)
594 if (!(group->entries[i].flags & G_OPTION_FLAG_IN_MAIN))
595 print_entry (group, max_length, &group->entries[i]);
602 /* Print application options if --help or --help-all has been specified */
603 if (main_help || !group)
605 list = context->groups;
607 g_print ("%s\n", _("Application Options:"));
609 if (context->main_group)
610 for (i = 0; i < context->main_group->n_entries; i++)
611 print_entry (context->main_group, max_length,
612 &context->main_group->entries[i]);
616 GOptionGroup *group = list->data;
618 /* Print main entries from other groups */
619 for (i = 0; i < group->n_entries; i++)
620 if (group->entries[i].flags & G_OPTION_FLAG_IN_MAIN)
621 print_entry (group, max_length, &group->entries[i]);
633 parse_int (const gchar *arg_name,
642 tmp = strtol (arg, &end, 0);
644 if (*arg == '\0' || *end != '\0')
647 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
648 _("Cannot parse integer value '%s' for %s"),
654 if (*result != tmp || errno == ERANGE)
657 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
658 _("Integer value '%s' for %s out of range"),
667 get_change (GOptionContext *context,
672 Change *change = NULL;
674 for (list = context->changes; list != NULL; list = list->next)
678 if (change->arg_data == arg_data)
682 change = g_new0 (Change, 1);
683 change->arg_type = arg_type;
684 change->arg_data = arg_data;
686 context->changes = g_list_prepend (context->changes, change);
694 add_pending_null (GOptionContext *context,
700 n = g_new0 (PendingNull, 1);
704 context->pending_nulls = g_list_prepend (context->pending_nulls, n);
708 parse_arg (GOptionContext *context,
712 const gchar *option_name,
720 case G_OPTION_ARG_NONE:
722 change = get_change (context, G_OPTION_ARG_NONE,
725 *(gboolean *)entry->arg_data = !(entry->flags & G_OPTION_FLAG_REVERSE);
728 case G_OPTION_ARG_STRING:
732 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
737 change = get_change (context, G_OPTION_ARG_STRING,
739 g_free (change->allocated.str);
741 change->prev.str = *(gchar **)entry->arg_data;
742 change->allocated.str = data;
744 *(gchar **)entry->arg_data = data;
747 case G_OPTION_ARG_STRING_ARRAY:
751 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
756 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
759 if (change->allocated.array.len == 0)
761 change->prev.array = entry->arg_data;
762 change->allocated.array.data = g_new (gchar *, 2);
765 change->allocated.array.data =
766 g_renew (gchar *, change->allocated.array.data,
767 change->allocated.array.len + 2);
769 change->allocated.array.data[change->allocated.array.len] = data;
770 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
772 change->allocated.array.len ++;
774 *(gchar ***)entry->arg_data = change->allocated.array.data;
779 case G_OPTION_ARG_FILENAME:
784 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
789 data = g_strdup (value);
791 change = get_change (context, G_OPTION_ARG_FILENAME,
793 g_free (change->allocated.str);
795 change->prev.str = *(gchar **)entry->arg_data;
796 change->allocated.str = data;
798 *(gchar **)entry->arg_data = data;
802 case G_OPTION_ARG_FILENAME_ARRAY:
807 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
812 data = g_strdup (value);
814 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
817 if (change->allocated.array.len == 0)
819 change->prev.array = entry->arg_data;
820 change->allocated.array.data = g_new (gchar *, 2);
823 change->allocated.array.data =
824 g_renew (gchar *, change->allocated.array.data,
825 change->allocated.array.len + 2);
827 change->allocated.array.data[change->allocated.array.len] = data;
828 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
830 change->allocated.array.len ++;
832 *(gchar ***)entry->arg_data = change->allocated.array.data;
837 case G_OPTION_ARG_INT:
841 if (!parse_int (option_name, value,
846 change = get_change (context, G_OPTION_ARG_INT,
848 change->prev.integer = *(gint *)entry->arg_data;
849 *(gint *)entry->arg_data = data;
852 case G_OPTION_ARG_CALLBACK:
857 if (entry->flags & G_OPTION_FLAG_NO_ARG)
859 else if (entry->flags & G_OPTION_FLAG_FILENAME)
862 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
864 data = g_strdup (value);
868 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
870 if (!(entry->flags & G_OPTION_FLAG_NO_ARG) && !data)
873 retval = (* (GOptionArgFunc) entry->arg_data) (option_name, data, group->user_data, error);
882 g_assert_not_reached ();
889 parse_short_option (GOptionContext *context,
901 for (j = 0; j < group->n_entries; j++)
903 if (arg == group->entries[j].short_name)
905 if (NO_ARG (&group->entries[j]))
907 parse_arg (context, group, &group->entries[j],
916 if (*new_index > index)
918 g_warning ("FIXME: figure out the correct error here");
923 option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
925 if (index < *argc - 1)
927 value = (*argv)[index + 1];
928 add_pending_null (context, &((*argv)[index + 1]), NULL);
929 *new_index = index + 1;
934 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
935 _("Missing argument for %s"), option_name);
936 g_free (option_name);
940 if (!parse_arg (context, group, &group->entries[j], value, option_name, error))
942 g_free (option_name);
946 g_free (option_name);
956 parse_long_option (GOptionContext *context,
967 for (j = 0; j < group->n_entries; j++)
972 if (NO_ARG (&group->entries[j]) &&
973 strcmp (arg, group->entries[j].long_name) == 0)
975 parse_arg (context, group, &group->entries[j],
978 add_pending_null (context, &((*argv)[*index]), NULL);
983 gint len = strlen (group->entries[j].long_name);
985 if (strncmp (arg, group->entries[j].long_name, len) == 0 &&
986 (arg[len] == '=' || arg[len] == 0))
991 add_pending_null (context, &((*argv)[*index]), NULL);
992 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
995 value = arg + len + 1;
996 else if (*index < *argc - 1)
998 value = (*argv)[*index + 1];
999 add_pending_null (context, &((*argv)[*index + 1]), NULL);
1005 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1006 _("Missing argument for %s"), option_name);
1007 g_free (option_name);
1011 if (!parse_arg (context, group, &group->entries[j], value, option_name, error))
1013 g_free (option_name);
1017 g_free (option_name);
1027 parse_remaining_arg (GOptionContext *context,
1028 GOptionGroup *group,
1037 for (j = 0; j < group->n_entries; j++)
1039 if (*index >= *argc)
1042 if (group->entries[j].long_name[0])
1045 g_return_val_if_fail (group->entries[j].arg == G_OPTION_ARG_STRING_ARRAY ||
1046 group->entries[j].arg == G_OPTION_ARG_FILENAME_ARRAY, FALSE);
1048 add_pending_null (context, &((*argv)[*index]), NULL);
1050 if (!parse_arg (context, group, &group->entries[j], (*argv)[*index], "", error))
1061 free_changes_list (GOptionContext *context,
1066 for (list = context->changes; list != NULL; list = list->next)
1068 Change *change = list->data;
1072 switch (change->arg_type)
1074 case G_OPTION_ARG_NONE:
1075 *(gboolean *)change->arg_data = change->prev.bool;
1077 case G_OPTION_ARG_INT:
1078 *(gint *)change->arg_data = change->prev.integer;
1080 case G_OPTION_ARG_STRING:
1081 case G_OPTION_ARG_FILENAME:
1082 g_free (change->allocated.str);
1083 *(gchar **)change->arg_data = change->prev.str;
1085 case G_OPTION_ARG_STRING_ARRAY:
1086 case G_OPTION_ARG_FILENAME_ARRAY:
1087 g_strfreev (change->allocated.array.data);
1088 *(gchar ***)change->arg_data = change->prev.array;
1091 g_assert_not_reached ();
1098 g_list_free (context->changes);
1099 context->changes = NULL;
1103 free_pending_nulls (GOptionContext *context,
1104 gboolean perform_nulls)
1108 for (list = context->pending_nulls; list != NULL; list = list->next)
1110 PendingNull *n = list->data;
1116 /* Copy back the short options */
1118 strcpy (*n->ptr + 1, n->value);
1128 g_list_free (context->pending_nulls);
1129 context->pending_nulls = NULL;
1133 * g_option_context_parse:
1134 * @context: a #GOptionContext
1135 * @argc: a pointer to the number of command line arguments.
1136 * @argv: a pointer to the array of command line arguments.
1137 * @error: a return location for errors
1139 * Parses the command line arguments, recognizing options
1140 * which have been added to @context. A side-effect of
1141 * calling this function is that g_set_prgname() will be
1144 * If the parsing is successful, any parsed arguments are
1145 * removed from the array and @argc and @argv are updated
1146 * accordingly. A '--' option is stripped from @argv
1147 * unless there are unparsed options before and after it,
1148 * or some of the options after it start with '-'. In case
1149 * of an error, @argc and @argv are left unmodified.
1151 * If automatic <option>--help</option> support is enabled
1152 * (see g_option_context_set_help_enabled()), and the
1153 * @argv array contains one of the recognized help options,
1154 * this function will produce help output to stdout and
1155 * call <literal>exit (0)</literal>.
1157 * Return value: %TRUE if the parsing was successful,
1158 * %FALSE if an error occurred
1163 g_option_context_parse (GOptionContext *context,
1171 /* Set program name */
1172 if (argc && argv && *argc)
1176 prgname = g_path_get_basename ((*argv)[0]);
1177 g_set_prgname (prgname);
1182 g_set_prgname ("<unknown>");
1185 /* Call pre-parse hooks */
1186 list = context->groups;
1189 GOptionGroup *group = list->data;
1191 if (group->pre_parse_func)
1193 if (!(* group->pre_parse_func) (context, group,
1194 group->user_data, error))
1201 if (context->main_group && context->main_group->pre_parse_func)
1203 if (!(* context->main_group->pre_parse_func) (context, context->main_group,
1204 context->main_group->user_data, error))
1210 gboolean stop_parsing = FALSE;
1211 gboolean has_unknown = FALSE;
1212 gint separator_pos = 0;
1214 for (i = 1; i < *argc; i++)
1217 gboolean parsed = FALSE;
1219 if ((*argv)[i][0] == '-' && (*argv)[i][1] != '\0' && !stop_parsing)
1221 if ((*argv)[i][1] == '-')
1225 arg = (*argv)[i] + 2;
1227 /* '--' terminates list of arguments */
1231 stop_parsing = TRUE;
1235 /* Handle help options */
1236 if (context->help_enabled)
1238 if (strcmp (arg, "help") == 0)
1239 print_help (context, TRUE, NULL);
1240 else if (strcmp (arg, "help-all") == 0)
1241 print_help (context, FALSE, NULL);
1242 else if (strncmp (arg, "help-", 5) == 0)
1246 list = context->groups;
1250 GOptionGroup *group = list->data;
1252 if (strcmp (arg + 5, group->name) == 0)
1253 print_help (context, FALSE, group);
1260 if (context->main_group &&
1261 !parse_long_option (context, context->main_group, &i, arg,
1262 argc, argv, error, &parsed))
1268 /* Try the groups */
1269 list = context->groups;
1272 GOptionGroup *group = list->data;
1274 if (!parse_long_option (context, group, &i, arg,
1275 argc, argv, error, &parsed))
1287 /* Now look for --<group>-<option> */
1288 dash = strchr (arg, '-');
1291 /* Try the groups */
1292 list = context->groups;
1295 GOptionGroup *group = list->data;
1297 if (strncmp (group->name, arg, dash - arg) == 0)
1299 if (!parse_long_option (context, group, &i, dash + 1,
1300 argc, argv, error, &parsed))
1311 if (context->ignore_unknown)
1319 gboolean *nulled_out = NULL;
1321 arg = (*argv)[i] + 1;
1325 if (context->ignore_unknown)
1326 nulled_out = g_new0 (gboolean, strlen (arg));
1328 for (j = 0; j < strlen (arg); j++)
1330 if (context->help_enabled && arg[j] == '?')
1331 print_help (context, TRUE, NULL);
1335 if (context->main_group &&
1336 !parse_short_option (context, context->main_group,
1338 argc, argv, error, &parsed))
1341 g_free (nulled_out);
1347 /* Try the groups */
1348 list = context->groups;
1351 GOptionGroup *group = list->data;
1353 if (!parse_short_option (context, group, i, &new_i, arg[j],
1354 argc, argv, error, &parsed))
1364 if (context->ignore_unknown)
1367 nulled_out[j] = TRUE;
1376 if (context->ignore_unknown)
1378 gchar *new_arg = NULL;
1381 for (j = 0; j < strlen (arg); j++)
1386 new_arg = g_malloc (strlen (arg) + 1);
1387 new_arg[arg_index++] = arg[j];
1391 new_arg[arg_index] = '\0';
1393 add_pending_null (context, &((*argv)[i]), new_arg);
1397 add_pending_null (context, &((*argv)[i]), NULL);
1405 if (!parsed && !context->ignore_unknown)
1408 G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
1409 _("Unknown option %s"), (*argv)[i]);
1415 /* Collect remaining args */
1416 if (context->main_group &&
1417 !parse_remaining_arg (context, context->main_group, &i,
1418 argc, argv, error, &parsed))
1421 if (!parsed && (has_unknown || (*argv)[i][0] == '-'))
1426 if (separator_pos > 0)
1427 add_pending_null (context, &((*argv)[separator_pos]), NULL);
1431 /* Call post-parse hooks */
1432 list = context->groups;
1435 GOptionGroup *group = list->data;
1437 if (group->post_parse_func)
1439 if (!(* group->post_parse_func) (context, group,
1440 group->user_data, error))
1447 if (context->main_group && context->main_group->post_parse_func)
1449 if (!(* context->main_group->post_parse_func) (context, context->main_group,
1450 context->main_group->user_data, error))
1456 free_pending_nulls (context, TRUE);
1458 for (i = 1; i < *argc; i++)
1460 for (k = i; k < *argc; k++)
1461 if ((*argv)[k] != NULL)
1467 for (j = i + k; j < *argc; j++)
1469 (*argv)[j-k] = (*argv)[j];
1481 /* Call error hooks */
1482 list = context->groups;
1485 GOptionGroup *group = list->data;
1487 if (group->error_func)
1488 (* group->error_func) (context, group,
1489 group->user_data, error);
1494 if (context->main_group && context->main_group->error_func)
1495 (* context->main_group->error_func) (context, context->main_group,
1496 context->main_group->user_data, error);
1498 free_changes_list (context, TRUE);
1499 free_pending_nulls (context, FALSE);
1505 * g_option_group_new:
1506 * @name: the name for the option group, this is used to provide
1507 * help for the options in this group with <option>--help-</option>@name
1508 * @description: a description for this group to be shown in
1509 * <option>--help</option>. This string is translated using the translation
1510 * domain or translation function of the group
1511 * @help_description: a description for the <option>--help-</option>@name option.
1512 * This string is translated using the translation domain or translation function
1514 * @user_data: user data that will be passed to the pre- and post-parse hooks,
1515 * the error hook and to callbacks of %G_OPTION_ARG_CALLBACK options, or %NULL
1516 * @destroy: a function that will be called to free @user_data, or %NULL
1518 * Creates a new #GOptionGroup.
1520 * Return value: a newly created option group. It should be added
1521 * to a #GOptionContext or freed with g_option_group_free().
1526 g_option_group_new (const gchar *name,
1527 const gchar *description,
1528 const gchar *help_description,
1530 GDestroyNotify destroy)
1533 GOptionGroup *group;
1535 group = g_new0 (GOptionGroup, 1);
1536 group->name = g_strdup (name);
1537 group->description = g_strdup (description);
1538 group->help_description = g_strdup (help_description);
1539 group->user_data = user_data;
1540 group->destroy_notify = destroy;
1547 * g_option_group_free:
1548 * @group: a #GOptionGroup
1550 * Frees a #GOptionGroup. Note that you must <emphasis>not</emphasis>
1551 * free groups which have been added to a #GOptionContext.
1556 g_option_group_free (GOptionGroup *group)
1558 g_return_if_fail (group != NULL);
1560 g_free (group->name);
1561 g_free (group->description);
1562 g_free (group->help_description);
1564 g_free (group->entries);
1566 if (group->destroy_notify)
1567 (* group->destroy_notify) (group->user_data);
1569 if (group->translate_notify)
1570 (* group->translate_notify) (group->translate_data);
1577 * g_option_group_add_entries:
1578 * @group: a #GOptionGroup
1579 * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
1581 * Adds the options specified in @entries to @group.
1586 g_option_group_add_entries (GOptionGroup *group,
1587 const GOptionEntry *entries)
1591 g_return_if_fail (entries != NULL);
1593 for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++);
1595 group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
1597 memcpy (group->entries + group->n_entries, entries, sizeof (GOptionEntry) * n_entries);
1599 group->n_entries += n_entries;
1603 * g_option_group_set_parse_hooks:
1604 * @group: a #GOptionGroup
1605 * @pre_parse_func: a function to call before parsing, or %NULL
1606 * @post_parse_func: a function to call after parsing, or %NULL
1608 * Associates two functions with @group which will be called
1609 * from g_option_context_parse() before the first option is parsed
1610 * and after the last option has been parsed, respectively.
1612 * Note that the user data to be passed to @pre_parse_func and
1613 * @post_parse_func can be specified when constructing the group
1614 * with g_option_group_new().
1619 g_option_group_set_parse_hooks (GOptionGroup *group,
1620 GOptionParseFunc pre_parse_func,
1621 GOptionParseFunc post_parse_func)
1623 g_return_if_fail (group != NULL);
1625 group->pre_parse_func = pre_parse_func;
1626 group->post_parse_func = post_parse_func;
1630 * g_option_group_set_error_hook:
1631 * @group: a #GOptionGroup
1632 * @error_func: a function to call when an error occurs
1634 * Associates a function with @group which will be called
1635 * from g_option_context_parse() when an error occurs.
1637 * Note that the user data to be passed to @pre_parse_func and
1638 * @post_parse_func can be specified when constructing the group
1639 * with g_option_group_new().
1644 g_option_group_set_error_hook (GOptionGroup *group,
1645 GOptionErrorFunc error_func)
1647 g_return_if_fail (group != NULL);
1649 group->error_func = error_func;
1654 * g_option_group_set_translate_func:
1655 * @group: a #GOptionGroup
1656 * @func: the #GTranslateFunc, or %NULL
1657 * @data: user data to pass to @func, or %NULL
1658 * @destroy_notify: a function which gets called to free @data, or %NULL
1660 * Sets the function which is used to translate user-visible
1661 * strings, for <option>--help</option> output. Different
1662 * groups can use different #GTranslateFunc<!-- -->s. If @func
1663 * is %NULL, strings are not translated.
1665 * If you are using gettext(), you only need to set the translation
1666 * domain, see g_option_group_set_translation_domain().
1671 g_option_group_set_translate_func (GOptionGroup *group,
1672 GTranslateFunc func,
1674 GDestroyNotify destroy_notify)
1676 g_return_if_fail (group != NULL);
1678 if (group->translate_notify)
1679 group->translate_notify (group->translate_data);
1681 group->translate_func = func;
1682 group->translate_data = data;
1683 group->translate_notify = destroy_notify;
1687 dgettext_swapped (const gchar *msgid,
1688 const gchar *domainname)
1690 return dgettext (domainname, msgid);
1694 * g_option_group_set_translation_domain:
1695 * @group: a #GOptionGroup
1696 * @domain: the domain to use
1698 * A convenience function to use gettext() for translating
1699 * user-visible strings.
1704 g_option_group_set_translation_domain (GOptionGroup *group,
1705 const gchar *domain)
1707 g_return_if_fail (group != NULL);
1709 g_option_group_set_translate_func (group,
1710 (GTranslateFunc)dgettext_swapped,
1715 #define __G_OPTION_C__
1716 #include "galiasdef.c"