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;
545 *(gchar **)entry->arg_data = data;
549 case G_OPTION_ARG_FILENAME_ARRAY:
553 data = g_strdup (value);
555 change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
558 if (change->allocated.array.len == 0)
560 change->prev.array = entry->arg_data;
561 change->allocated.array.data = g_new (gchar *, 2);
564 change->allocated.array.data =
565 g_renew (gchar *, change->allocated.array.data,
566 change->allocated.array.len + 2);
568 change->allocated.array.data[change->allocated.array.len] = data;
569 change->allocated.array.data[change->allocated.array.len + 1] = NULL;
571 change->allocated.array.len ++;
573 *(gchar ***)entry->arg_data = change->allocated.array.data;
578 case G_OPTION_ARG_INT:
582 if (!parse_int (option_name, value,
587 change = get_change (context, G_OPTION_ARG_INT,
589 change->prev.integer = *(gint *)entry->arg_data;
590 *(gint *)entry->arg_data = data;
593 case G_OPTION_ARG_CALLBACK:
598 tmp = g_locale_to_utf8 (value, -1, NULL, NULL, error);
603 retval = (* (GOptionArgFunc) entry->arg_data) (option_name, tmp, group->user_data, error);
612 g_assert_not_reached ();
619 parse_short_option (GOptionContext *context,
631 for (j = 0; j < group->n_entries; j++)
633 if (arg == group->entries[j].short_name)
635 if (group->entries[j].arg == G_OPTION_ARG_NONE)
637 parse_arg (context, group, &group->entries[j],
646 if (*new_index > index)
648 g_warning ("FIXME: figure out the correct error here");
653 if (index < *argc - 1)
655 value = (*argv)[index + 1];
656 add_pending_null (context, &((*argv)[index + 1]), NULL);
657 *new_index = index + 1;
663 option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
665 if (!parse_arg (context, group, &group->entries[j], value, option_name, error))
667 g_free (option_name);
671 g_free (option_name);
681 parse_long_option (GOptionContext *context,
692 for (j = 0; j < group->n_entries; j++)
697 if (group->entries[j].arg == G_OPTION_ARG_NONE &&
698 strcmp (arg, group->entries[j].long_name) == 0)
700 parse_arg (context, group, &group->entries[j],
703 add_pending_null (context, &((*argv)[*index]), NULL);
708 gint len = strlen (group->entries[j].long_name);
710 if (strncmp (arg, group->entries[j].long_name, len) == 0 &&
711 (arg[len] == '=' || arg[len] == 0))
716 add_pending_null (context, &((*argv)[*index]), NULL);
719 value = arg + len + 1;
720 else if (*index < *argc - 1)
722 value = (*argv)[*index + 1];
723 add_pending_null (context, &((*argv)[*index + 1]), NULL);
729 option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
731 if (!parse_arg (context, group, &group->entries[j], value, option_name, error))
733 g_free (option_name);
737 g_free (option_name);
747 free_changes_list (GOptionContext *context,
752 for (list = context->changes; list != NULL; list = list->next)
754 Change *change = list->data;
758 switch (change->arg_type)
760 case G_OPTION_ARG_NONE:
761 *(gboolean *)change->arg_data = change->prev.bool;
763 case G_OPTION_ARG_INT:
764 *(gint *)change->arg_data = change->prev.integer;
766 case G_OPTION_ARG_STRING:
767 case G_OPTION_ARG_FILENAME:
768 g_free (change->allocated.str);
769 *(gchar **)change->arg_data = change->prev.str;
771 case G_OPTION_ARG_STRING_ARRAY:
772 case G_OPTION_ARG_FILENAME_ARRAY:
773 g_strfreev (change->allocated.array.data);
774 *(gchar ***)change->arg_data = change->prev.array;
777 g_assert_not_reached ();
784 g_list_free (context->changes);
785 context->changes = NULL;
789 free_pending_nulls (GOptionContext *context,
790 gboolean perform_nulls)
794 for (list = context->pending_nulls; list != NULL; list = list->next)
796 PendingNull *n = list->data;
802 /* Copy back the short options */
804 strcpy (*n->ptr + 1, n->value);
814 g_list_free (context->pending_nulls);
815 context->pending_nulls = NULL;
819 g_option_context_parse (GOptionContext *context,
827 /* Set program name */
832 prgname = g_path_get_basename ((*argv)[0]);
833 g_set_prgname (prgname);
838 g_set_prgname ("<unknown>");
841 /* Call pre-parse hooks */
842 list = context->groups;
845 GOptionGroup *group = list->data;
847 if (group->pre_parse_func)
849 if (!(* group->pre_parse_func) (context, group,
850 group->user_data, error))
857 if (context->main_group && context->main_group->pre_parse_func)
859 if (!(* context->main_group->pre_parse_func) (context, context->main_group,
860 context->main_group->user_data, error))
866 for (i = 1; i < *argc; i++)
869 gboolean parsed = FALSE;
871 if ((*argv)[i][0] == '-')
873 if ((*argv)[i][1] == '-')
877 arg = (*argv)[i] + 2;
879 /* '--' terminates list of arguments */
882 add_pending_null (context, &((*argv)[i]), NULL);
886 /* Handle help options */
887 if (context->help_enabled)
889 if (strcmp (arg, "help") == 0)
890 print_help (context, TRUE, NULL);
891 else if (strcmp (arg, "help-all") == 0)
892 print_help (context, FALSE, NULL);
893 else if (strncmp (arg, "help-", 5) == 0)
897 list = context->groups;
901 GOptionGroup *group = list->data;
903 if (strcmp (arg + 5, group->name) == 0)
904 print_help (context, FALSE, group);
911 if (context->main_group &&
912 !parse_long_option (context, context->main_group, &i, arg,
913 argc, argv, error, &parsed))
920 list = context->groups;
923 GOptionGroup *group = list->data;
925 if (!parse_long_option (context, group, &i, arg,
926 argc, argv, error, &parsed))
935 if (context->ignore_unknown)
941 gboolean *nulled_out = NULL;
943 arg = (*argv)[i] + 1;
947 if (context->ignore_unknown)
948 nulled_out = g_new0 (gboolean, strlen (arg));
950 for (j = 0; j < strlen (arg); j++)
954 if (context->main_group &&
955 !parse_short_option (context, context->main_group,
957 argc, argv, error, &parsed))
967 list = context->groups;
970 GOptionGroup *group = list->data;
972 if (!parse_short_option (context, group, i, &new_i, arg[j],
973 argc, argv, error, &parsed))
983 if (context->ignore_unknown)
986 nulled_out[j] = TRUE;
995 if (context->ignore_unknown)
997 gchar *new_arg = NULL;
1000 for (j = 0; j < strlen (arg); j++)
1005 new_arg = g_malloc (strlen (arg));
1006 new_arg[arg_index++] = arg[j];
1010 new_arg[arg_index] = '\0';
1012 add_pending_null (context, &((*argv)[i]), new_arg);
1016 add_pending_null (context, &((*argv)[i]), NULL);
1021 if (!parsed && !context->ignore_unknown)
1024 G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
1025 _("Unknown option %s"), (*argv)[i]);
1031 /* Call post-parse hooks */
1032 list = context->groups;
1035 GOptionGroup *group = list->data;
1037 if (group->post_parse_func)
1039 if (!(* group->post_parse_func) (context, group,
1040 group->user_data, error))
1047 if (context->main_group && context->main_group->post_parse_func)
1049 if (!(* context->main_group->post_parse_func) (context, context->main_group,
1050 context->main_group->user_data, error))
1054 free_pending_nulls (context, TRUE);
1056 for (i = 1; i < *argc; i++)
1058 for (k = i; k < *argc; k++)
1059 if ((*argv)[k] != NULL)
1065 for (j = i + k; j < *argc; j++)
1066 (*argv)[j-k] = (*argv)[j];
1076 /* Call error hooks */
1077 list = context->groups;
1080 GOptionGroup *group = list->data;
1082 if (group->error_func)
1083 (* group->error_func) (context, group,
1084 group->user_data, error);
1089 if (context->main_group && context->main_group->error_func)
1090 (* context->main_group->error_func) (context, context->main_group,
1091 context->main_group->user_data, error);
1093 free_changes_list (context, TRUE);
1094 free_pending_nulls (context, FALSE);
1101 g_option_group_new (const gchar *name,
1102 const gchar *description,
1103 const gchar *help_description,
1105 GDestroyNotify destroy)
1108 GOptionGroup *group;
1110 group = g_new0 (GOptionGroup, 1);
1111 group->name = g_strdup (name);
1112 group->description = g_strdup (description);
1113 group->help_description = g_strdup (help_description);
1114 group->user_data = user_data;
1115 group->destroy_notify = destroy;
1121 g_option_group_free (GOptionGroup *group)
1123 g_return_if_fail (group != NULL);
1125 g_free (group->name);
1126 g_free (group->description);
1127 g_free (group->help_description);
1129 g_free (group->entries);
1131 if (group->destroy_notify)
1132 (* group->destroy_notify) (group->user_data);
1134 if (group->translate_notify)
1135 (* group->translate_notify) (group->translate_data);
1142 g_option_group_add_entries (GOptionGroup *group,
1143 const GOptionEntry *entries)
1147 g_return_if_fail (entries != NULL);
1149 for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++);
1151 group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
1153 memcpy (group->entries + group->n_entries, entries, sizeof (GOptionEntry) * n_entries);
1155 group->n_entries += n_entries;
1159 g_option_group_set_parse_hooks (GOptionGroup *group,
1160 GOptionParseFunc pre_parse_func,
1161 GOptionParseFunc post_parse_func)
1163 g_return_if_fail (group != NULL);
1165 group->pre_parse_func = pre_parse_func;
1166 group->post_parse_func = post_parse_func;
1170 g_option_group_set_error_hook (GOptionGroup *group,
1171 GOptionErrorFunc error_func)
1173 g_return_if_fail (group != NULL);
1175 group->error_func = error_func;
1180 g_option_group_set_translate_func (GOptionGroup *group,
1181 GTranslateFunc func,
1183 GDestroyNotify notify)
1185 g_return_if_fail (group != NULL);
1187 if (group->translate_notify)
1188 group->translate_notify (group->translate_data);
1190 group->translate_func = func;
1191 group->translate_data = data;
1192 group->translate_notify = notify;
1196 dgettext_swapped (const gchar *msgid,
1197 const gchar *domainname)
1199 return dgettext (domainname, msgid);
1203 g_option_group_set_translation_domain (GOptionGroup *group,
1204 const gchar *domain)
1206 g_return_if_fail (group != NULL);
1208 g_option_group_set_translate_func (group,
1209 (GTranslateFunc)dgettext_swapped,