Only set the prgname if it hasn't been set before. (#334611, Chong Kai
[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               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 (!g_get_prgname())
1236     {
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         g_set_prgname ("<unknown>");
1247     }
1248
1249   /* Call pre-parse hooks */
1250   list = context->groups;
1251   while (list)
1252     {
1253       GOptionGroup *group = list->data;
1254       
1255       if (group->pre_parse_func)
1256         {
1257           if (!(* group->pre_parse_func) (context, group,
1258                                           group->user_data, error))
1259             goto fail;
1260         }
1261       
1262       list = list->next;
1263     }
1264
1265   if (context->main_group && context->main_group->pre_parse_func)
1266     {
1267       if (!(* context->main_group->pre_parse_func) (context, context->main_group,
1268                                                     context->main_group->user_data, error))
1269         goto fail;
1270     }
1271
1272   if (argc && argv)
1273     {
1274       gboolean stop_parsing = FALSE;
1275       gboolean has_unknown = FALSE;
1276       gint separator_pos = 0;
1277
1278       for (i = 1; i < *argc; i++)
1279         {
1280           gchar *arg, *dash;
1281           gboolean parsed = FALSE;
1282
1283           if ((*argv)[i][0] == '-' && (*argv)[i][1] != '\0' && !stop_parsing)
1284             {
1285               if ((*argv)[i][1] == '-')
1286                 {
1287                   /* -- option */
1288
1289                   arg = (*argv)[i] + 2;
1290
1291                   /* '--' terminates list of arguments */
1292                   if (*arg == 0)
1293                     {
1294                       separator_pos = i;
1295                       stop_parsing = TRUE;
1296                       continue;
1297                     }
1298
1299                   /* Handle help options */
1300                   if (context->help_enabled)
1301                     {
1302                       if (strcmp (arg, "help") == 0)
1303                         print_help (context, TRUE, NULL);
1304                       else if (strcmp (arg, "help-all") == 0)
1305                         print_help (context, FALSE, NULL);                    
1306                       else if (strncmp (arg, "help-", 5) == 0)
1307                         {
1308                           GList *list;
1309                           
1310                           list = context->groups;
1311                           
1312                           while (list)
1313                             {
1314                               GOptionGroup *group = list->data;
1315                               
1316                               if (strcmp (arg + 5, group->name) == 0)
1317                                 print_help (context, FALSE, group);                                           
1318                               
1319                               list = list->next;
1320                             }
1321                         }
1322                     }
1323
1324                   if (context->main_group &&
1325                       !parse_long_option (context, context->main_group, &i, arg,
1326                                           FALSE, argc, argv, error, &parsed))
1327                     goto fail;
1328
1329                   if (parsed)
1330                     continue;
1331                   
1332                   /* Try the groups */
1333                   list = context->groups;
1334                   while (list)
1335                     {
1336                       GOptionGroup *group = list->data;
1337                       
1338                       if (!parse_long_option (context, group, &i, arg, 
1339                                               FALSE, argc, argv, error, &parsed))
1340                         goto fail;
1341                       
1342                       if (parsed)
1343                         break;
1344                       
1345                       list = list->next;
1346                     }
1347                   
1348                   if (parsed)
1349                     continue;
1350
1351                   /* Now look for --<group>-<option> */
1352                   dash = strchr (arg, '-');
1353                   if (dash)
1354                     {
1355                       /* Try the groups */
1356                       list = context->groups;
1357                       while (list)
1358                         {
1359                           GOptionGroup *group = list->data;
1360                           
1361                           if (strncmp (group->name, arg, dash - arg) == 0)
1362                             {
1363                               if (!parse_long_option (context, group, &i, dash + 1,
1364                                                       TRUE, argc, argv, error, &parsed))
1365                                 goto fail;
1366                               
1367                               if (parsed)
1368                                 break;
1369                             }
1370                           
1371                           list = list->next;
1372                         }
1373                     }
1374                   
1375                   if (context->ignore_unknown)
1376                     continue;
1377                 }
1378               else
1379                 {
1380                   /* short option */
1381
1382                   gint new_i, j;
1383                   gboolean *nulled_out = NULL;
1384                   
1385                   arg = (*argv)[i] + 1;
1386
1387                   new_i = i;
1388
1389                   if (context->ignore_unknown)
1390                     nulled_out = g_new0 (gboolean, strlen (arg));
1391                   
1392                   for (j = 0; j < strlen (arg); j++)
1393                     {
1394                       if (context->help_enabled && arg[j] == '?')
1395                         print_help (context, TRUE, NULL);
1396                       
1397                       parsed = FALSE;
1398                       
1399                       if (context->main_group &&
1400                           !parse_short_option (context, context->main_group,
1401                                                i, &new_i, arg[j],
1402                                                argc, argv, error, &parsed))
1403                         {
1404
1405                           g_free (nulled_out);
1406                           goto fail;
1407                         }
1408
1409                       if (!parsed)
1410                         {
1411                           /* Try the groups */
1412                           list = context->groups;
1413                           while (list)
1414                             {
1415                               GOptionGroup *group = list->data;
1416                               
1417                               if (!parse_short_option (context, group, i, &new_i, arg[j],
1418                                                        argc, argv, error, &parsed))
1419                                 goto fail;
1420                               
1421                               if (parsed)
1422                                 break;
1423                           
1424                               list = list->next;
1425                             }
1426                         }
1427
1428                       if (context->ignore_unknown)
1429                         {
1430                           if (parsed)
1431                             nulled_out[j] = TRUE;
1432                           else
1433                             continue;
1434                         }
1435
1436                       if (!parsed)
1437                         break;
1438                     }
1439
1440                   if (context->ignore_unknown)
1441                     {
1442                       gchar *new_arg = NULL; 
1443                       gint arg_index = 0;
1444                       
1445                       for (j = 0; j < strlen (arg); j++)
1446                         {
1447                           if (!nulled_out[j])
1448                             {
1449                               if (!new_arg)
1450                                 new_arg = g_malloc (strlen (arg) + 1);
1451                               new_arg[arg_index++] = arg[j];
1452                             }
1453                         }
1454                       if (new_arg)
1455                         new_arg[arg_index] = '\0';
1456                       
1457                       add_pending_null (context, &((*argv)[i]), new_arg);
1458                     }
1459                   else if (parsed)
1460                     {
1461                       add_pending_null (context, &((*argv)[i]), NULL);
1462                       i = new_i;
1463                     }
1464                 }
1465               
1466               if (!parsed)
1467                 has_unknown = TRUE;
1468
1469               if (!parsed && !context->ignore_unknown)
1470                 {
1471                   g_set_error (error,
1472                                G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
1473                                    _("Unknown option %s"), (*argv)[i]);
1474                   goto fail;
1475                 }
1476             }
1477           else
1478             {
1479               /* Collect remaining args */
1480               if (context->main_group &&
1481                   !parse_remaining_arg (context, context->main_group, &i,
1482                                         argc, argv, error, &parsed))
1483                 goto fail;
1484               
1485               if (!parsed && (has_unknown || (*argv)[i][0] == '-'))
1486                 separator_pos = 0;
1487             }
1488         }
1489
1490       if (separator_pos > 0)
1491         add_pending_null (context, &((*argv)[separator_pos]), NULL);
1492         
1493     }
1494
1495   /* Call post-parse hooks */
1496   list = context->groups;
1497   while (list)
1498     {
1499       GOptionGroup *group = list->data;
1500       
1501       if (group->post_parse_func)
1502         {
1503           if (!(* group->post_parse_func) (context, group,
1504                                            group->user_data, error))
1505             goto fail;
1506         }
1507       
1508       list = list->next;
1509     }
1510   
1511   if (context->main_group && context->main_group->post_parse_func)
1512     {
1513       if (!(* context->main_group->post_parse_func) (context, context->main_group,
1514                                                      context->main_group->user_data, error))
1515         goto fail;
1516     }
1517   
1518   if (argc && argv)
1519     {
1520       free_pending_nulls (context, TRUE);
1521       
1522       for (i = 1; i < *argc; i++)
1523         {
1524           for (k = i; k < *argc; k++)
1525             if ((*argv)[k] != NULL)
1526               break;
1527           
1528           if (k > i)
1529             {
1530               k -= i;
1531               for (j = i + k; j < *argc; j++)
1532                 {
1533                   (*argv)[j-k] = (*argv)[j];
1534                   (*argv)[j] = NULL;
1535                 }
1536               *argc -= k;
1537             }
1538         }      
1539     }
1540
1541   return TRUE;
1542
1543  fail:
1544   
1545   /* Call error hooks */
1546   list = context->groups;
1547   while (list)
1548     {
1549       GOptionGroup *group = list->data;
1550       
1551       if (group->error_func)
1552         (* group->error_func) (context, group,
1553                                group->user_data, error);
1554       
1555       list = list->next;
1556     }
1557
1558   if (context->main_group && context->main_group->error_func)
1559     (* context->main_group->error_func) (context, context->main_group,
1560                                          context->main_group->user_data, error);
1561   
1562   free_changes_list (context, TRUE);
1563   free_pending_nulls (context, FALSE);
1564
1565   return FALSE;
1566 }
1567                                    
1568 /**
1569  * g_option_group_new:
1570  * @name: the name for the option group, this is used to provide
1571  *   help for the options in this group with <option>--help-</option>@name
1572  * @description: a description for this group to be shown in 
1573  *   <option>--help</option>. This string is translated using the translation
1574  *   domain or translation function of the group
1575  * @help_description: a description for the <option>--help-</option>@name option.
1576  *   This string is translated using the translation domain or translation function
1577  *   of the group
1578  * @user_data: user data that will be passed to the pre- and post-parse hooks,
1579  *   the error hook and to callbacks of %G_OPTION_ARG_CALLBACK options, or %NULL
1580  * @destroy: a function that will be called to free @user_data, or %NULL
1581  * 
1582  * Creates a new #GOptionGroup.
1583  * 
1584  * Return value: a newly created option group. It should be added 
1585  *   to a #GOptionContext or freed with g_option_group_free().
1586  *
1587  * Since: 2.6
1588  **/
1589 GOptionGroup *
1590 g_option_group_new (const gchar    *name,
1591                     const gchar    *description,
1592                     const gchar    *help_description,
1593                     gpointer        user_data,
1594                     GDestroyNotify  destroy)
1595
1596 {
1597   GOptionGroup *group;
1598
1599   group = g_new0 (GOptionGroup, 1);
1600   group->name = g_strdup (name);
1601   group->description = g_strdup (description);
1602   group->help_description = g_strdup (help_description);
1603   group->user_data = user_data;
1604   group->destroy_notify = destroy;
1605   
1606   return group;
1607 }
1608
1609
1610 /**
1611  * g_option_group_free:
1612  * @group: a #GOptionGroup
1613  * 
1614  * Frees a #GOptionGroup. Note that you must <emphasis>not</emphasis>
1615  * free groups which have been added to a #GOptionContext.
1616  *
1617  * Since: 2.6
1618  **/
1619 void
1620 g_option_group_free (GOptionGroup *group)
1621 {
1622   g_return_if_fail (group != NULL);
1623
1624   g_free (group->name);
1625   g_free (group->description);
1626   g_free (group->help_description);
1627
1628   g_free (group->entries);
1629   
1630   if (group->destroy_notify)
1631     (* group->destroy_notify) (group->user_data);
1632
1633   if (group->translate_notify)
1634     (* group->translate_notify) (group->translate_data);
1635   
1636   g_free (group);
1637 }
1638
1639
1640 /**
1641  * g_option_group_add_entries:
1642  * @group: a #GOptionGroup
1643  * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
1644  * 
1645  * Adds the options specified in @entries to @group.
1646  *
1647  * Since: 2.6
1648  **/
1649 void
1650 g_option_group_add_entries (GOptionGroup       *group,
1651                             const GOptionEntry *entries)
1652 {
1653   gint i, n_entries;
1654   
1655   g_return_if_fail (entries != NULL);
1656
1657   for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++) ;
1658
1659   group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
1660
1661   memcpy (group->entries + group->n_entries, entries, sizeof (GOptionEntry) * n_entries);
1662
1663   for (i = group->n_entries; i < group->n_entries + n_entries; i++)
1664     {
1665       gchar c = group->entries[i].short_name;
1666
1667       if (c)
1668         {
1669           if (c == '-' || !g_ascii_isprint (c))
1670             {
1671               g_warning (G_STRLOC": ignoring invalid short option '%c' (%d)", c, c);
1672               group->entries[i].short_name = 0;
1673             }
1674         }
1675     }
1676
1677   group->n_entries += n_entries;
1678 }
1679
1680 /**
1681  * g_option_group_set_parse_hooks:
1682  * @group: a #GOptionGroup
1683  * @pre_parse_func: a function to call before parsing, or %NULL
1684  * @post_parse_func: a function to call after parsing, or %NULL
1685  * 
1686  * Associates two functions with @group which will be called 
1687  * from g_option_context_parse() before the first option is parsed
1688  * and after the last option has been parsed, respectively.
1689  *
1690  * Note that the user data to be passed to @pre_parse_func and
1691  * @post_parse_func can be specified when constructing the group
1692  * with g_option_group_new().
1693  *
1694  * Since: 2.6
1695  **/
1696 void
1697 g_option_group_set_parse_hooks (GOptionGroup     *group,
1698                                 GOptionParseFunc  pre_parse_func,
1699                                 GOptionParseFunc  post_parse_func)
1700 {
1701   g_return_if_fail (group != NULL);
1702
1703   group->pre_parse_func = pre_parse_func;
1704   group->post_parse_func = post_parse_func;  
1705 }
1706
1707 /**
1708  * g_option_group_set_error_hook:
1709  * @group: a #GOptionGroup
1710  * @error_func: a function to call when an error occurs
1711  * 
1712  * Associates a function with @group which will be called 
1713  * from g_option_context_parse() when an error occurs.
1714  *
1715  * Note that the user data to be passed to @pre_parse_func and
1716  * @post_parse_func can be specified when constructing the group
1717  * with g_option_group_new().
1718  *
1719  * Since: 2.6
1720  **/
1721 void
1722 g_option_group_set_error_hook (GOptionGroup     *group,
1723                                GOptionErrorFunc  error_func)
1724 {
1725   g_return_if_fail (group != NULL);
1726
1727   group->error_func = error_func;  
1728 }
1729
1730
1731 /**
1732  * g_option_group_set_translate_func:
1733  * @group: a #GOptionGroup
1734  * @func: the #GTranslateFunc, or %NULL 
1735  * @data: user data to pass to @func, or %NULL
1736  * @destroy_notify: a function which gets called to free @data, or %NULL
1737  * 
1738  * Sets the function which is used to translate user-visible
1739  * strings, for <option>--help</option> output. Different
1740  * groups can use different #GTranslateFunc<!-- -->s. If @func
1741  * is %NULL, strings are not translated.
1742  *
1743  * If you are using gettext(), you only need to set the translation
1744  * domain, see g_option_group_set_translation_domain().
1745  *
1746  * Since: 2.6
1747  **/
1748 void
1749 g_option_group_set_translate_func (GOptionGroup   *group,
1750                                    GTranslateFunc  func,
1751                                    gpointer        data,
1752                                    GDestroyNotify  destroy_notify)
1753 {
1754   g_return_if_fail (group != NULL);
1755   
1756   if (group->translate_notify)
1757     group->translate_notify (group->translate_data);
1758       
1759   group->translate_func = func;
1760   group->translate_data = data;
1761   group->translate_notify = destroy_notify;
1762 }
1763
1764 static gchar *
1765 dgettext_swapped (const gchar *msgid, 
1766                   const gchar *domainname)
1767 {
1768   return dgettext (domainname, msgid);
1769 }
1770
1771 /**
1772  * g_option_group_set_translation_domain:
1773  * @group: a #GOptionGroup
1774  * @domain: the domain to use
1775  * 
1776  * A convenience function to use gettext() for translating
1777  * user-visible strings. 
1778  * 
1779  * Since: 2.6
1780  **/
1781 void
1782 g_option_group_set_translation_domain (GOptionGroup *group,
1783                                        const gchar  *domain)
1784 {
1785   g_return_if_fail (group != NULL);
1786
1787   g_option_group_set_translate_func (group, 
1788                                      (GTranslateFunc)dgettext_swapped,
1789                                      g_strdup (domain),
1790                                      g_free);
1791
1792
1793 #define __G_OPTION_C__
1794 #include "galiasdef.c"