Set an error in all failure cases. (#324332, Tim-Philipp Müller)
[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 "glibintl.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_set_error (error, 
925                                G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
926                                _("Error parsing option %s"), option_name);
927                   g_free (option_name);
928                   return FALSE;
929                 }
930
931               option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
932               
933               if (index < *argc - 1)
934                 {
935                   if (!OPTIONAL_ARG (&group->entries[j]))       
936                     {    
937                       value = (*argv)[index + 1];
938                       add_pending_null (context, &((*argv)[index + 1]), NULL);
939                       *new_index = index+1;
940                     }
941                   else
942                     {
943                       if ((*argv)[index + 1][0] == '-') 
944                         value = NULL;
945                       else
946                         {
947                           value = (*argv)[index + 1];
948                           add_pending_null (context, &((*argv)[index + 1]), NULL);
949                           *new_index = index + 1;
950                         }
951                     }
952                 }
953               else if (index >= *argc - 1 && OPTIONAL_ARG (&group->entries[j]))
954                 value = NULL;
955               else
956                 {
957                   g_set_error (error, 
958                                G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
959                                _("Missing argument for %s"), option_name);
960                   g_free (option_name);
961                   return FALSE;
962                 }
963             }
964
965           if (!parse_arg (context, group, &group->entries[j], 
966                           value, option_name, error))
967             {
968               g_free (option_name);
969               return FALSE;
970             }
971           
972           g_free (option_name);
973           *parsed = TRUE;
974         }
975     }
976
977   return TRUE;
978 }
979
980 static gboolean
981 parse_long_option (GOptionContext *context,
982                    GOptionGroup   *group,
983                    gint           *index,
984                    gchar          *arg,
985                    gboolean        aliased,
986                    gint           *argc,
987                    gchar        ***argv,
988                    GError        **error,
989                    gboolean       *parsed)
990 {
991   gint j;
992
993   for (j = 0; j < group->n_entries; j++)
994     {
995       if (*index >= *argc)
996         return TRUE;
997
998       if (aliased && (group->entries[j].flags & G_OPTION_FLAG_NOALIAS))
999         continue;
1000
1001       if (NO_ARG (&group->entries[j]) &&
1002           strcmp (arg, group->entries[j].long_name) == 0)
1003         {
1004           gchar *option_name;
1005
1006           option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1007           parse_arg (context, group, &group->entries[j],
1008                      NULL, option_name, error);
1009           g_free(option_name);
1010           
1011           add_pending_null (context, &((*argv)[*index]), NULL);
1012           *parsed = TRUE;
1013         }
1014       else
1015         {
1016           gint len = strlen (group->entries[j].long_name);
1017           
1018           if (strncmp (arg, group->entries[j].long_name, len) == 0 &&
1019               (arg[len] == '=' || arg[len] == 0))
1020             {
1021               gchar *value = NULL;
1022               gchar *option_name;
1023
1024               add_pending_null (context, &((*argv)[*index]), NULL);
1025               option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1026
1027               if (arg[len] == '=')
1028                 value = arg + len + 1;
1029               else if (*index < *argc - 1) 
1030                 {
1031                   if (!(group->entries[j].flags & G_OPTION_FLAG_OPTIONAL_ARG))  
1032                     {    
1033                       value = (*argv)[*index + 1];
1034                       add_pending_null (context, &((*argv)[*index + 1]), NULL);
1035                       (*index)++;
1036                     }
1037                   else
1038                     {
1039                       if ((*argv)[*index + 1][0] == '-') 
1040                         {
1041                           gboolean retval;
1042                           retval = parse_arg (context, group, &group->entries[j],
1043                                               NULL, option_name, error);
1044                           *parsed = TRUE;
1045                           g_free (option_name);
1046                           return retval;
1047                         }
1048                       else
1049                         {
1050                           value = (*argv)[*index + 1];
1051                           add_pending_null (context, &((*argv)[*index + 1]), NULL);
1052                           (*index)++;
1053                         }
1054                     }
1055                 }
1056               else if (*index >= *argc - 1 &&
1057                        group->entries[j].flags & G_OPTION_FLAG_OPTIONAL_ARG)
1058                 {
1059                     gboolean retval;
1060                     retval = parse_arg (context, group, &group->entries[j],
1061                                         NULL, option_name, error);
1062                     *parsed = TRUE;
1063                     g_free (option_name);
1064                     return retval;
1065                 }
1066               else
1067                 {
1068                   g_set_error (error, 
1069                                G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1070                                _("Missing argument for %s"), option_name);
1071                   g_free (option_name);
1072                   return FALSE;
1073                 }
1074
1075               if (!parse_arg (context, group, &group->entries[j], 
1076                               value, option_name, error))
1077                 {
1078                   g_free (option_name);
1079                   return FALSE;
1080                 }
1081
1082               g_free (option_name);
1083               *parsed = TRUE;
1084             } 
1085         }
1086     }
1087   
1088   return TRUE;
1089 }
1090
1091 static gboolean
1092 parse_remaining_arg (GOptionContext *context,
1093                      GOptionGroup   *group,
1094                      gint           *index,
1095                      gint           *argc,
1096                      gchar        ***argv,
1097                      GError        **error,
1098                      gboolean       *parsed)
1099 {
1100   gint j;
1101
1102   for (j = 0; j < group->n_entries; j++)
1103     {
1104       if (*index >= *argc)
1105         return TRUE;
1106
1107       if (group->entries[j].long_name[0])
1108         continue;
1109
1110       g_return_val_if_fail (group->entries[j].arg == G_OPTION_ARG_STRING_ARRAY ||
1111                             group->entries[j].arg == G_OPTION_ARG_FILENAME_ARRAY, FALSE);
1112       
1113       add_pending_null (context, &((*argv)[*index]), NULL);
1114       
1115       if (!parse_arg (context, group, &group->entries[j], (*argv)[*index], "", error))
1116         return FALSE;
1117       
1118       *parsed = TRUE;
1119       return TRUE;
1120     }
1121
1122   return TRUE;
1123 }
1124
1125 static void
1126 free_changes_list (GOptionContext *context,
1127                    gboolean        revert)
1128 {
1129   GList *list;
1130
1131   for (list = context->changes; list != NULL; list = list->next)
1132     {
1133       Change *change = list->data;
1134
1135       if (revert)
1136         {
1137           switch (change->arg_type)
1138             {
1139             case G_OPTION_ARG_NONE:
1140               *(gboolean *)change->arg_data = change->prev.bool;
1141               break;
1142             case G_OPTION_ARG_INT:
1143               *(gint *)change->arg_data = change->prev.integer;
1144               break;
1145             case G_OPTION_ARG_STRING:
1146             case G_OPTION_ARG_FILENAME:
1147               g_free (change->allocated.str);
1148               *(gchar **)change->arg_data = change->prev.str;
1149               break;
1150             case G_OPTION_ARG_STRING_ARRAY:
1151             case G_OPTION_ARG_FILENAME_ARRAY:
1152               g_strfreev (change->allocated.array.data);
1153               *(gchar ***)change->arg_data = change->prev.array;
1154               break;
1155             default:
1156               g_assert_not_reached ();
1157             }
1158         }
1159       
1160       g_free (change);
1161     }
1162
1163   g_list_free (context->changes);
1164   context->changes = NULL;
1165 }
1166
1167 static void
1168 free_pending_nulls (GOptionContext *context,
1169                     gboolean        perform_nulls)
1170 {
1171   GList *list;
1172
1173   for (list = context->pending_nulls; list != NULL; list = list->next)
1174     {
1175       PendingNull *n = list->data;
1176
1177       if (perform_nulls)
1178         {
1179           if (n->value)
1180             {
1181               /* Copy back the short options */
1182               *(n->ptr)[0] = '-';             
1183               strcpy (*n->ptr + 1, n->value);
1184             }
1185           else
1186             *n->ptr = NULL;
1187         }
1188       
1189       g_free (n->value);
1190       g_free (n);
1191     }
1192
1193   g_list_free (context->pending_nulls);
1194   context->pending_nulls = NULL;
1195 }
1196
1197 /**
1198  * g_option_context_parse:
1199  * @context: a #GOptionContext
1200  * @argc: a pointer to the number of command line arguments.
1201  * @argv: a pointer to the array of command line arguments.
1202  * @error: a return location for errors 
1203  * 
1204  * Parses the command line arguments, recognizing options
1205  * which have been added to @context. A side-effect of 
1206  * calling this function is that g_set_prgname() will be
1207  * called.
1208  *
1209  * If the parsing is successful, any parsed arguments are
1210  * removed from the array and @argc and @argv are updated 
1211  * accordingly. A '--' option is stripped from @argv
1212  * unless there are unparsed options before and after it, 
1213  * or some of the options after it start with '-'. In case 
1214  * of an error, @argc and @argv are left unmodified. 
1215  *
1216  * If automatic <option>--help</option> support is enabled
1217  * (see g_option_context_set_help_enabled()), and the 
1218  * @argv array contains one of the recognized help options,
1219  * this function will produce help output to stdout and
1220  * call <literal>exit (0)</literal>.
1221  * 
1222  * Return value: %TRUE if the parsing was successful, 
1223  *               %FALSE if an error occurred
1224  *
1225  * Since: 2.6
1226  **/
1227 gboolean
1228 g_option_context_parse (GOptionContext   *context,
1229                         gint             *argc,
1230                         gchar          ***argv,
1231                         GError          **error)
1232 {
1233   gint i, j, k;
1234   GList *list;
1235
1236   /* Set program name */
1237   if (argc && argv && *argc)
1238     {
1239       gchar *prgname;
1240       
1241       prgname = g_path_get_basename ((*argv)[0]);
1242       g_set_prgname (prgname);
1243       g_free (prgname);
1244     }
1245   else
1246     {
1247       g_set_prgname ("<unknown>");
1248     }
1249   
1250   /* Call pre-parse hooks */
1251   list = context->groups;
1252   while (list)
1253     {
1254       GOptionGroup *group = list->data;
1255       
1256       if (group->pre_parse_func)
1257         {
1258           if (!(* group->pre_parse_func) (context, group,
1259                                           group->user_data, error))
1260             goto fail;
1261         }
1262       
1263       list = list->next;
1264     }
1265
1266   if (context->main_group && context->main_group->pre_parse_func)
1267     {
1268       if (!(* context->main_group->pre_parse_func) (context, context->main_group,
1269                                                     context->main_group->user_data, error))
1270         goto fail;
1271     }
1272
1273   if (argc && argv)
1274     {
1275       gboolean stop_parsing = FALSE;
1276       gboolean has_unknown = FALSE;
1277       gint separator_pos = 0;
1278
1279       for (i = 1; i < *argc; i++)
1280         {
1281           gchar *arg, *dash;
1282           gboolean parsed = FALSE;
1283
1284           if ((*argv)[i][0] == '-' && (*argv)[i][1] != '\0' && !stop_parsing)
1285             {
1286               if ((*argv)[i][1] == '-')
1287                 {
1288                   /* -- option */
1289
1290                   arg = (*argv)[i] + 2;
1291
1292                   /* '--' terminates list of arguments */
1293                   if (*arg == 0)
1294                     {
1295                       separator_pos = i;
1296                       stop_parsing = TRUE;
1297                       continue;
1298                     }
1299
1300                   /* Handle help options */
1301                   if (context->help_enabled)
1302                     {
1303                       if (strcmp (arg, "help") == 0)
1304                         print_help (context, TRUE, NULL);
1305                       else if (strcmp (arg, "help-all") == 0)
1306                         print_help (context, FALSE, NULL);                    
1307                       else if (strncmp (arg, "help-", 5) == 0)
1308                         {
1309                           GList *list;
1310                           
1311                           list = context->groups;
1312                           
1313                           while (list)
1314                             {
1315                               GOptionGroup *group = list->data;
1316                               
1317                               if (strcmp (arg + 5, group->name) == 0)
1318                                 print_help (context, FALSE, group);                                           
1319                               
1320                               list = list->next;
1321                             }
1322                         }
1323                     }
1324
1325                   if (context->main_group &&
1326                       !parse_long_option (context, context->main_group, &i, arg,
1327                                           FALSE, argc, argv, error, &parsed))
1328                     goto fail;
1329
1330                   if (parsed)
1331                     continue;
1332                   
1333                   /* Try the groups */
1334                   list = context->groups;
1335                   while (list)
1336                     {
1337                       GOptionGroup *group = list->data;
1338                       
1339                       if (!parse_long_option (context, group, &i, arg, 
1340                                               FALSE, argc, argv, error, &parsed))
1341                         goto fail;
1342                       
1343                       if (parsed)
1344                         break;
1345                       
1346                       list = list->next;
1347                     }
1348                   
1349                   if (parsed)
1350                     continue;
1351
1352                   /* Now look for --<group>-<option> */
1353                   dash = strchr (arg, '-');
1354                   if (dash)
1355                     {
1356                       /* Try the groups */
1357                       list = context->groups;
1358                       while (list)
1359                         {
1360                           GOptionGroup *group = list->data;
1361                           
1362                           if (strncmp (group->name, arg, dash - arg) == 0)
1363                             {
1364                               if (!parse_long_option (context, group, &i, dash + 1,
1365                                                       TRUE, argc, argv, error, &parsed))
1366                                 goto fail;
1367                               
1368                               if (parsed)
1369                                 break;
1370                             }
1371                           
1372                           list = list->next;
1373                         }
1374                     }
1375                   
1376                   if (context->ignore_unknown)
1377                     continue;
1378                 }
1379               else
1380                 {
1381                   /* short option */
1382
1383                   gint new_i, j;
1384                   gboolean *nulled_out = NULL;
1385                   
1386                   arg = (*argv)[i] + 1;
1387
1388                   new_i = i;
1389
1390                   if (context->ignore_unknown)
1391                     nulled_out = g_new0 (gboolean, strlen (arg));
1392                   
1393                   for (j = 0; j < strlen (arg); j++)
1394                     {
1395                       if (context->help_enabled && arg[j] == '?')
1396                         print_help (context, TRUE, NULL);
1397                       
1398                       parsed = FALSE;
1399                       
1400                       if (context->main_group &&
1401                           !parse_short_option (context, context->main_group,
1402                                                i, &new_i, arg[j],
1403                                                argc, argv, error, &parsed))
1404                         {
1405
1406                           g_free (nulled_out);
1407                           goto fail;
1408                         }
1409
1410                       if (!parsed)
1411                         {
1412                           /* Try the groups */
1413                           list = context->groups;
1414                           while (list)
1415                             {
1416                               GOptionGroup *group = list->data;
1417                               
1418                               if (!parse_short_option (context, group, i, &new_i, arg[j],
1419                                                        argc, argv, error, &parsed))
1420                                 goto fail;
1421                               
1422                               if (parsed)
1423                                 break;
1424                           
1425                               list = list->next;
1426                             }
1427                         }
1428
1429                       if (context->ignore_unknown)
1430                         {
1431                           if (parsed)
1432                             nulled_out[j] = TRUE;
1433                           else
1434                             continue;
1435                         }
1436
1437                       if (!parsed)
1438                         break;
1439                     }
1440
1441                   if (context->ignore_unknown)
1442                     {
1443                       gchar *new_arg = NULL; 
1444                       gint arg_index = 0;
1445                       
1446                       for (j = 0; j < strlen (arg); j++)
1447                         {
1448                           if (!nulled_out[j])
1449                             {
1450                               if (!new_arg)
1451                                 new_arg = g_malloc (strlen (arg) + 1);
1452                               new_arg[arg_index++] = arg[j];
1453                             }
1454                         }
1455                       if (new_arg)
1456                         new_arg[arg_index] = '\0';
1457                       
1458                       add_pending_null (context, &((*argv)[i]), new_arg);
1459                     }
1460                   else if (parsed)
1461                     {
1462                       add_pending_null (context, &((*argv)[i]), NULL);
1463                       i = new_i;
1464                     }
1465                 }
1466               
1467               if (!parsed)
1468                 has_unknown = TRUE;
1469
1470               if (!parsed && !context->ignore_unknown)
1471                 {
1472                   g_set_error (error,
1473                                G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
1474                                    _("Unknown option %s"), (*argv)[i]);
1475                   goto fail;
1476                 }
1477             }
1478           else
1479             {
1480               /* Collect remaining args */
1481               if (context->main_group &&
1482                   !parse_remaining_arg (context, context->main_group, &i,
1483                                         argc, argv, error, &parsed))
1484                 goto fail;
1485               
1486               if (!parsed && (has_unknown || (*argv)[i][0] == '-'))
1487                 separator_pos = 0;
1488             }
1489         }
1490
1491       if (separator_pos > 0)
1492         add_pending_null (context, &((*argv)[separator_pos]), NULL);
1493         
1494     }
1495
1496   /* Call post-parse hooks */
1497   list = context->groups;
1498   while (list)
1499     {
1500       GOptionGroup *group = list->data;
1501       
1502       if (group->post_parse_func)
1503         {
1504           if (!(* group->post_parse_func) (context, group,
1505                                            group->user_data, error))
1506             goto fail;
1507         }
1508       
1509       list = list->next;
1510     }
1511   
1512   if (context->main_group && context->main_group->post_parse_func)
1513     {
1514       if (!(* context->main_group->post_parse_func) (context, context->main_group,
1515                                                      context->main_group->user_data, error))
1516         goto fail;
1517     }
1518   
1519   if (argc && argv)
1520     {
1521       free_pending_nulls (context, TRUE);
1522       
1523       for (i = 1; i < *argc; i++)
1524         {
1525           for (k = i; k < *argc; k++)
1526             if ((*argv)[k] != NULL)
1527               break;
1528           
1529           if (k > i)
1530             {
1531               k -= i;
1532               for (j = i + k; j < *argc; j++)
1533                 {
1534                   (*argv)[j-k] = (*argv)[j];
1535                   (*argv)[j] = NULL;
1536                 }
1537               *argc -= k;
1538             }
1539         }      
1540     }
1541
1542   return TRUE;
1543
1544  fail:
1545   
1546   /* Call error hooks */
1547   list = context->groups;
1548   while (list)
1549     {
1550       GOptionGroup *group = list->data;
1551       
1552       if (group->error_func)
1553         (* group->error_func) (context, group,
1554                                group->user_data, error);
1555       
1556       list = list->next;
1557     }
1558
1559   if (context->main_group && context->main_group->error_func)
1560     (* context->main_group->error_func) (context, context->main_group,
1561                                          context->main_group->user_data, error);
1562   
1563   free_changes_list (context, TRUE);
1564   free_pending_nulls (context, FALSE);
1565
1566   return FALSE;
1567 }
1568                                    
1569 /**
1570  * g_option_group_new:
1571  * @name: the name for the option group, this is used to provide
1572  *   help for the options in this group with <option>--help-</option>@name
1573  * @description: a description for this group to be shown in 
1574  *   <option>--help</option>. This string is translated using the translation
1575  *   domain or translation function of the group
1576  * @help_description: a description for the <option>--help-</option>@name option.
1577  *   This string is translated using the translation domain or translation function
1578  *   of the group
1579  * @user_data: user data that will be passed to the pre- and post-parse hooks,
1580  *   the error hook and to callbacks of %G_OPTION_ARG_CALLBACK options, or %NULL
1581  * @destroy: a function that will be called to free @user_data, or %NULL
1582  * 
1583  * Creates a new #GOptionGroup.
1584  * 
1585  * Return value: a newly created option group. It should be added 
1586  *   to a #GOptionContext or freed with g_option_group_free().
1587  *
1588  * Since: 2.6
1589  **/
1590 GOptionGroup *
1591 g_option_group_new (const gchar    *name,
1592                     const gchar    *description,
1593                     const gchar    *help_description,
1594                     gpointer        user_data,
1595                     GDestroyNotify  destroy)
1596
1597 {
1598   GOptionGroup *group;
1599
1600   group = g_new0 (GOptionGroup, 1);
1601   group->name = g_strdup (name);
1602   group->description = g_strdup (description);
1603   group->help_description = g_strdup (help_description);
1604   group->user_data = user_data;
1605   group->destroy_notify = destroy;
1606   
1607   return group;
1608 }
1609
1610
1611 /**
1612  * g_option_group_free:
1613  * @group: a #GOptionGroup
1614  * 
1615  * Frees a #GOptionGroup. Note that you must <emphasis>not</emphasis>
1616  * free groups which have been added to a #GOptionContext.
1617  *
1618  * Since: 2.6
1619  **/
1620 void
1621 g_option_group_free (GOptionGroup *group)
1622 {
1623   g_return_if_fail (group != NULL);
1624
1625   g_free (group->name);
1626   g_free (group->description);
1627   g_free (group->help_description);
1628
1629   g_free (group->entries);
1630   
1631   if (group->destroy_notify)
1632     (* group->destroy_notify) (group->user_data);
1633
1634   if (group->translate_notify)
1635     (* group->translate_notify) (group->translate_data);
1636   
1637   g_free (group);
1638 }
1639
1640
1641 /**
1642  * g_option_group_add_entries:
1643  * @group: a #GOptionGroup
1644  * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
1645  * 
1646  * Adds the options specified in @entries to @group.
1647  *
1648  * Since: 2.6
1649  **/
1650 void
1651 g_option_group_add_entries (GOptionGroup       *group,
1652                             const GOptionEntry *entries)
1653 {
1654   gint i, n_entries;
1655   
1656   g_return_if_fail (entries != NULL);
1657
1658   for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++) ;
1659
1660   group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
1661
1662   memcpy (group->entries + group->n_entries, entries, sizeof (GOptionEntry) * n_entries);
1663
1664   for (i = group->n_entries; i < group->n_entries + n_entries; i++)
1665     {
1666       gchar c = group->entries[i].short_name;
1667
1668       if (c)
1669         {
1670           if (c == '-' || !g_ascii_isprint (c))
1671             {
1672               g_warning (G_STRLOC": ignoring invalid short option '%c' (%d)", c, c);
1673               group->entries[i].short_name = 0;
1674             }
1675         }
1676     }
1677
1678   group->n_entries += n_entries;
1679 }
1680
1681 /**
1682  * g_option_group_set_parse_hooks:
1683  * @group: a #GOptionGroup
1684  * @pre_parse_func: a function to call before parsing, or %NULL
1685  * @post_parse_func: a function to call after parsing, or %NULL
1686  * 
1687  * Associates two functions with @group which will be called 
1688  * from g_option_context_parse() before the first option is parsed
1689  * and after the last option has been parsed, respectively.
1690  *
1691  * Note that the user data to be passed to @pre_parse_func and
1692  * @post_parse_func can be specified when constructing the group
1693  * with g_option_group_new().
1694  *
1695  * Since: 2.6
1696  **/
1697 void
1698 g_option_group_set_parse_hooks (GOptionGroup     *group,
1699                                 GOptionParseFunc  pre_parse_func,
1700                                 GOptionParseFunc  post_parse_func)
1701 {
1702   g_return_if_fail (group != NULL);
1703
1704   group->pre_parse_func = pre_parse_func;
1705   group->post_parse_func = post_parse_func;  
1706 }
1707
1708 /**
1709  * g_option_group_set_error_hook:
1710  * @group: a #GOptionGroup
1711  * @error_func: a function to call when an error occurs
1712  * 
1713  * Associates a function with @group which will be called 
1714  * from g_option_context_parse() when an error occurs.
1715  *
1716  * Note that the user data to be passed to @pre_parse_func and
1717  * @post_parse_func can be specified when constructing the group
1718  * with g_option_group_new().
1719  *
1720  * Since: 2.6
1721  **/
1722 void
1723 g_option_group_set_error_hook (GOptionGroup     *group,
1724                                GOptionErrorFunc  error_func)
1725 {
1726   g_return_if_fail (group != NULL);
1727
1728   group->error_func = error_func;  
1729 }
1730
1731
1732 /**
1733  * g_option_group_set_translate_func:
1734  * @group: a #GOptionGroup
1735  * @func: the #GTranslateFunc, or %NULL 
1736  * @data: user data to pass to @func, or %NULL
1737  * @destroy_notify: a function which gets called to free @data, or %NULL
1738  * 
1739  * Sets the function which is used to translate user-visible
1740  * strings, for <option>--help</option> output. Different
1741  * groups can use different #GTranslateFunc<!-- -->s. If @func
1742  * is %NULL, strings are not translated.
1743  *
1744  * If you are using gettext(), you only need to set the translation
1745  * domain, see g_option_group_set_translation_domain().
1746  *
1747  * Since: 2.6
1748  **/
1749 void
1750 g_option_group_set_translate_func (GOptionGroup   *group,
1751                                    GTranslateFunc  func,
1752                                    gpointer        data,
1753                                    GDestroyNotify  destroy_notify)
1754 {
1755   g_return_if_fail (group != NULL);
1756   
1757   if (group->translate_notify)
1758     group->translate_notify (group->translate_data);
1759       
1760   group->translate_func = func;
1761   group->translate_data = data;
1762   group->translate_notify = destroy_notify;
1763 }
1764
1765 static gchar *
1766 dgettext_swapped (const gchar *msgid, 
1767                   const gchar *domainname)
1768 {
1769   return dgettext (domainname, msgid);
1770 }
1771
1772 /**
1773  * g_option_group_set_translation_domain:
1774  * @group: a #GOptionGroup
1775  * @domain: the domain to use
1776  * 
1777  * A convenience function to use gettext() for translating
1778  * user-visible strings. 
1779  * 
1780  * Since: 2.6
1781  **/
1782 void
1783 g_option_group_set_translation_domain (GOptionGroup *group,
1784                                        const gchar  *domain)
1785 {
1786   g_return_if_fail (group != NULL);
1787
1788   g_option_group_set_translate_func (group, 
1789                                      (GTranslateFunc)dgettext_swapped,
1790                                      g_strdup (domain),
1791                                      g_free);
1792
1793
1794 #define __G_OPTION_C__
1795 #include "galiasdef.c"