Warn if a short name is not acceptable.
[platform/upstream/glib.git] / glib / goption.c
1 /* goption.c - Option parser
2  *
3  *  Copyright (C) 1999, 2003 Red Hat Software
4  *  Copyright (C) 2004       Anders Carlsson <andersca@gnome.org>
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 #include "config.h"
23
24 #include "goption.h"
25 #include "glib.h"
26 #include "gi18n.h"
27
28 #include "galias.h"
29
30 #include <string.h>
31 #include <stdlib.h>
32 #include <errno.h>
33
34 #define TRANSLATE(group, str) (((group)->translate_func ? (* (group)->translate_func) ((str), (group)->translate_data) : (str)))
35
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)))
39
40 #define OPTIONAL_ARG(entry) ((entry)->arg == G_OPTION_ARG_CALLBACK &&  \
41                        (entry)->flags & G_OPTION_FLAG_OPTIONAL_ARG)
42
43 typedef struct 
44 {
45   GOptionArg arg_type;
46   gpointer arg_data;  
47   union 
48   {
49     gboolean bool;
50     gint integer;
51     gchar *str;
52     gchar **array;
53   } prev;
54   union 
55   {
56     gchar *str;
57     struct 
58     {
59       gint len;
60       gchar **data;
61     } array;
62   } allocated;
63 } Change;
64
65 typedef struct
66 {
67   gchar **ptr;
68   gchar *value;
69 } PendingNull;
70
71 struct _GOptionContext
72 {
73   GList *groups;
74
75   gchar *parameter_string;
76
77   gboolean help_enabled;
78   gboolean ignore_unknown;
79   
80   GOptionGroup *main_group;
81
82   /* We keep a list of change so we can revert them */
83   GList *changes;
84   
85   /* We also keep track of all argv elements that should be NULLed or
86    * modified.
87    */
88   GList *pending_nulls;
89 };
90
91 struct _GOptionGroup
92 {
93   gchar *name;
94   gchar *description;
95   gchar *help_description;
96
97   GDestroyNotify  destroy_notify;
98   gpointer        user_data;
99
100   GTranslateFunc  translate_func;
101   GDestroyNotify  translate_notify;
102   gpointer        translate_data;
103
104   GOptionEntry *entries;
105   gint         n_entries;
106
107   GOptionParseFunc pre_parse_func;
108   GOptionParseFunc post_parse_func;
109   GOptionErrorFunc error_func;
110 };
111
112 static void free_changes_list (GOptionContext *context,
113                                gboolean        revert);
114 static void free_pending_nulls (GOptionContext *context,
115                                 gboolean        perform_nulls);
116
117 GQuark
118 g_option_error_quark (void)
119 {
120   static GQuark q = 0;
121   
122   if (q == 0)
123     q = g_quark_from_static_string ("g-option-context-error-quark");
124
125   return q;
126 }
127
128 /**
129  * g_option_context_new:
130  * @parameter_string: a string which is displayed in
131  *    the first line of <option>--help</option> output, after 
132  *    <literal><replaceable>programname</replaceable> [OPTION...]</literal>
133  *
134  * Creates a new option context. 
135  *
136  * Returns: a newly created #GOptionContext, which must be
137  *    freed with g_option_context_free() after use.
138  *
139  * Since: 2.6
140  */
141 GOptionContext *
142 g_option_context_new (const gchar *parameter_string)
143
144 {
145   GOptionContext *context;
146
147   context = g_new0 (GOptionContext, 1);
148
149   context->parameter_string = g_strdup (parameter_string);
150   context->help_enabled = TRUE;
151   context->ignore_unknown = FALSE;
152
153   return context;
154 }
155
156 /**
157  * g_option_context_free:
158  * @context: a #GOptionContext 
159  *
160  * Frees context and all the groups which have been 
161  * added to it.
162  *
163  * Since: 2.6
164  */
165 void g_option_context_free (GOptionContext *context) 
166 {
167   g_return_if_fail (context != NULL);
168
169   g_list_foreach (context->groups, (GFunc)g_option_group_free, NULL);
170   g_list_free (context->groups);
171
172   if (context->main_group) 
173     g_option_group_free (context->main_group);
174
175   free_changes_list (context, FALSE);
176   free_pending_nulls (context, FALSE);
177   
178   g_free (context->parameter_string);
179   
180   g_free (context);
181 }
182
183
184 /**
185  * g_option_context_set_help_enabled:
186  * @context: a #GOptionContext
187  * @help_enabled: %TRUE to enable <option>--help</option>, %FALSE to disable it
188  *
189  * Enables or disables automatic generation of <option>--help</option> 
190  * output. By default, g_option_context_parse() recognizes
191  * <option>--help</option>, <option>-?</option>, <option>--help-all</option>
192  * and <option>--help-</option><replaceable>groupname</replaceable> and creates
193  * suitable output to stdout. 
194  *
195  * Since: 2.6
196  */
197 void g_option_context_set_help_enabled (GOptionContext *context,
198                                         gboolean        help_enabled)
199
200 {
201   g_return_if_fail (context != NULL);
202
203   context->help_enabled = help_enabled;
204 }
205
206 /**
207  * g_option_context_get_help_enabled:
208  * @context: a #GOptionContext
209  * 
210  * Returns whether automatic <option>--help</option> generation
211  * is turned on for @context. See g_option_context_set_help_enabled().
212  * 
213  * Returns: %TRUE if automatic help generation is turned on.
214  *
215  * Since: 2.6
216  */
217 gboolean 
218 g_option_context_get_help_enabled (GOptionContext *context) 
219 {
220   g_return_val_if_fail (context != NULL, FALSE);
221   
222   return context->help_enabled;
223 }
224
225 /**
226  * g_option_context_set_ignore_unknown_options:
227  * @context: a #GOptionContext
228  * @ignore_unknown: %TRUE to ignore unknown options, %FALSE to produce
229  *    an error when unknown options are met
230  * 
231  * Sets whether to ignore unknown options or not. If an argument is 
232  * ignored, it is left in the @argv array after parsing. By default, 
233  * g_option_context_parse() treats unknown options as error.
234  * 
235  * This setting does not affect non-option arguments (i.e. arguments 
236  * which don't start with a dash). But note that GOption cannot reliably
237  * determine whether a non-option belongs to a preceding unknown option.
238  *
239  * Since: 2.6
240  **/
241 void
242 g_option_context_set_ignore_unknown_options (GOptionContext *context,
243                                              gboolean        ignore_unknown)
244 {
245   g_return_if_fail (context != NULL);
246
247   context->ignore_unknown = ignore_unknown;
248 }
249
250 /**
251  * g_option_context_get_ignore_unknown_options:
252  * @context: a #GOptionContext
253  * 
254  * Returns whether unknown options are ignored or not. See
255  * g_option_context_set_ignore_unknown_options().
256  * 
257  * Returns: %TRUE if unknown options are ignored.
258  * 
259  * Since: 2.6
260  **/
261 gboolean
262 g_option_context_get_ignore_unknown_options (GOptionContext *context)
263 {
264   g_return_val_if_fail (context != NULL, FALSE);
265
266   return context->ignore_unknown;
267 }
268
269 /**
270  * g_option_context_add_group:
271  * @context: a #GOptionContext
272  * @group: the group to add
273  * 
274  * Adds a #GOptionGroup to the @context, so that parsing with @context
275  * will recognize the options in the group. Note that the group will
276  * be freed together with the context when g_option_context_free() is
277  * called, so you must not free the group yourself after adding it
278  * to a context.
279  *
280  * Since: 2.6
281  **/
282 void
283 g_option_context_add_group (GOptionContext *context,
284                             GOptionGroup   *group)
285 {
286   GList *list;
287
288   g_return_if_fail (context != NULL);
289   g_return_if_fail (group != NULL);
290   g_return_if_fail (group->name != NULL);
291   g_return_if_fail (group->description != NULL);
292   g_return_if_fail (group->help_description != NULL);
293
294   for (list = context->groups; list; list = list->next)
295     {
296       GOptionGroup *g = (GOptionGroup *)list->data;
297
298       if ((group->name == NULL && g->name == NULL) ||
299           (group->name && g->name && strcmp (group->name, g->name) == 0))
300         g_warning ("A group named \"%s\" is already part of this GOptionContext", 
301                    group->name);
302     }
303
304   context->groups = g_list_append (context->groups, group);
305 }
306
307 /**
308  * g_option_context_set_main_group:
309  * @context: a #GOptionContext
310  * @group: the group to set as main group
311  * 
312  * Sets a #GOptionGroup as main group of the @context. 
313  * This has the same effect as calling g_option_context_add_group(), 
314  * the only difference is that the options in the main group are 
315  * treated differently when generating <option>--help</option> output.
316  *
317  * Since: 2.6
318  **/
319 void
320 g_option_context_set_main_group (GOptionContext *context,
321                                  GOptionGroup   *group)
322 {
323   g_return_if_fail (context != NULL);
324   g_return_if_fail (group != NULL);
325
326   if (context->main_group)
327     {
328       g_warning ("This GOptionContext already has a main group");
329
330       return;
331     }
332   
333   context->main_group = group;
334 }
335
336 /**
337  * g_option_context_get_main_group:
338  * @context: a #GOptionContext
339  * 
340  * Returns a pointer to the main group of @context.
341  * 
342  * Return value: the main group of @context, or %NULL if @context doesn't
343  *  have a main group. Note that group belongs to @context and should
344  *  not be modified or freed.
345  *
346  * Since: 2.6
347  **/
348 GOptionGroup *
349 g_option_context_get_main_group (GOptionContext *context)
350 {
351   g_return_val_if_fail (context != NULL, NULL);
352
353   return context->main_group;
354 }
355
356 /**
357  * g_option_context_add_main_entries:
358  * @context: a #GOptionContext
359  * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
360  * @translation_domain: a translation domain to use for translating
361  *    the <option>--help</option> output for the options in @entries
362  *    with gettext(), or %NULL
363  * 
364  * A convenience function which creates a main group if it doesn't 
365  * exist, adds the @entries to it and sets the translation domain.
366  * 
367  * Since: 2.6
368  **/
369 void
370 g_option_context_add_main_entries (GOptionContext      *context,
371                                    const GOptionEntry  *entries,
372                                    const gchar         *translation_domain)
373 {
374   g_return_if_fail (entries != NULL);
375
376   if (!context->main_group)
377     context->main_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
378   
379   g_option_group_add_entries (context->main_group, entries);
380   g_option_group_set_translation_domain (context->main_group, translation_domain);
381 }
382
383 static gint
384 calculate_max_length (GOptionGroup *group)
385 {
386   GOptionEntry *entry;
387   gint i, len, max_length;
388
389   max_length = 0;
390
391   for (i = 0; i < group->n_entries; i++)
392     {
393       entry = &group->entries[i];
394
395       if (entry->flags & G_OPTION_FLAG_HIDDEN)
396         continue;
397
398       len = g_utf8_strlen (entry->long_name, -1);
399       
400       if (entry->short_name)
401         len += 4;
402       
403       if (!NO_ARG (entry) && entry->arg_description)
404         len += 1 + g_utf8_strlen (TRANSLATE (group, entry->arg_description), -1);
405       
406       max_length = MAX (max_length, len);
407     }
408
409   return max_length;
410 }
411
412 static void
413 print_entry (GOptionGroup       *group,
414              gint                max_length,
415              const GOptionEntry *entry)
416 {
417   GString *str;
418
419   if (entry->flags & G_OPTION_FLAG_HIDDEN)
420     return;
421
422   if (entry->long_name[0] == 0)
423     return;
424
425   str = g_string_new (NULL);
426   
427   if (entry->short_name)
428     g_string_append_printf (str, "  -%c, --%s", entry->short_name, entry->long_name);
429   else
430     g_string_append_printf (str, "  --%s", entry->long_name);
431   
432   if (entry->arg_description)
433     g_string_append_printf (str, "=%s", TRANSLATE (group, entry->arg_description));
434   
435   g_print ("%-*s %s\n", max_length + 4, str->str,
436            entry->description ? TRANSLATE (group, entry->description) : "");
437   g_string_free (str, TRUE);  
438 }
439
440 static void
441 print_help (GOptionContext *context,
442             gboolean        main_help,
443             GOptionGroup   *group) 
444 {
445   GList *list;
446   gint max_length, len;
447   gint i;
448   GOptionEntry *entry;
449   GHashTable *shadow_map;
450   gboolean seen[256];
451   const gchar *rest_description;
452   
453   rest_description = NULL;
454   if (context->main_group)
455     {
456       for (i = 0; i < context->main_group->n_entries; i++)
457         {
458           entry = &context->main_group->entries[i];
459           if (entry->long_name[0] == 0)
460             {
461               rest_description = entry->arg_description;
462               break;
463             }
464         }
465     }
466   
467   g_print ("%s\n  %s %s%s%s%s%s\n\n", 
468            _("Usage:"), g_get_prgname(), _("[OPTION...]"),
469            rest_description ? " " : "",
470            rest_description ? rest_description : "",
471            context->parameter_string ? " " : "",
472            context->parameter_string ? context->parameter_string : "");
473
474   memset (seen, 0, sizeof (gboolean) * 256);
475   shadow_map = g_hash_table_new (g_str_hash, g_str_equal);
476
477   if (context->main_group)
478     {
479       for (i = 0; i < context->main_group->n_entries; i++)
480         {
481           entry = &context->main_group->entries[i];
482           g_hash_table_insert (shadow_map, 
483                                (gpointer)entry->long_name, 
484                                entry);
485           
486           if (seen[(guchar)entry->short_name])
487             entry->short_name = 0;
488           else
489             seen[(guchar)entry->short_name] = TRUE;
490         }
491     }
492
493   list = context->groups;
494   while (list != NULL)
495     {
496       GOptionGroup *group = list->data;
497       for (i = 0; i < group->n_entries; i++)
498         {
499           entry = &group->entries[i];
500           if (g_hash_table_lookup (shadow_map, entry->long_name) && 
501               !(entry->flags && G_OPTION_FLAG_NOALIAS))
502             entry->long_name = g_strdup_printf ("%s-%s", group->name, entry->long_name);
503           else  
504             g_hash_table_insert (shadow_map, (gpointer)entry->long_name, entry);
505
506           if (seen[(guchar)entry->short_name] && 
507               !(entry->flags && G_OPTION_FLAG_NOALIAS))
508             entry->short_name = 0;
509           else
510             seen[(guchar)entry->short_name] = TRUE;
511         }
512       list = list->next;
513     }
514
515   g_hash_table_destroy (shadow_map);
516
517   list = context->groups;
518
519   max_length = g_utf8_strlen ("-?, --help", -1);
520
521   if (list)
522     {
523       len = g_utf8_strlen ("--help-all", -1);
524       max_length = MAX (max_length, len);
525     }
526
527   if (context->main_group)
528     {
529       len = calculate_max_length (context->main_group);
530       max_length = MAX (max_length, len);
531     }
532
533   while (list != NULL)
534     {
535       GOptionGroup *group = list->data;
536       
537       /* First, we check the --help-<groupname> options */
538       len = g_utf8_strlen ("--help-", -1) + g_utf8_strlen (group->name, -1);
539       max_length = MAX (max_length, len);
540
541       /* Then we go through the entries */
542       len = calculate_max_length (group);
543       max_length = MAX (max_length, len);
544       
545       list = list->next;
546     }
547
548   /* Add a bit of padding */
549   max_length += 4;
550
551   if (!group)
552     {
553       list = context->groups;
554       
555       g_print ("%s\n  -%c, --%-*s %s\n", 
556                _("Help Options:"), '?', max_length - 4, "help", 
557                _("Show help options"));
558       
559       /* We only want --help-all when there are groups */
560       if (list)
561         g_print ("  --%-*s %s\n", max_length, "help-all", 
562                  _("Show all help options"));
563       
564       while (list)
565         {
566           GOptionGroup *group = list->data;
567           
568           g_print ("  --help-%-*s %s\n", max_length - 5, group->name, 
569                    TRANSLATE (group, group->help_description));
570           
571           list = list->next;
572         }
573
574       g_print ("\n");
575     }
576
577   if (group)
578     {
579       /* Print a certain group */
580       
581       g_print ("%s\n", TRANSLATE (group, group->description));
582       for (i = 0; i < group->n_entries; i++)
583         print_entry (group, max_length, &group->entries[i]);
584       g_print ("\n");
585     }
586   else if (!main_help)
587     {
588       /* Print all groups */
589
590       list = context->groups;
591
592       while (list)
593         {
594           GOptionGroup *group = list->data;
595
596           g_print ("%s\n", group->description);
597
598           for (i = 0; i < group->n_entries; i++)
599             if (!(group->entries[i].flags & G_OPTION_FLAG_IN_MAIN))
600               print_entry (group, max_length, &group->entries[i]);
601           
602           g_print ("\n");
603           list = list->next;
604         }
605     }
606   
607   /* Print application options if --help or --help-all has been specified */
608   if (main_help || !group)
609     {
610       list = context->groups;
611
612       g_print ("%s\n", _("Application Options:"));
613
614       if (context->main_group)
615         for (i = 0; i < context->main_group->n_entries; i++) 
616           print_entry (context->main_group, max_length, 
617                        &context->main_group->entries[i]);
618
619       while (list != NULL)
620         {
621           GOptionGroup *group = list->data;
622
623           /* Print main entries from other groups */
624           for (i = 0; i < group->n_entries; i++)
625             if (group->entries[i].flags & G_OPTION_FLAG_IN_MAIN)
626               print_entry (group, max_length, &group->entries[i]);
627           
628           list = list->next;
629         }
630
631       g_print ("\n");
632     }
633   
634   exit (0);
635 }
636
637 static gboolean
638 parse_int (const gchar *arg_name,
639            const gchar *arg,
640            gint        *result,
641            GError     **error)
642 {
643   gchar *end;
644   glong tmp;
645
646   errno = 0;
647   tmp = strtol (arg, &end, 0);
648   
649   if (*arg == '\0' || *end != '\0')
650     {
651       g_set_error (error,
652                    G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
653                    _("Cannot parse integer value '%s' for %s"),
654                    arg, arg_name);
655       return FALSE;
656     }
657
658   *result = tmp;
659   if (*result != tmp || errno == ERANGE)
660     {
661       g_set_error (error,
662                    G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
663                    _("Integer value '%s' for %s out of range"),
664                    arg, arg_name);
665       return FALSE;
666     }
667
668   return TRUE;
669 }
670
671 static Change *
672 get_change (GOptionContext *context,
673             GOptionArg      arg_type,
674             gpointer        arg_data)
675 {
676   GList *list;
677   Change *change = NULL;
678   
679   for (list = context->changes; list != NULL; list = list->next)
680     {
681       change = list->data;
682
683       if (change->arg_data == arg_data)
684         goto found;
685     }
686
687   change = g_new0 (Change, 1);
688   change->arg_type = arg_type;
689   change->arg_data = arg_data;
690   
691   context->changes = g_list_prepend (context->changes, change);
692   
693  found:
694
695   return change;
696 }
697
698 static void
699 add_pending_null (GOptionContext *context,
700                   gchar         **ptr,
701                   gchar          *value)
702 {
703   PendingNull *n;
704
705   n = g_new0 (PendingNull, 1);
706   n->ptr = ptr;
707   n->value = value;
708
709   context->pending_nulls = g_list_prepend (context->pending_nulls, n);
710 }
711                   
712 static gboolean
713 parse_arg (GOptionContext *context,
714            GOptionGroup   *group,
715            GOptionEntry   *entry,
716            const gchar    *value,
717            const gchar    *option_name,
718            GError        **error)
719      
720 {
721   Change *change;
722   
723   switch (entry->arg)
724     {
725     case G_OPTION_ARG_NONE:
726       {
727         change = get_change (context, G_OPTION_ARG_NONE,
728                              entry->arg_data);
729
730         *(gboolean *)entry->arg_data = !(entry->flags & G_OPTION_FLAG_REVERSE);
731         break;
732       }      
733     case G_OPTION_ARG_STRING:
734       {
735         gchar *data;
736         
737         data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
738
739         if (!data)
740           return FALSE;
741
742         change = get_change (context, G_OPTION_ARG_STRING,
743                              entry->arg_data);
744         g_free (change->allocated.str);
745         
746         change->prev.str = *(gchar **)entry->arg_data;
747         change->allocated.str = data;
748         
749         *(gchar **)entry->arg_data = data;
750         break;
751       }
752     case G_OPTION_ARG_STRING_ARRAY:
753       {
754         gchar *data;
755
756         data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
757
758         if (!data)
759           return FALSE;
760
761         change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
762                              entry->arg_data);
763
764         if (change->allocated.array.len == 0)
765           {
766             change->prev.array = *(gchar ***)entry->arg_data;
767             change->allocated.array.data = g_new (gchar *, 2);
768           }
769         else
770           change->allocated.array.data =
771             g_renew (gchar *, change->allocated.array.data,
772                      change->allocated.array.len + 2);
773
774         change->allocated.array.data[change->allocated.array.len] = data;
775         change->allocated.array.data[change->allocated.array.len + 1] = NULL;
776
777         change->allocated.array.len ++;
778
779         *(gchar ***)entry->arg_data = change->allocated.array.data;
780
781         break;
782       }
783       
784     case G_OPTION_ARG_FILENAME:
785       {
786         gchar *data;
787
788 #ifdef G_OS_WIN32
789         data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
790         
791         if (!data)
792           return FALSE;
793 #else
794         data = g_strdup (value);
795 #endif
796         change = get_change (context, G_OPTION_ARG_FILENAME,
797                              entry->arg_data);
798         g_free (change->allocated.str);
799         
800         change->prev.str = *(gchar **)entry->arg_data;
801         change->allocated.str = data;
802
803         *(gchar **)entry->arg_data = data;
804         break;
805       }
806
807     case G_OPTION_ARG_FILENAME_ARRAY:
808       {
809         gchar *data;
810         
811 #ifdef G_OS_WIN32
812         data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
813         
814         if (!data)
815           return FALSE;
816 #else
817         data = g_strdup (value);
818 #endif
819         change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
820                              entry->arg_data);
821
822         if (change->allocated.array.len == 0)
823           {
824             change->prev.array = *(gchar ***)entry->arg_data;
825             change->allocated.array.data = g_new (gchar *, 2);
826           }
827         else
828           change->allocated.array.data =
829             g_renew (gchar *, change->allocated.array.data,
830                      change->allocated.array.len + 2);
831
832         change->allocated.array.data[change->allocated.array.len] = data;
833         change->allocated.array.data[change->allocated.array.len + 1] = NULL;
834
835         change->allocated.array.len ++;
836
837         *(gchar ***)entry->arg_data = change->allocated.array.data;
838
839         break;
840       }
841       
842     case G_OPTION_ARG_INT:
843       {
844         gint data;
845
846         if (!parse_int (option_name, value,
847                         &data,
848                         error))
849           return FALSE;
850
851         change = get_change (context, G_OPTION_ARG_INT,
852                              entry->arg_data);
853         change->prev.integer = *(gint *)entry->arg_data;
854         *(gint *)entry->arg_data = data;
855         break;
856       }
857     case G_OPTION_ARG_CALLBACK:
858       {
859         gchar *data;
860         gboolean retval;
861
862         if (!value && entry->flags & G_OPTION_FLAG_OPTIONAL_ARG)
863           data = NULL;
864         else if (entry->flags & G_OPTION_FLAG_NO_ARG)
865           data = NULL;
866         else if (entry->flags & G_OPTION_FLAG_FILENAME)
867           {
868 #ifdef G_OS_WIN32
869             data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
870 #else
871             data = g_strdup (value);
872 #endif
873           }
874         else
875           data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
876
877         if (!(entry->flags & (G_OPTION_FLAG_NO_ARG|G_OPTION_FLAG_OPTIONAL_ARG)) && 
878             !data)
879           return FALSE;
880
881         retval = (* (GOptionArgFunc) entry->arg_data) (option_name, data, group->user_data, error);
882         
883         g_free (data);
884         
885         return retval;
886         
887         break;
888       }
889     default:
890       g_assert_not_reached ();
891     }
892
893   return TRUE;
894 }
895
896 static gboolean
897 parse_short_option (GOptionContext *context,
898                     GOptionGroup   *group,
899                     gint            index,
900                     gint           *new_index,
901                     gchar           arg,
902                     gint           *argc,
903                     gchar        ***argv,
904                     GError        **error,
905                     gboolean       *parsed)
906 {
907   gint j;
908     
909   for (j = 0; j < group->n_entries; j++)
910     {
911       if (arg == group->entries[j].short_name)
912         {
913           gchar *option_name;
914           gchar *value = NULL;
915           
916           option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
917
918           if (NO_ARG (&group->entries[j]))
919             value = NULL;
920           else
921             {
922               if (*new_index > index)
923                 {
924                   g_warning ("FIXME: figure out the correct error here");
925
926                   return FALSE;
927                 }
928
929               option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
930               
931               if (index < *argc - 1)
932                 {
933                   if (!OPTIONAL_ARG (&group->entries[j]))       
934                     {    
935                       value = (*argv)[index + 1];
936                       add_pending_null (context, &((*argv)[index + 1]), NULL);
937                       *new_index = index+1;
938                     }
939                   else
940                     {
941                       if ((*argv)[index + 1][0] == '-') 
942                         value = NULL;
943                       else
944                         {
945                           value = (*argv)[index + 1];
946                           add_pending_null (context, &((*argv)[index + 1]), NULL);
947                           *new_index = index + 1;
948                         }
949                     }
950                 }
951               else if (index >= *argc - 1 && OPTIONAL_ARG (&group->entries[j]))
952                 value = NULL;
953               else
954                 {
955                   g_set_error (error, 
956                                G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
957                                _("Missing argument for %s"), option_name);
958                   g_free (option_name);
959                   return FALSE;
960                 }
961             }
962
963           if (!parse_arg (context, group, &group->entries[j], 
964                           value, option_name, error))
965             {
966               g_free (option_name);
967               return FALSE;
968             }
969           
970           g_free (option_name);
971           *parsed = TRUE;
972         }
973     }
974
975   return TRUE;
976 }
977
978 static gboolean
979 parse_long_option (GOptionContext *context,
980                    GOptionGroup   *group,
981                    gint           *index,
982                    gchar          *arg,
983                    gboolean        aliased,
984                    gint           *argc,
985                    gchar        ***argv,
986                    GError        **error,
987                    gboolean       *parsed)
988 {
989   gint j;
990
991   for (j = 0; j < group->n_entries; j++)
992     {
993       if (*index >= *argc)
994         return TRUE;
995
996       if (aliased && (group->entries[j].flags & G_OPTION_FLAG_NOALIAS))
997         continue;
998
999       if (NO_ARG (&group->entries[j]) &&
1000           strcmp (arg, group->entries[j].long_name) == 0)
1001         {
1002           gchar *option_name;
1003
1004           option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1005           parse_arg (context, group, &group->entries[j],
1006                      NULL, option_name, error);
1007           g_free(option_name);
1008           
1009           add_pending_null (context, &((*argv)[*index]), NULL);
1010           *parsed = TRUE;
1011         }
1012       else
1013         {
1014           gint len = strlen (group->entries[j].long_name);
1015           
1016           if (strncmp (arg, group->entries[j].long_name, len) == 0 &&
1017               (arg[len] == '=' || arg[len] == 0))
1018             {
1019               gchar *value = NULL;
1020               gchar *option_name;
1021
1022               add_pending_null (context, &((*argv)[*index]), NULL);
1023               option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1024
1025               if (arg[len] == '=')
1026                 value = arg + len + 1;
1027               else if (*index < *argc - 1) 
1028                 {
1029                   if (!(group->entries[j].flags & G_OPTION_FLAG_OPTIONAL_ARG))  
1030                     {    
1031                       value = (*argv)[*index + 1];
1032                       add_pending_null (context, &((*argv)[*index + 1]), NULL);
1033                       (*index)++;
1034                     }
1035                   else
1036                     {
1037                       if ((*argv)[*index + 1][0] == '-') 
1038                         {
1039                           gboolean retval;
1040                           retval = parse_arg (context, group, &group->entries[j],
1041                                               NULL, option_name, error);
1042                           *parsed = TRUE;
1043                           g_free (option_name);
1044                           return retval;
1045                         }
1046                       else
1047                         {
1048                           value = (*argv)[*index + 1];
1049                           add_pending_null (context, &((*argv)[*index + 1]), NULL);
1050                           (*index)++;
1051                         }
1052                     }
1053                 }
1054               else if (*index >= *argc - 1 &&
1055                        group->entries[j].flags & G_OPTION_FLAG_OPTIONAL_ARG)
1056                 {
1057                     gboolean retval;
1058                     retval = parse_arg (context, group, &group->entries[j],
1059                                         NULL, option_name, error);
1060                     *parsed = TRUE;
1061                     g_free (option_name);
1062                     return retval;
1063                 }
1064               else
1065                 {
1066                   g_set_error (error, 
1067                                G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1068                                _("Missing argument for %s"), option_name);
1069                   g_free (option_name);
1070                   return FALSE;
1071                 }
1072
1073               if (!parse_arg (context, group, &group->entries[j], 
1074                               value, option_name, error))
1075                 {
1076                   g_free (option_name);
1077                   return FALSE;
1078                 }
1079
1080               g_free (option_name);
1081               *parsed = TRUE;
1082             } 
1083         }
1084     }
1085   
1086   return TRUE;
1087 }
1088
1089 static gboolean
1090 parse_remaining_arg (GOptionContext *context,
1091                      GOptionGroup   *group,
1092                      gint           *index,
1093                      gint           *argc,
1094                      gchar        ***argv,
1095                      GError        **error,
1096                      gboolean       *parsed)
1097 {
1098   gint j;
1099
1100   for (j = 0; j < group->n_entries; j++)
1101     {
1102       if (*index >= *argc)
1103         return TRUE;
1104
1105       if (group->entries[j].long_name[0])
1106         continue;
1107
1108       g_return_val_if_fail (group->entries[j].arg == G_OPTION_ARG_STRING_ARRAY ||
1109                             group->entries[j].arg == G_OPTION_ARG_FILENAME_ARRAY, FALSE);
1110       
1111       add_pending_null (context, &((*argv)[*index]), NULL);
1112       
1113       if (!parse_arg (context, group, &group->entries[j], (*argv)[*index], "", error))
1114         return FALSE;
1115       
1116       *parsed = TRUE;
1117       return TRUE;
1118     }
1119
1120   return TRUE;
1121 }
1122
1123 static void
1124 free_changes_list (GOptionContext *context,
1125                    gboolean        revert)
1126 {
1127   GList *list;
1128
1129   for (list = context->changes; list != NULL; list = list->next)
1130     {
1131       Change *change = list->data;
1132
1133       if (revert)
1134         {
1135           switch (change->arg_type)
1136             {
1137             case G_OPTION_ARG_NONE:
1138               *(gboolean *)change->arg_data = change->prev.bool;
1139               break;
1140             case G_OPTION_ARG_INT:
1141               *(gint *)change->arg_data = change->prev.integer;
1142               break;
1143             case G_OPTION_ARG_STRING:
1144             case G_OPTION_ARG_FILENAME:
1145               g_free (change->allocated.str);
1146               *(gchar **)change->arg_data = change->prev.str;
1147               break;
1148             case G_OPTION_ARG_STRING_ARRAY:
1149             case G_OPTION_ARG_FILENAME_ARRAY:
1150               g_strfreev (change->allocated.array.data);
1151               *(gchar ***)change->arg_data = change->prev.array;
1152               break;
1153             default:
1154               g_assert_not_reached ();
1155             }
1156         }
1157       
1158       g_free (change);
1159     }
1160
1161   g_list_free (context->changes);
1162   context->changes = NULL;
1163 }
1164
1165 static void
1166 free_pending_nulls (GOptionContext *context,
1167                     gboolean        perform_nulls)
1168 {
1169   GList *list;
1170
1171   for (list = context->pending_nulls; list != NULL; list = list->next)
1172     {
1173       PendingNull *n = list->data;
1174
1175       if (perform_nulls)
1176         {
1177           if (n->value)
1178             {
1179               /* Copy back the short options */
1180               *(n->ptr)[0] = '-';             
1181               strcpy (*n->ptr + 1, n->value);
1182             }
1183           else
1184             *n->ptr = NULL;
1185         }
1186       
1187       g_free (n->value);
1188       g_free (n);
1189     }
1190
1191   g_list_free (context->pending_nulls);
1192   context->pending_nulls = NULL;
1193 }
1194
1195 /**
1196  * g_option_context_parse:
1197  * @context: a #GOptionContext
1198  * @argc: a pointer to the number of command line arguments.
1199  * @argv: a pointer to the array of command line arguments.
1200  * @error: a return location for errors 
1201  * 
1202  * Parses the command line arguments, recognizing options
1203  * which have been added to @context. A side-effect of 
1204  * calling this function is that g_set_prgname() will be
1205  * called.
1206  *
1207  * If the parsing is successful, any parsed arguments are
1208  * removed from the array and @argc and @argv are updated 
1209  * accordingly. A '--' option is stripped from @argv
1210  * unless there are unparsed options before and after it, 
1211  * or some of the options after it start with '-'. In case 
1212  * of an error, @argc and @argv are left unmodified. 
1213  *
1214  * If automatic <option>--help</option> support is enabled
1215  * (see g_option_context_set_help_enabled()), and the 
1216  * @argv array contains one of the recognized help options,
1217  * this function will produce help output to stdout and
1218  * call <literal>exit (0)</literal>.
1219  * 
1220  * Return value: %TRUE if the parsing was successful, 
1221  *               %FALSE if an error occurred
1222  *
1223  * Since: 2.6
1224  **/
1225 gboolean
1226 g_option_context_parse (GOptionContext   *context,
1227                         gint             *argc,
1228                         gchar          ***argv,
1229                         GError          **error)
1230 {
1231   gint i, j, k;
1232   GList *list;
1233
1234   /* Set program name */
1235   if (argc && argv && *argc)
1236     {
1237       gchar *prgname;
1238       
1239       prgname = g_path_get_basename ((*argv)[0]);
1240       g_set_prgname (prgname);
1241       g_free (prgname);
1242     }
1243   else
1244     {
1245       g_set_prgname ("<unknown>");
1246     }
1247   
1248   /* Call pre-parse hooks */
1249   list = context->groups;
1250   while (list)
1251     {
1252       GOptionGroup *group = list->data;
1253       
1254       if (group->pre_parse_func)
1255         {
1256           if (!(* group->pre_parse_func) (context, group,
1257                                           group->user_data, error))
1258             goto fail;
1259         }
1260       
1261       list = list->next;
1262     }
1263
1264   if (context->main_group && context->main_group->pre_parse_func)
1265     {
1266       if (!(* context->main_group->pre_parse_func) (context, context->main_group,
1267                                                     context->main_group->user_data, error))
1268         goto fail;
1269     }
1270
1271   if (argc && argv)
1272     {
1273       gboolean stop_parsing = FALSE;
1274       gboolean has_unknown = FALSE;
1275       gint separator_pos = 0;
1276
1277       for (i = 1; i < *argc; i++)
1278         {
1279           gchar *arg, *dash;
1280           gboolean parsed = FALSE;
1281
1282           if ((*argv)[i][0] == '-' && (*argv)[i][1] != '\0' && !stop_parsing)
1283             {
1284               if ((*argv)[i][1] == '-')
1285                 {
1286                   /* -- option */
1287
1288                   arg = (*argv)[i] + 2;
1289
1290                   /* '--' terminates list of arguments */
1291                   if (*arg == 0)
1292                     {
1293                       separator_pos = i;
1294                       stop_parsing = TRUE;
1295                       continue;
1296                     }
1297
1298                   /* Handle help options */
1299                   if (context->help_enabled)
1300                     {
1301                       if (strcmp (arg, "help") == 0)
1302                         print_help (context, TRUE, NULL);
1303                       else if (strcmp (arg, "help-all") == 0)
1304                         print_help (context, FALSE, NULL);                    
1305                       else if (strncmp (arg, "help-", 5) == 0)
1306                         {
1307                           GList *list;
1308                           
1309                           list = context->groups;
1310                           
1311                           while (list)
1312                             {
1313                               GOptionGroup *group = list->data;
1314                               
1315                               if (strcmp (arg + 5, group->name) == 0)
1316                                 print_help (context, FALSE, group);                                           
1317                               
1318                               list = list->next;
1319                             }
1320                         }
1321                     }
1322
1323                   if (context->main_group &&
1324                       !parse_long_option (context, context->main_group, &i, arg,
1325                                           FALSE, argc, argv, error, &parsed))
1326                     goto fail;
1327
1328                   if (parsed)
1329                     continue;
1330                   
1331                   /* Try the groups */
1332                   list = context->groups;
1333                   while (list)
1334                     {
1335                       GOptionGroup *group = list->data;
1336                       
1337                       if (!parse_long_option (context, group, &i, arg, 
1338                                               FALSE, argc, argv, error, &parsed))
1339                         goto fail;
1340                       
1341                       if (parsed)
1342                         break;
1343                       
1344                       list = list->next;
1345                     }
1346                   
1347                   if (parsed)
1348                     continue;
1349
1350                   /* Now look for --<group>-<option> */
1351                   dash = strchr (arg, '-');
1352                   if (dash)
1353                     {
1354                       /* Try the groups */
1355                       list = context->groups;
1356                       while (list)
1357                         {
1358                           GOptionGroup *group = list->data;
1359                           
1360                           if (strncmp (group->name, arg, dash - arg) == 0)
1361                             {
1362                               if (!parse_long_option (context, group, &i, dash + 1,
1363                                                       TRUE, argc, argv, error, &parsed))
1364                                 goto fail;
1365                               
1366                               if (parsed)
1367                                 break;
1368                             }
1369                           
1370                           list = list->next;
1371                         }
1372                     }
1373                   
1374                   if (context->ignore_unknown)
1375                     continue;
1376                 }
1377               else
1378                 {
1379                   /* short option */
1380
1381                   gint new_i, j;
1382                   gboolean *nulled_out = NULL;
1383                   
1384                   arg = (*argv)[i] + 1;
1385
1386                   new_i = i;
1387
1388                   if (context->ignore_unknown)
1389                     nulled_out = g_new0 (gboolean, strlen (arg));
1390                   
1391                   for (j = 0; j < strlen (arg); j++)
1392                     {
1393                       if (context->help_enabled && arg[j] == '?')
1394                         print_help (context, TRUE, NULL);
1395                       
1396                       parsed = FALSE;
1397                       
1398                       if (context->main_group &&
1399                           !parse_short_option (context, context->main_group,
1400                                                i, &new_i, arg[j],
1401                                                argc, argv, error, &parsed))
1402                         {
1403
1404                           g_free (nulled_out);
1405                           goto fail;
1406                         }
1407
1408                       if (!parsed)
1409                         {
1410                           /* Try the groups */
1411                           list = context->groups;
1412                           while (list)
1413                             {
1414                               GOptionGroup *group = list->data;
1415                               
1416                               if (!parse_short_option (context, group, i, &new_i, arg[j],
1417                                                        argc, argv, error, &parsed))
1418                                 goto fail;
1419                               
1420                               if (parsed)
1421                                 break;
1422                           
1423                               list = list->next;
1424                             }
1425                         }
1426
1427                       if (context->ignore_unknown)
1428                         {
1429                           if (parsed)
1430                             nulled_out[j] = TRUE;
1431                           else
1432                             continue;
1433                         }
1434
1435                       if (!parsed)
1436                         break;
1437                     }
1438
1439                   if (context->ignore_unknown)
1440                     {
1441                       gchar *new_arg = NULL; 
1442                       gint arg_index = 0;
1443                       
1444                       for (j = 0; j < strlen (arg); j++)
1445                         {
1446                           if (!nulled_out[j])
1447                             {
1448                               if (!new_arg)
1449                                 new_arg = g_malloc (strlen (arg) + 1);
1450                               new_arg[arg_index++] = arg[j];
1451                             }
1452                         }
1453                       if (new_arg)
1454                         new_arg[arg_index] = '\0';
1455                       
1456                       add_pending_null (context, &((*argv)[i]), new_arg);
1457                     }
1458                   else if (parsed)
1459                     {
1460                       add_pending_null (context, &((*argv)[i]), NULL);
1461                       i = new_i;
1462                     }
1463                 }
1464               
1465               if (!parsed)
1466                 has_unknown = TRUE;
1467
1468               if (!parsed && !context->ignore_unknown)
1469                 {
1470                   g_set_error (error,
1471                                G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
1472                                    _("Unknown option %s"), (*argv)[i]);
1473                   goto fail;
1474                 }
1475             }
1476           else
1477             {
1478               /* Collect remaining args */
1479               if (context->main_group &&
1480                   !parse_remaining_arg (context, context->main_group, &i,
1481                                         argc, argv, error, &parsed))
1482                 goto fail;
1483               
1484               if (!parsed && (has_unknown || (*argv)[i][0] == '-'))
1485                 separator_pos = 0;
1486             }
1487         }
1488
1489       if (separator_pos > 0)
1490         add_pending_null (context, &((*argv)[separator_pos]), NULL);
1491         
1492     }
1493
1494   /* Call post-parse hooks */
1495   list = context->groups;
1496   while (list)
1497     {
1498       GOptionGroup *group = list->data;
1499       
1500       if (group->post_parse_func)
1501         {
1502           if (!(* group->post_parse_func) (context, group,
1503                                            group->user_data, error))
1504             goto fail;
1505         }
1506       
1507       list = list->next;
1508     }
1509   
1510   if (context->main_group && context->main_group->post_parse_func)
1511     {
1512       if (!(* context->main_group->post_parse_func) (context, context->main_group,
1513                                                      context->main_group->user_data, error))
1514         goto fail;
1515     }
1516   
1517   if (argc && argv)
1518     {
1519       free_pending_nulls (context, TRUE);
1520       
1521       for (i = 1; i < *argc; i++)
1522         {
1523           for (k = i; k < *argc; k++)
1524             if ((*argv)[k] != NULL)
1525               break;
1526           
1527           if (k > i)
1528             {
1529               k -= i;
1530               for (j = i + k; j < *argc; j++)
1531                 {
1532                   (*argv)[j-k] = (*argv)[j];
1533                   (*argv)[j] = NULL;
1534                 }
1535               *argc -= k;
1536             }
1537         }      
1538     }
1539
1540   return TRUE;
1541
1542  fail:
1543   
1544   /* Call error hooks */
1545   list = context->groups;
1546   while (list)
1547     {
1548       GOptionGroup *group = list->data;
1549       
1550       if (group->error_func)
1551         (* group->error_func) (context, group,
1552                                group->user_data, error);
1553       
1554       list = list->next;
1555     }
1556
1557   if (context->main_group && context->main_group->error_func)
1558     (* context->main_group->error_func) (context, context->main_group,
1559                                          context->main_group->user_data, error);
1560   
1561   free_changes_list (context, TRUE);
1562   free_pending_nulls (context, FALSE);
1563
1564   return FALSE;
1565 }
1566                                    
1567 /**
1568  * g_option_group_new:
1569  * @name: the name for the option group, this is used to provide
1570  *   help for the options in this group with <option>--help-</option>@name
1571  * @description: a description for this group to be shown in 
1572  *   <option>--help</option>. This string is translated using the translation
1573  *   domain or translation function of the group
1574  * @help_description: a description for the <option>--help-</option>@name option.
1575  *   This string is translated using the translation domain or translation function
1576  *   of the group
1577  * @user_data: user data that will be passed to the pre- and post-parse hooks,
1578  *   the error hook and to callbacks of %G_OPTION_ARG_CALLBACK options, or %NULL
1579  * @destroy: a function that will be called to free @user_data, or %NULL
1580  * 
1581  * Creates a new #GOptionGroup.
1582  * 
1583  * Return value: a newly created option group. It should be added 
1584  *   to a #GOptionContext or freed with g_option_group_free().
1585  *
1586  * Since: 2.6
1587  **/
1588 GOptionGroup *
1589 g_option_group_new (const gchar    *name,
1590                     const gchar    *description,
1591                     const gchar    *help_description,
1592                     gpointer        user_data,
1593                     GDestroyNotify  destroy)
1594
1595 {
1596   GOptionGroup *group;
1597
1598   group = g_new0 (GOptionGroup, 1);
1599   group->name = g_strdup (name);
1600   group->description = g_strdup (description);
1601   group->help_description = g_strdup (help_description);
1602   group->user_data = user_data;
1603   group->destroy_notify = destroy;
1604   
1605   return group;
1606 }
1607
1608
1609 /**
1610  * g_option_group_free:
1611  * @group: a #GOptionGroup
1612  * 
1613  * Frees a #GOptionGroup. Note that you must <emphasis>not</emphasis>
1614  * free groups which have been added to a #GOptionContext.
1615  *
1616  * Since: 2.6
1617  **/
1618 void
1619 g_option_group_free (GOptionGroup *group)
1620 {
1621   g_return_if_fail (group != NULL);
1622
1623   g_free (group->name);
1624   g_free (group->description);
1625   g_free (group->help_description);
1626
1627   g_free (group->entries);
1628   
1629   if (group->destroy_notify)
1630     (* group->destroy_notify) (group->user_data);
1631
1632   if (group->translate_notify)
1633     (* group->translate_notify) (group->translate_data);
1634   
1635   g_free (group);
1636 }
1637
1638
1639 /**
1640  * g_option_group_add_entries:
1641  * @group: a #GOptionGroup
1642  * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
1643  * 
1644  * Adds the options specified in @entries to @group.
1645  *
1646  * Since: 2.6
1647  **/
1648 void
1649 g_option_group_add_entries (GOptionGroup       *group,
1650                             const GOptionEntry *entries)
1651 {
1652   gint i, n_entries;
1653   
1654   g_return_if_fail (entries != NULL);
1655
1656   for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++) ;
1657
1658   group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
1659
1660   memcpy (group->entries + group->n_entries, entries, sizeof (GOptionEntry) * n_entries);
1661
1662   for (i = group->n_entries; i < group->n_entries + n_entries; i++)
1663     {
1664       gchar c = group->entries[i].short_name;
1665
1666       if (c)
1667         {
1668           if (c == '-' || !g_ascii_isprint (c))
1669             {
1670               g_warning (G_STRLOC": ignoring invalid short option '%c' (%d)", c, c);
1671               group->entries[i].short_name = 0;
1672             }
1673         }
1674     }
1675
1676   group->n_entries += n_entries;
1677 }
1678
1679 /**
1680  * g_option_group_set_parse_hooks:
1681  * @group: a #GOptionGroup
1682  * @pre_parse_func: a function to call before parsing, or %NULL
1683  * @post_parse_func: a function to call after parsing, or %NULL
1684  * 
1685  * Associates two functions with @group which will be called 
1686  * from g_option_context_parse() before the first option is parsed
1687  * and after the last option has been parsed, respectively.
1688  *
1689  * Note that the user data to be passed to @pre_parse_func and
1690  * @post_parse_func can be specified when constructing the group
1691  * with g_option_group_new().
1692  *
1693  * Since: 2.6
1694  **/
1695 void
1696 g_option_group_set_parse_hooks (GOptionGroup     *group,
1697                                 GOptionParseFunc  pre_parse_func,
1698                                 GOptionParseFunc  post_parse_func)
1699 {
1700   g_return_if_fail (group != NULL);
1701
1702   group->pre_parse_func = pre_parse_func;
1703   group->post_parse_func = post_parse_func;  
1704 }
1705
1706 /**
1707  * g_option_group_set_error_hook:
1708  * @group: a #GOptionGroup
1709  * @error_func: a function to call when an error occurs
1710  * 
1711  * Associates a function with @group which will be called 
1712  * from g_option_context_parse() when an error occurs.
1713  *
1714  * Note that the user data to be passed to @pre_parse_func and
1715  * @post_parse_func can be specified when constructing the group
1716  * with g_option_group_new().
1717  *
1718  * Since: 2.6
1719  **/
1720 void
1721 g_option_group_set_error_hook (GOptionGroup     *group,
1722                                GOptionErrorFunc  error_func)
1723 {
1724   g_return_if_fail (group != NULL);
1725
1726   group->error_func = error_func;  
1727 }
1728
1729
1730 /**
1731  * g_option_group_set_translate_func:
1732  * @group: a #GOptionGroup
1733  * @func: the #GTranslateFunc, or %NULL 
1734  * @data: user data to pass to @func, or %NULL
1735  * @destroy_notify: a function which gets called to free @data, or %NULL
1736  * 
1737  * Sets the function which is used to translate user-visible
1738  * strings, for <option>--help</option> output. Different
1739  * groups can use different #GTranslateFunc<!-- -->s. If @func
1740  * is %NULL, strings are not translated.
1741  *
1742  * If you are using gettext(), you only need to set the translation
1743  * domain, see g_option_group_set_translation_domain().
1744  *
1745  * Since: 2.6
1746  **/
1747 void
1748 g_option_group_set_translate_func (GOptionGroup   *group,
1749                                    GTranslateFunc  func,
1750                                    gpointer        data,
1751                                    GDestroyNotify  destroy_notify)
1752 {
1753   g_return_if_fail (group != NULL);
1754   
1755   if (group->translate_notify)
1756     group->translate_notify (group->translate_data);
1757       
1758   group->translate_func = func;
1759   group->translate_data = data;
1760   group->translate_notify = destroy_notify;
1761 }
1762
1763 static gchar *
1764 dgettext_swapped (const gchar *msgid, 
1765                   const gchar *domainname)
1766 {
1767   return dgettext (domainname, msgid);
1768 }
1769
1770 /**
1771  * g_option_group_set_translation_domain:
1772  * @group: a #GOptionGroup
1773  * @domain: the domain to use
1774  * 
1775  * A convenience function to use gettext() for translating
1776  * user-visible strings. 
1777  * 
1778  * Since: 2.6
1779  **/
1780 void
1781 g_option_group_set_translation_domain (GOptionGroup *group,
1782                                        const gchar  *domain)
1783 {
1784   g_return_if_fail (group != NULL);
1785
1786   g_option_group_set_translate_func (group, 
1787                                      (GTranslateFunc)dgettext_swapped,
1788                                      g_strdup (domain),
1789                                      g_free);
1790
1791
1792 #define __G_OPTION_C__
1793 #include "galiasdef.c"