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.
32 #define TRANSLATE(group, str) (((group)->translate_func ? (* (group)->translate_func) ((str), (group)->translate_data) : (str)))
58 struct _GOptionContext
62 gchar *parameter_string;
64 gboolean help_enabled;
65 gboolean ignore_unknown;
67 GOptionGroup *main_group;
69 /* We keep a list of change so we can revert them */
72 /* We also keep track of all argv elements that should be NULLed or
82 gchar *help_description;
84 GDestroyNotify destroy_notify;
87 GTranslateFunc translate_func;
88 GDestroyNotify translate_notify;
89 gpointer translate_data;
91 GOptionEntry *entries;
94 GOptionParseFunc pre_parse_func;
95 GOptionParseFunc post_parse_func;
96 GOptionErrorFunc error_func;
99 static void free_changes_list (GOptionContext *context,
101 static void free_pending_nulls (GOptionContext *context,
102 gboolean perform_nulls);
105 g_option_error_quark (void)
110 q = g_quark_from_static_string ("g-option-context-error-quark");
116 g_option_context_new (const gchar *parameter_string)
119 GOptionContext *context;
121 context = g_new0 (GOptionContext, 1);
123 context->parameter_string = g_strdup (parameter_string);
124 context->help_enabled = TRUE;
130 g_option_context_free (GOptionContext *context)
132 g_return_if_fail (context != NULL);
134 g_list_foreach (context->groups, (GFunc)g_option_group_free, NULL);
135 g_list_free (context->groups);
137 if (context->main_group)
138 g_option_group_free (context->main_group);
140 free_changes_list (context, FALSE);
141 free_pending_nulls (context, FALSE);
143 g_free (context->parameter_string);
149 g_option_context_set_help_enabled (GOptionContext *context,
150 gboolean help_enabled)
153 g_return_if_fail (context != NULL);
155 context->help_enabled = help_enabled;
159 g_option_context_get_help_enabled (GOptionContext *context)
161 g_return_val_if_fail (context != NULL, FALSE);
163 return context->help_enabled;
167 g_option_context_set_ignore_unknown_options (GOptionContext *context,
168 gboolean ignore_unknown)
170 g_return_if_fail (context != NULL);
172 context->ignore_unknown = ignore_unknown;
176 g_option_context_get_ignore_unknown_options (GOptionContext *context)
178 g_return_val_if_fail (context != NULL, FALSE);
180 return context->ignore_unknown;
184 g_option_context_add_group (GOptionContext *context,
187 g_return_if_fail (context != NULL);
188 g_return_if_fail (group != NULL);
189 g_return_if_fail (group->name != NULL);
190 g_return_if_fail (group->description != NULL);
191 g_return_if_fail (group->help_description != NULL);
193 context->groups = g_list_prepend (context->groups, group);
197 g_option_context_set_main_group (GOptionContext *context,
200 g_return_if_fail (context != NULL);
201 g_return_if_fail (group != NULL);
203 context->main_group = group;
207 g_option_context_get_main_group (GOptionContext *context)
209 g_return_val_if_fail (context != NULL, NULL);
211 return context->main_group;
215 g_option_context_add_main_entries (GOptionContext *context,
216 const GOptionEntry *entries,
217 const gchar *translation_domain)
219 g_return_if_fail (entries != NULL);
221 if (!context->main_group)
222 context->main_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
224 g_option_group_add_entries (context->main_group, entries);
225 g_option_group_set_translation_domain (context->main_group, translation_domain);
229 print_entry (GOptionGroup *group,
231 const GOptionEntry *entry)
235 if (entry->flags & G_OPTION_FLAG_HIDDEN)
238 str = g_string_new (NULL);
240 if (entry->short_name)
241 g_string_append_printf (str, " -%c, --%s", entry->short_name, entry->long_name);
243 g_string_append_printf (str, " --%s", entry->long_name);
245 if (entry->arg_description)
246 g_string_append_printf (str, "=%s", TRANSLATE (group, entry->arg_description));
248 g_print ("%-*s %s\n", max_length + 4, str->str,
249 entry->description ? TRANSLATE (group, entry->description) : "");
250 g_string_free (str, TRUE);
254 print_help (GOptionContext *context,
259 gint max_length, len;
262 g_print ("%s\n %s %s %s\n\n",
263 _("Usage:"), g_get_prgname(), _("[OPTION...]"),
264 context->parameter_string ? context->parameter_string : "");
266 list = context->groups;
268 max_length = g_utf8_strlen ("--help, -?", -1);
272 len = g_utf8_strlen ("--help-all", -1);
273 max_length = MAX (max_length, len);
278 GOptionGroup *group = list->data;
280 /* First, we check the --help-<groupname> options */
281 len = g_utf8_strlen ("--help-", -1) + g_utf8_strlen (group->name, -1);
282 max_length = MAX (max_length, len);
284 /* Then we go through the entries */
285 for (i = 0; i < group->n_entries; i++)
287 len = g_utf8_strlen (group->entries[i].long_name, -1);
289 if (group->entries[i].short_name)
292 if (group->entries[i].arg != G_OPTION_ARG_NONE &&
293 group->entries[i].arg_description)
294 len += 1 + g_utf8_strlen (TRANSLATE (group, group->entries[i].arg_description), -1);
296 max_length = MAX (max_length, len);
302 /* Add a bit of padding */
305 list = context->groups;
307 g_print ("%s\n --%-*s %s\n",
308 _("Help Options:"), max_length, "help", _("Show help options"));
310 /* We only want --help-all when there are groups */
312 g_print (" --%-*s %s\n", max_length, "help-all", _("Show all help options"));
316 GOptionGroup *group = list->data;
318 g_print (" --help-%-*s %s\n", max_length - 5, group->name, TRANSLATE (group, group->help_description));
327 /* Print a certain group */
329 g_print ("%s\n", TRANSLATE (group, group->description));
330 for (i = 0; i < group->n_entries; i++)
331 print_entry (group, max_length, &group->entries[i]);
336 /* Print all groups */
338 list = context->groups;
342 GOptionGroup *group = list->data;
344 g_print ("%s\n", group->description);
346 for (i = 0; i < group->n_entries; i++)
347 if (!(group->entries[i].flags & G_OPTION_FLAG_IN_MAIN))
348 print_entry (group, max_length, &group->entries[i]);
355 /* Print application options if --help or --help-all has been specified */
356 if (main_help || !group)
358 list = context->groups;
360 g_print ("%s\n", _("Application Options:"));
362 if (context->main_group)
363 for (i = 0; i < context->main_group->n_entries; i++)
364 print_entry (context->main_group, max_length, &context->main_group->entries[i]);
368 GOptionGroup *group = list->data;
370 /* Print main entries from other groups */
371 for (i = 0; i < group->n_entries; i++)
372 if (group->entries[i].flags & G_OPTION_FLAG_IN_MAIN)
373 print_entry (group, max_length, &group->entries[i]);
386 parse_int (const gchar *arg_name,
392 glong tmp = strtol (arg, &end, 0);
396 if (*arg == '\0' || *end != '\0')
399 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
400 _("Cannot parse integer value '%s' for --%s"),
406 if (*result != tmp || errno == ERANGE)
409 G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
410 _("Integer value '%s' for %s out of range"),
419 get_change (GOptionContext *context,
424 Change *change = NULL;
426 for (list = context->changes; list != NULL; list = list->next)
430 if (change->arg_data == arg_data)
436 change = g_new0 (Change, 1);
437 change->arg_type = arg_type;
438 change->arg_data = arg_data;
440 context->changes = g_list_prepend (context->changes, change);
447 add_pending_null (GOptionContext *context,
453 n = g_new0 (PendingNull, 1);
457 context->pending_nulls = g_list_prepend (context->pending_nulls, n);
461 parse_arg (GOptionContext *context,
465 const gchar *option_name,
473 case G_OPTION_ARG_NONE:
475 change = get_change (context, G_OPTION_ARG_NONE,
478 *(gboolean *)entry->arg_data = TRUE;
481 case G_OPTION_ARG_STRING:
485 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
490 change = get_change (context, G_OPTION_ARG_STRING,
492 g_free (change->allocated.str);
494 change->prev.str = *(gchar **)entry->arg_data;
495 change->allocated.str = data;
497 *(gchar **)entry->arg_data = data;
500 case G_OPTION_ARG_STRING_ARRAY:
504 data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
509 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
512 if (change->allocated.array.len == 0)
514 change->prev.array = entry->arg_data;
515 change->allocated.array.data = g_new (gchar *, 2);
518 change->allocated.array.data =
519 g_renew (gchar *, change->allocated.array.data,
520 change->allocated.array.len + 2);
522 change->allocated.array.data[change->allocated.array.len] = data;
523 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
525 change->allocated.array.len ++;
527 *(gchar ***)entry->arg_data = change->allocated.array.data;
532 case G_OPTION_ARG_FILENAME:
536 data = g_strdup (value);
538 change = get_change (context, G_OPTION_ARG_FILENAME,
540 g_free (change->allocated.str);
542 change->prev.str = *(gchar **)entry->arg_data;
543 change->allocated.str = data;
548 case G_OPTION_ARG_FILENAME_ARRAY:
552 data = g_strdup (value);
554 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
557 if (change->allocated.array.len == 0)
559 change->prev.array = entry->arg_data;
560 change->allocated.array.data = g_new (gchar *, 2);
563 change->allocated.array.data =
564 g_renew (gchar *, change->allocated.array.data,
565 change->allocated.array.len + 2);
567 change->allocated.array.data[change->allocated.array.len] = data;
568 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
570 change->allocated.array.len ++;
572 *(gchar ***)entry->arg_data = change->allocated.array.data;
577 case G_OPTION_ARG_INT:
581 if (!parse_int (option_name, value,
586 change = get_change (context, G_OPTION_ARG_INT,
588 change->prev.integer = *(gint *)entry->arg_data;
589 *(gint *)entry->arg_data = data;
592 case G_OPTION_ARG_CALLBACK:
597 tmp = g_locale_to_utf8 (value, -1, NULL, NULL, error);
602 retval = (* (GOptionArgFunc) entry->arg_data) (option_name, tmp, group->user_data, error);
611 g_assert_not_reached ();
618 parse_short_option (GOptionContext *context,
630 for (j = 0; j < group->n_entries; j++)
632 if (arg == group->entries[j].short_name)
634 if (group->entries[j].arg == G_OPTION_ARG_NONE)
636 parse_arg (context, group, &group->entries[j],
645 if (*new_index > index)
647 g_warning ("FIXME: figure out the correct error here");
652 if (index < *argc - 1)
654 value = (*argv)[index + 1];
655 add_pending_null (context, &((*argv)[index + 1]), NULL);
656 *new_index = index + 1;
662 option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
664 if (!parse_arg (context, group, &group->entries[j], value, option_name, error))
666 g_free (option_name);
670 g_free (option_name);
680 parse_long_option (GOptionContext *context,
691 for (j = 0; j < group->n_entries; j++)
696 if (group->entries[j].arg == G_OPTION_ARG_NONE &&
697 strcmp (arg, group->entries[j].long_name) == 0)
699 parse_arg (context, group, &group->entries[j],
702 add_pending_null (context, &((*argv)[*index]), NULL);
707 gint len = strlen (group->entries[j].long_name);
709 if (strncmp (arg, group->entries[j].long_name, len) == 0 &&
710 (arg[len] == '=' || arg[len] == 0))
715 add_pending_null (context, &((*argv)[*index]), NULL);
718 value = arg + len + 1;
719 else if (*index < *argc - 1)
721 value = (*argv)[*index + 1];
722 add_pending_null (context, &((*argv)[*index + 1]), NULL);
728 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
730 if (!parse_arg (context, group, &group->entries[j], value, option_name, error))
732 g_free (option_name);
736 g_free (option_name);
746 free_changes_list (GOptionContext *context,
751 for (list = context->changes; list != NULL; list = list->next)
753 Change *change = list->data;
757 switch (change->arg_type)
759 case G_OPTION_ARG_NONE:
760 *(gboolean *)change->arg_data = change->prev.bool;
762 case G_OPTION_ARG_INT:
763 *(gint *)change->arg_data = change->prev.integer;
765 case G_OPTION_ARG_STRING:
766 case G_OPTION_ARG_FILENAME:
767 g_free (change->allocated.str);
768 *(gchar **)change->arg_data = change->prev.str;
770 case G_OPTION_ARG_STRING_ARRAY:
771 case G_OPTION_ARG_FILENAME_ARRAY:
772 g_strfreev (change->allocated.array.data);
773 *(gchar ***)change->arg_data = change->prev.array;
776 g_assert_not_reached ();
783 g_list_free (context->changes);
784 context->changes = NULL;
788 free_pending_nulls (GOptionContext *context,
789 gboolean perform_nulls)
793 for (list = context->pending_nulls; list != NULL; list = list->next)
795 PendingNull *n = list->data;
801 /* Copy back the short options */
803 strcpy (*n->ptr + 1, n->value);
813 g_list_free (context->pending_nulls);
814 context->pending_nulls = NULL;
818 g_option_context_parse (GOptionContext *context,
826 /* Set program name */
831 prgname = g_path_get_basename ((*argv)[0]);
832 g_set_prgname (prgname);
837 g_set_prgname ("<unknown>");
840 /* Call pre-parse hooks */
841 list = context->groups;
844 GOptionGroup *group = list->data;
846 if (group->pre_parse_func)
848 if (!(* group->pre_parse_func) (context, group,
849 group->user_data, error))
856 if (context->main_group && context->main_group->pre_parse_func)
858 if (!(* context->main_group->pre_parse_func) (context, context->main_group,
859 context->main_group->user_data, error))
865 for (i = 1; i < *argc; i++)
868 gboolean parsed = FALSE;
870 if ((*argv)[i][0] == '-')
872 if ((*argv)[i][1] == '-')
876 arg = (*argv)[i] + 2;
878 /* '--' terminates list of arguments */
881 add_pending_null (context, &((*argv)[i]), NULL);
885 /* Handle help options */
886 if (context->help_enabled)
888 if (strcmp (arg, "help") == 0)
889 print_help (context, TRUE, NULL);
890 else if (strcmp (arg, "help-all") == 0)
891 print_help (context, FALSE, NULL);
892 else if (strncmp (arg, "help-", 5) == 0)
896 list = context->groups;
900 GOptionGroup *group = list->data;
902 if (strcmp (arg + 5, group->name) == 0)
903 print_help (context, FALSE, group);
910 if (context->main_group &&
911 !parse_long_option (context, context->main_group, &i, arg,
912 argc, argv, error, &parsed))
919 list = context->groups;
922 GOptionGroup *group = list->data;
924 if (!parse_long_option (context, group, &i, arg,
925 argc, argv, error, &parsed))
934 if (context->ignore_unknown)
940 gboolean *nulled_out = NULL;
942 arg = (*argv)[i] + 1;
946 if (context->ignore_unknown)
947 nulled_out = g_new0 (gboolean, strlen (arg));
949 for (j = 0; j < strlen (arg); j++)
953 if (context->main_group &&
954 !parse_short_option (context, context->main_group,
956 argc, argv, error, &parsed))
966 list = context->groups;
969 GOptionGroup *group = list->data;
971 if (!parse_short_option (context, group, i, &new_i, arg[j],
972 argc, argv, error, &parsed))
982 if (context->ignore_unknown)
985 nulled_out[j] = TRUE;
994 if (context->ignore_unknown)
996 gchar *new_arg = NULL;
999 for (j = 0; j < strlen (arg); j++)
1004 new_arg = g_malloc (strlen (arg));
1005 new_arg[arg_index++] = arg[j];
1009 new_arg[arg_index] = '\0';
1011 add_pending_null (context, &((*argv)[i]), new_arg);
1015 add_pending_null (context, &((*argv)[i]), NULL);
1020 if (!parsed && !context->ignore_unknown)
1023 G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
1024 _("Unknown option %s"), (*argv)[i]);
1030 /* Call post-parse hooks */
1031 list = context->groups;
1034 GOptionGroup *group = list->data;
1036 if (group->post_parse_func)
1038 if (!(* group->post_parse_func) (context, group,
1039 group->user_data, error))
1046 if (context->main_group && context->main_group->post_parse_func)
1048 if (!(* context->main_group->post_parse_func) (context, context->main_group,
1049 context->main_group->user_data, error))
1053 free_pending_nulls (context, TRUE);
1055 for (i = 1; i < *argc; i++)
1057 for (k = i; k < *argc; k++)
1058 if ((*argv)[k] != NULL)
1064 for (j = i + k; j < *argc; j++)
1065 (*argv)[j-k] = (*argv)[j];
1075 /* Call error hooks */
1076 list = context->groups;
1079 GOptionGroup *group = list->data;
1081 if (group->error_func)
1082 (* group->error_func) (context, group,
1083 group->user_data, error);
1088 if (context->main_group && context->main_group->error_func)
1089 (* context->main_group->error_func) (context, context->main_group,
1090 context->main_group->user_data, error);
1092 free_changes_list (context, TRUE);
1093 free_pending_nulls (context, FALSE);
1100 g_option_group_new (const gchar *name,
1101 const gchar *description,
1102 const gchar *help_description,
1104 GDestroyNotify destroy)
1107 GOptionGroup *group;
1109 group = g_new0 (GOptionGroup, 1);
1110 group->name = g_strdup (name);
1111 group->description = g_strdup (description);
1112 group->help_description = g_strdup (help_description);
1113 group->user_data = user_data;
1114 group->destroy_notify = destroy;
1120 g_option_group_free (GOptionGroup *group)
1122 g_return_if_fail (group != NULL);
1124 g_free (group->name);
1125 g_free (group->description);
1126 g_free (group->help_description);
1128 g_free (group->entries);
1130 if (group->destroy_notify)
1131 (* group->destroy_notify) (group->user_data);
1133 if (group->translate_notify)
1134 (* group->translate_notify) (group->translate_data);
1141 g_option_group_add_entries (GOptionGroup *group,
1142 const GOptionEntry *entries)
1146 g_return_if_fail (entries != NULL);
1148 for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++);
1150 group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
1152 memcpy (group->entries + group->n_entries, entries, sizeof (GOptionEntry) * n_entries);
1154 group->n_entries += n_entries;
1158 g_option_group_set_parse_hooks (GOptionGroup *group,
1159 GOptionParseFunc pre_parse_func,
1160 GOptionParseFunc post_parse_func)
1162 g_return_if_fail (group != NULL);
1164 group->pre_parse_func = pre_parse_func;
1165 group->post_parse_func = post_parse_func;
1169 g_option_group_set_error_hook (GOptionGroup *group,
1170 GOptionErrorFunc error_func)
1172 g_return_if_fail (group != NULL);
1174 group->error_func = error_func;
1179 g_option_group_set_translate_func (GOptionGroup *group,
1180 GTranslateFunc func,
1182 GDestroyNotify notify)
1184 g_return_if_fail (group != NULL);
1186 if (group->translate_notify)
1187 group->translate_notify (group->translate_data);
1189 group->translate_func = func;
1190 group->translate_data = data;
1191 group->translate_notify = notify;
1195 dgettext_swapped (const gchar *msgid,
1196 const gchar *domainname)
1198 return dgettext (domainname, msgid);
1202 g_option_group_set_translation_domain (GOptionGroup *group,
1203 const gchar *domain)
1205 g_return_if_fail (group != NULL);
1207 g_option_group_set_translate_func (group,
1208 (GTranslateFunc)dgettext_swapped,