Merge remote-tracking branch 'gvdb/master'
[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 /**
23  * SECTION:option
24  * @Short_description: parses commandline options
25  * @Title: Commandline option parser
26  *
27  * The GOption commandline parser is intended to be a simpler replacement
28  * for the popt library. It supports short and long commandline options,
29  * as shown in the following example:
30  *
31  * <literal>testtreemodel -r 1 --max-size 20 --rand --display=:1.0 -vb -- file1 file2</literal>
32  *
33  * The example demonstrates a number of features of the GOption
34  * commandline parser
35  * <itemizedlist><listitem><para>
36  *   Options can be single letters, prefixed by a single dash. Multiple
37  *   short options can be grouped behind a single dash.
38  * </para></listitem><listitem><para>
39  *   Long options are prefixed by two consecutive dashes.
40  * </para></listitem><listitem><para>
41  *   Options can have an extra argument, which can be a number, a string or
42  *   a filename. For long options, the extra argument can be appended with
43  *   an equals sign after the option name, which is useful if the extra
44  *   argument starts with a dash, which would otherwise cause it to be
45  *   interpreted as another option.
46  * </para></listitem><listitem><para>
47  *   Non-option arguments are returned to the application as rest arguments.
48  * </para></listitem><listitem><para>
49  *   An argument consisting solely of two dashes turns off further parsing,
50  *   any remaining arguments (even those starting with a dash) are returned
51  *   to the application as rest arguments.
52  * </para></listitem></itemizedlist>
53  *
54  * Another important feature of GOption is that it can automatically
55  * generate nicely formatted help output. Unless it is explicitly turned
56  * off with g_option_context_set_help_enabled(), GOption will recognize
57  * the <option>--help</option>, <option>-?</option>,
58  * <option>--help-all</option> and
59  * <option>--help-</option><replaceable>groupname</replaceable> options
60  * (where <replaceable>groupname</replaceable> is the name of a
61  * #GOptionGroup) and write a text similar to the one shown in the
62  * following example to stdout.
63  *
64  * <informalexample><screen>
65  * Usage:
66  *   testtreemodel [OPTION...] - test tree model performance
67  *  
68  * Help Options:
69  *   -h, --help               Show help options
70  *   --help-all               Show all help options
71  *   --help-gtk               Show GTK+ Options
72  *  
73  * Application Options:
74  *   -r, --repeats=N          Average over N repetitions
75  *   -m, --max-size=M         Test up to 2^M items
76  *   --display=DISPLAY        X display to use
77  *   -v, --verbose            Be verbose
78  *   -b, --beep               Beep when done
79  *   --rand                   Randomize the data
80  * </screen></informalexample>
81  *
82  * GOption groups options in #GOptionGroup<!-- -->s, which makes it easy to
83  * incorporate options from multiple sources. The intended use for this is
84  * to let applications collect option groups from the libraries it uses,
85  * add them to their #GOptionContext, and parse all options by a single call
86  * to g_option_context_parse(). See gtk_get_option_group() for an example.
87  *
88  * If an option is declared to be of type string or filename, GOption takes
89  * care of converting it to the right encoding; strings are returned in
90  * UTF-8, filenames are returned in the GLib filename encoding. Note that
91  * this only works if setlocale() has been called before
92  * g_option_context_parse().
93  *
94  * Here is a complete example of setting up GOption to parse the example
95  * commandline above and produce the example help output.
96  *
97  * <informalexample><programlisting>
98  * static gint repeats = 2;
99  * static gint max_size = 8;
100  * static gboolean verbose = FALSE;
101  * static gboolean beep = FALSE;
102  * static gboolean rand = FALSE;
103  *
104  * static GOptionEntry entries[] =
105  * {
106  *   { "repeats", 'r', 0, G_OPTION_ARG_INT, &repeats, "Average over N repetitions", "N" },
107  *   { "max-size", 'm', 0, G_OPTION_ARG_INT, &max_size, "Test up to 2^M items", "M" },
108  *   { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL },
109  *   { "beep", 'b', 0, G_OPTION_ARG_NONE, &beep, "Beep when done", NULL },
110  *   { "rand", 0, 0, G_OPTION_ARG_NONE, &rand, "Randomize the data", NULL },
111  *   { NULL }
112  * };
113  *
114  * int
115  * main (int argc, char *argv[])
116  * {
117  *   GError *error = NULL;
118  *   GOptionContext *context;
119  *
120  *   context = g_option_context_new ("- test tree model performance");
121  *   g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
122  *   g_option_context_add_group (context, gtk_get_option_group (TRUE));
123  *   if (!g_option_context_parse (context, &argc, &argv, &error))
124  *     {
125  *       g_print ("option parsing failed: %s\n", error->message);
126  *       exit (1);
127  *     }
128  *
129  *   /&ast; ... &ast;/
130  *
131  * }
132  * </programlisting></informalexample>
133  */
134
135 #include "config.h"
136
137 #include <string.h>
138 #include <stdlib.h>
139 #include <stdio.h>
140 #include <errno.h>
141
142 #include "goption.h"
143
144 #include "gprintf.h"
145 #include "glibintl.h"
146
147 #define TRANSLATE(group, str) (((group)->translate_func ? (* (group)->translate_func) ((str), (group)->translate_data) : (str)))
148
149 #define NO_ARG(entry) ((entry)->arg == G_OPTION_ARG_NONE ||       \
150                        ((entry)->arg == G_OPTION_ARG_CALLBACK &&  \
151                         ((entry)->flags & G_OPTION_FLAG_NO_ARG)))
152
153 #define OPTIONAL_ARG(entry) ((entry)->arg == G_OPTION_ARG_CALLBACK &&  \
154                        (entry)->flags & G_OPTION_FLAG_OPTIONAL_ARG)
155
156 typedef struct
157 {
158   GOptionArg arg_type;
159   gpointer arg_data;
160   union
161   {
162     gboolean bool;
163     gint integer;
164     gchar *str;
165     gchar **array;
166     gdouble dbl;
167     gint64 int64;
168   } prev;
169   union
170   {
171     gchar *str;
172     struct
173     {
174       gint len;
175       gchar **data;
176     } array;
177   } allocated;
178 } Change;
179
180 typedef struct
181 {
182   gchar **ptr;
183   gchar *value;
184 } PendingNull;
185
186 struct _GOptionContext
187 {
188   GList           *groups;
189
190   gchar           *parameter_string;
191   gchar           *summary;
192   gchar           *description;
193
194   GTranslateFunc   translate_func;
195   GDestroyNotify   translate_notify;
196   gpointer         translate_data;
197
198   guint            help_enabled   : 1;
199   guint            ignore_unknown : 1;
200
201   GOptionGroup    *main_group;
202
203   /* We keep a list of change so we can revert them */
204   GList           *changes;
205
206   /* We also keep track of all argv elements
207    * that should be NULLed or modified.
208    */
209   GList           *pending_nulls;
210 };
211
212 struct _GOptionGroup
213 {
214   gchar           *name;
215   gchar           *description;
216   gchar           *help_description;
217
218   GDestroyNotify   destroy_notify;
219   gpointer         user_data;
220
221   GTranslateFunc   translate_func;
222   GDestroyNotify   translate_notify;
223   gpointer         translate_data;
224
225   GOptionEntry    *entries;
226   gint             n_entries;
227
228   GOptionParseFunc pre_parse_func;
229   GOptionParseFunc post_parse_func;
230   GOptionErrorFunc error_func;
231 };
232
233 static void free_changes_list (GOptionContext *context,
234                                gboolean        revert);
235 static void free_pending_nulls (GOptionContext *context,
236                                 gboolean        perform_nulls);
237
238
239 static int
240 _g_unichar_get_width (gunichar c)
241 {
242   if (G_UNLIKELY (g_unichar_iszerowidth (c)))
243     return 0;
244
245   /* we ignore the fact that we should call g_unichar_iswide_cjk() under
246    * some locales (legacy East Asian ones) */
247   if (g_unichar_iswide (c))
248     return 2;
249
250   return 1;
251 }
252
253 static glong
254 _g_utf8_strwidth (const gchar *p,
255                   gssize       max)
256 {
257   glong len = 0;
258   const gchar *start = p;
259   g_return_val_if_fail (p != NULL || max == 0, 0);
260
261   if (max < 0)
262     {
263       while (*p)
264         {
265           len += _g_unichar_get_width (g_utf8_get_char (p));
266           p = g_utf8_next_char (p);
267         }
268     }
269   else
270     {
271       if (max == 0 || !*p)
272         return 0;
273
274       /* this case may not be quite correct */
275
276       len += _g_unichar_get_width (g_utf8_get_char (p));
277       p = g_utf8_next_char (p);
278
279       while (p - start < max && *p)
280         {
281           len += _g_unichar_get_width (g_utf8_get_char (p));
282           p = g_utf8_next_char (p);
283         }
284     }
285
286   return len;
287 }
288
289
290 GQuark
291 g_option_error_quark (void)
292 {
293   return g_quark_from_static_string ("g-option-context-error-quark");
294 }
295
296 /**
297  * g_option_context_new:
298  * @parameter_string: a string which is displayed in
299  *    the first line of <option>--help</option> output, after the
300  *    usage summary
301  *    <literal><replaceable>programname</replaceable> [OPTION...]</literal>
302  *
303  * Creates a new option context.
304  *
305  * The @parameter_string can serve multiple purposes. It can be used
306  * to add descriptions for "rest" arguments, which are not parsed by
307  * the #GOptionContext, typically something like "FILES" or
308  * "FILE1 FILE2...". If you are using #G_OPTION_REMAINING for
309  * collecting "rest" arguments, GLib handles this automatically by
310  * using the @arg_description of the corresponding #GOptionEntry in
311  * the usage summary.
312  *
313  * Another usage is to give a short summary of the program
314  * functionality, like " - frob the strings", which will be displayed
315  * in the same line as the usage. For a longer description of the
316  * program functionality that should be displayed as a paragraph
317  * below the usage line, use g_option_context_set_summary().
318  *
319  * Note that the @parameter_string is translated using the
320  * function set with g_option_context_set_translate_func(), so
321  * it should normally be passed untranslated.
322  *
323  * Returns: a newly created #GOptionContext, which must be
324  *    freed with g_option_context_free() after use.
325  *
326  * Since: 2.6
327  */
328 GOptionContext *
329 g_option_context_new (const gchar *parameter_string)
330
331 {
332   GOptionContext *context;
333
334   context = g_new0 (GOptionContext, 1);
335
336   context->parameter_string = g_strdup (parameter_string);
337   context->help_enabled = TRUE;
338   context->ignore_unknown = FALSE;
339
340   return context;
341 }
342
343 /**
344  * g_option_context_free:
345  * @context: a #GOptionContext
346  *
347  * Frees context and all the groups which have been
348  * added to it.
349  *
350  * Please note that parsed arguments need to be freed separately (see
351  * #GOptionEntry).
352  *
353  * Since: 2.6
354  */
355 void g_option_context_free (GOptionContext *context)
356 {
357   g_return_if_fail (context != NULL);
358
359   g_list_free_full (context->groups, (GDestroyNotify) g_option_group_free);
360
361   if (context->main_group)
362     g_option_group_free (context->main_group);
363
364   free_changes_list (context, FALSE);
365   free_pending_nulls (context, FALSE);
366
367   g_free (context->parameter_string);
368   g_free (context->summary);
369   g_free (context->description);
370
371   if (context->translate_notify)
372     (* context->translate_notify) (context->translate_data);
373
374   g_free (context);
375 }
376
377
378 /**
379  * g_option_context_set_help_enabled:
380  * @context: a #GOptionContext
381  * @help_enabled: %TRUE to enable <option>--help</option>, %FALSE to disable it
382  *
383  * Enables or disables automatic generation of <option>--help</option>
384  * output. By default, g_option_context_parse() recognizes
385  * <option>--help</option>, <option>-h</option>,
386  * <option>-?</option>, <option>--help-all</option>
387  * and <option>--help-</option><replaceable>groupname</replaceable> and creates
388  * suitable output to stdout.
389  *
390  * Since: 2.6
391  */
392 void g_option_context_set_help_enabled (GOptionContext *context,
393                                         gboolean        help_enabled)
394
395 {
396   g_return_if_fail (context != NULL);
397
398   context->help_enabled = help_enabled;
399 }
400
401 /**
402  * g_option_context_get_help_enabled:
403  * @context: a #GOptionContext
404  *
405  * Returns whether automatic <option>--help</option> generation
406  * is turned on for @context. See g_option_context_set_help_enabled().
407  *
408  * Returns: %TRUE if automatic help generation is turned on.
409  *
410  * Since: 2.6
411  */
412 gboolean
413 g_option_context_get_help_enabled (GOptionContext *context)
414 {
415   g_return_val_if_fail (context != NULL, FALSE);
416
417   return context->help_enabled;
418 }
419
420 /**
421  * g_option_context_set_ignore_unknown_options:
422  * @context: a #GOptionContext
423  * @ignore_unknown: %TRUE to ignore unknown options, %FALSE to produce
424  *    an error when unknown options are met
425  *
426  * Sets whether to ignore unknown options or not. If an argument is
427  * ignored, it is left in the @argv array after parsing. By default,
428  * g_option_context_parse() treats unknown options as error.
429  *
430  * This setting does not affect non-option arguments (i.e. arguments
431  * which don't start with a dash). But note that GOption cannot reliably
432  * determine whether a non-option belongs to a preceding unknown option.
433  *
434  * Since: 2.6
435  **/
436 void
437 g_option_context_set_ignore_unknown_options (GOptionContext *context,
438                                              gboolean        ignore_unknown)
439 {
440   g_return_if_fail (context != NULL);
441
442   context->ignore_unknown = ignore_unknown;
443 }
444
445 /**
446  * g_option_context_get_ignore_unknown_options:
447  * @context: a #GOptionContext
448  *
449  * Returns whether unknown options are ignored or not. See
450  * g_option_context_set_ignore_unknown_options().
451  *
452  * Returns: %TRUE if unknown options are ignored.
453  *
454  * Since: 2.6
455  **/
456 gboolean
457 g_option_context_get_ignore_unknown_options (GOptionContext *context)
458 {
459   g_return_val_if_fail (context != NULL, FALSE);
460
461   return context->ignore_unknown;
462 }
463
464 /**
465  * g_option_context_add_group:
466  * @context: a #GOptionContext
467  * @group: the group to add
468  *
469  * Adds a #GOptionGroup to the @context, so that parsing with @context
470  * will recognize the options in the group. Note that the group will
471  * be freed together with the context when g_option_context_free() is
472  * called, so you must not free the group yourself after adding it
473  * to a context.
474  *
475  * Since: 2.6
476  **/
477 void
478 g_option_context_add_group (GOptionContext *context,
479                             GOptionGroup   *group)
480 {
481   GList *list;
482
483   g_return_if_fail (context != NULL);
484   g_return_if_fail (group != NULL);
485   g_return_if_fail (group->name != NULL);
486   g_return_if_fail (group->description != NULL);
487   g_return_if_fail (group->help_description != NULL);
488
489   for (list = context->groups; list; list = list->next)
490     {
491       GOptionGroup *g = (GOptionGroup *)list->data;
492
493       if ((group->name == NULL && g->name == NULL) ||
494           (group->name && g->name && strcmp (group->name, g->name) == 0))
495         g_warning ("A group named \"%s\" is already part of this GOptionContext",
496                    group->name);
497     }
498
499   context->groups = g_list_append (context->groups, group);
500 }
501
502 /**
503  * g_option_context_set_main_group:
504  * @context: a #GOptionContext
505  * @group: the group to set as main group
506  *
507  * Sets a #GOptionGroup as main group of the @context.
508  * This has the same effect as calling g_option_context_add_group(),
509  * the only difference is that the options in the main group are
510  * treated differently when generating <option>--help</option> output.
511  *
512  * Since: 2.6
513  **/
514 void
515 g_option_context_set_main_group (GOptionContext *context,
516                                  GOptionGroup   *group)
517 {
518   g_return_if_fail (context != NULL);
519   g_return_if_fail (group != NULL);
520
521   if (context->main_group)
522     {
523       g_warning ("This GOptionContext already has a main group");
524
525       return;
526     }
527
528   context->main_group = group;
529 }
530
531 /**
532  * g_option_context_get_main_group:
533  * @context: a #GOptionContext
534  *
535  * Returns a pointer to the main group of @context.
536  *
537  * Return value: the main group of @context, or %NULL if @context doesn't
538  *  have a main group. Note that group belongs to @context and should
539  *  not be modified or freed.
540  *
541  * Since: 2.6
542  **/
543 GOptionGroup *
544 g_option_context_get_main_group (GOptionContext *context)
545 {
546   g_return_val_if_fail (context != NULL, NULL);
547
548   return context->main_group;
549 }
550
551 /**
552  * g_option_context_add_main_entries:
553  * @context: a #GOptionContext
554  * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
555  * @translation_domain: a translation domain to use for translating
556  *    the <option>--help</option> output for the options in @entries
557  *    with gettext(), or %NULL
558  *
559  * A convenience function which creates a main group if it doesn't
560  * exist, adds the @entries to it and sets the translation domain.
561  *
562  * Since: 2.6
563  **/
564 void
565 g_option_context_add_main_entries (GOptionContext      *context,
566                                    const GOptionEntry  *entries,
567                                    const gchar         *translation_domain)
568 {
569   g_return_if_fail (entries != NULL);
570
571   if (!context->main_group)
572     context->main_group = g_option_group_new (NULL, NULL, NULL, NULL, NULL);
573
574   g_option_group_add_entries (context->main_group, entries);
575   g_option_group_set_translation_domain (context->main_group, translation_domain);
576 }
577
578 static gint
579 calculate_max_length (GOptionGroup *group)
580 {
581   GOptionEntry *entry;
582   gint i, len, max_length;
583
584   max_length = 0;
585
586   for (i = 0; i < group->n_entries; i++)
587     {
588       entry = &group->entries[i];
589
590       if (entry->flags & G_OPTION_FLAG_HIDDEN)
591         continue;
592
593       len = _g_utf8_strwidth (entry->long_name, -1);
594
595       if (entry->short_name)
596         len += 4;
597
598       if (!NO_ARG (entry) && entry->arg_description)
599         len += 1 + _g_utf8_strwidth (TRANSLATE (group, entry->arg_description), -1);
600
601       max_length = MAX (max_length, len);
602     }
603
604   return max_length;
605 }
606
607 static void
608 print_entry (GOptionGroup       *group,
609              gint                max_length,
610              const GOptionEntry *entry,
611              GString            *string)
612 {
613   GString *str;
614
615   if (entry->flags & G_OPTION_FLAG_HIDDEN)
616     return;
617
618   if (entry->long_name[0] == 0)
619     return;
620
621   str = g_string_new (NULL);
622
623   if (entry->short_name)
624     g_string_append_printf (str, "  -%c, --%s", entry->short_name, entry->long_name);
625   else
626     g_string_append_printf (str, "  --%s", entry->long_name);
627
628   if (entry->arg_description)
629     g_string_append_printf (str, "=%s", TRANSLATE (group, entry->arg_description));
630
631   g_string_append_printf (string, "%s%*s %s\n", str->str,
632                           (int) (max_length + 4 - _g_utf8_strwidth (str->str, -1)), "",
633                           entry->description ? TRANSLATE (group, entry->description) : "");
634   g_string_free (str, TRUE);
635 }
636
637 static gboolean
638 group_has_visible_entries (GOptionContext *context,
639                            GOptionGroup *group,
640                            gboolean      main_entries)
641 {
642   GOptionFlags reject_filter = G_OPTION_FLAG_HIDDEN;
643   GOptionEntry *entry;
644   gint i, l;
645   gboolean main_group = group == context->main_group;
646
647   if (!main_entries)
648     reject_filter |= G_OPTION_FLAG_IN_MAIN;
649
650   for (i = 0, l = (group ? group->n_entries : 0); i < l; i++)
651     {
652       entry = &group->entries[i];
653
654       if (main_entries && !main_group && !(entry->flags & G_OPTION_FLAG_IN_MAIN))
655         continue;
656       if (!(entry->flags & reject_filter))
657         return TRUE;
658     }
659
660   return FALSE;
661 }
662
663 static gboolean
664 group_list_has_visible_entires (GOptionContext *context,
665                                 GList          *group_list,
666                                 gboolean       main_entries)
667 {
668   while (group_list)
669     {
670       if (group_has_visible_entries (context, group_list->data, main_entries))
671         return TRUE;
672
673       group_list = group_list->next;
674     }
675
676   return FALSE;
677 }
678
679 static gboolean
680 context_has_h_entry (GOptionContext *context)
681 {
682   gsize i;
683   GList *list;
684
685   if (context->main_group)
686     {
687       for (i = 0; i < context->main_group->n_entries; i++)
688         {
689           if (context->main_group->entries[i].short_name == 'h')
690             return TRUE;
691         }
692     }
693
694   for (list = context->groups; list != NULL; list = g_list_next (list))
695     {
696      GOptionGroup *group;
697
698       group = (GOptionGroup*)list->data;
699       for (i = 0; i < group->n_entries; i++)
700         {
701           if (group->entries[i].short_name == 'h')
702             return TRUE;
703         }
704     }
705   return FALSE;
706 }
707
708 /**
709  * g_option_context_get_help:
710  * @context: a #GOptionContext
711  * @main_help: if %TRUE, only include the main group
712  * @group: the #GOptionGroup to create help for, or %NULL
713  *
714  * Returns a formatted, translated help text for the given context.
715  * To obtain the text produced by <option>--help</option>, call
716  * <literal>g_option_context_get_help (context, TRUE, NULL)</literal>.
717  * To obtain the text produced by <option>--help-all</option>, call
718  * <literal>g_option_context_get_help (context, FALSE, NULL)</literal>.
719  * To obtain the help text for an option group, call
720  * <literal>g_option_context_get_help (context, FALSE, group)</literal>.
721  *
722  * Returns: A newly allocated string containing the help text
723  *
724  * Since: 2.14
725  */
726 gchar *
727 g_option_context_get_help (GOptionContext *context,
728                            gboolean        main_help,
729                            GOptionGroup   *group)
730 {
731   GList *list;
732   gint max_length, len;
733   gint i;
734   GOptionEntry *entry;
735   GHashTable *shadow_map;
736   gboolean seen[256];
737   const gchar *rest_description;
738   GString *string;
739   guchar token;
740
741   string = g_string_sized_new (1024);
742
743   rest_description = NULL;
744   if (context->main_group)
745     {
746
747       for (i = 0; i < context->main_group->n_entries; i++)
748         {
749           entry = &context->main_group->entries[i];
750           if (entry->long_name[0] == 0)
751             {
752               rest_description = TRANSLATE (context->main_group, entry->arg_description);
753               break;
754             }
755         }
756     }
757
758   g_string_append_printf (string, "%s\n  %s %s",
759                           _("Usage:"), g_get_prgname(), _("[OPTION...]"));
760
761   if (rest_description)
762     {
763       g_string_append (string, " ");
764       g_string_append (string, rest_description);
765     }
766
767   if (context->parameter_string)
768     {
769       g_string_append (string, " ");
770       g_string_append (string, TRANSLATE (context, context->parameter_string));
771     }
772
773   g_string_append (string, "\n\n");
774
775   if (context->summary)
776     {
777       g_string_append (string, TRANSLATE (context, context->summary));
778       g_string_append (string, "\n\n");
779     }
780
781   memset (seen, 0, sizeof (gboolean) * 256);
782   shadow_map = g_hash_table_new (g_str_hash, g_str_equal);
783
784   if (context->main_group)
785     {
786       for (i = 0; i < context->main_group->n_entries; i++)
787         {
788           entry = &context->main_group->entries[i];
789           g_hash_table_insert (shadow_map,
790                                (gpointer)entry->long_name,
791                                entry);
792
793           if (seen[(guchar)entry->short_name])
794             entry->short_name = 0;
795           else
796             seen[(guchar)entry->short_name] = TRUE;
797         }
798     }
799
800   list = context->groups;
801   while (list != NULL)
802     {
803       GOptionGroup *g = list->data;
804       for (i = 0; i < g->n_entries; i++)
805         {
806           entry = &g->entries[i];
807           if (g_hash_table_lookup (shadow_map, entry->long_name) &&
808               !(entry->flags & G_OPTION_FLAG_NOALIAS))
809             entry->long_name = g_strdup_printf ("%s-%s", g->name, entry->long_name);
810           else
811             g_hash_table_insert (shadow_map, (gpointer)entry->long_name, entry);
812
813           if (seen[(guchar)entry->short_name] &&
814               !(entry->flags & G_OPTION_FLAG_NOALIAS))
815             entry->short_name = 0;
816           else
817             seen[(guchar)entry->short_name] = TRUE;
818         }
819       list = list->next;
820     }
821
822   g_hash_table_destroy (shadow_map);
823
824   list = context->groups;
825
826   max_length = _g_utf8_strwidth ("-?, --help", -1);
827
828   if (list)
829     {
830       len = _g_utf8_strwidth ("--help-all", -1);
831       max_length = MAX (max_length, len);
832     }
833
834   if (context->main_group)
835     {
836       len = calculate_max_length (context->main_group);
837       max_length = MAX (max_length, len);
838     }
839
840   while (list != NULL)
841     {
842       GOptionGroup *g = list->data;
843
844       /* First, we check the --help-<groupname> options */
845       len = _g_utf8_strwidth ("--help-", -1) + _g_utf8_strwidth (g->name, -1);
846       max_length = MAX (max_length, len);
847
848       /* Then we go through the entries */
849       len = calculate_max_length (g);
850       max_length = MAX (max_length, len);
851
852       list = list->next;
853     }
854
855   /* Add a bit of padding */
856   max_length += 4;
857
858   if (!group)
859     {
860       list = context->groups;
861
862       token = context_has_h_entry (context) ? '?' : 'h';
863
864       g_string_append_printf (string, "%s\n  -%c, --%-*s %s\n",
865                               _("Help Options:"), token, max_length - 4, "help",
866                               _("Show help options"));
867
868       /* We only want --help-all when there are groups */
869       if (list)
870         g_string_append_printf (string, "  --%-*s %s\n",
871                                 max_length, "help-all",
872                                 _("Show all help options"));
873
874       while (list)
875         {
876           GOptionGroup *g = list->data;
877
878           if (group_has_visible_entries (context, g, FALSE))
879             g_string_append_printf (string, "  --help-%-*s %s\n",
880                                     max_length - 5, g->name,
881                                     TRANSLATE (g, g->help_description));
882
883           list = list->next;
884         }
885
886       g_string_append (string, "\n");
887     }
888
889   if (group)
890     {
891       /* Print a certain group */
892
893       if (group_has_visible_entries (context, group, FALSE))
894         {
895           g_string_append (string, TRANSLATE (group, group->description));
896           g_string_append (string, "\n");
897           for (i = 0; i < group->n_entries; i++)
898             print_entry (group, max_length, &group->entries[i], string);
899           g_string_append (string, "\n");
900         }
901     }
902   else if (!main_help)
903     {
904       /* Print all groups */
905
906       list = context->groups;
907
908       while (list)
909         {
910           GOptionGroup *g = list->data;
911
912           if (group_has_visible_entries (context, g, FALSE))
913             {
914               g_string_append (string, g->description);
915               g_string_append (string, "\n");
916               for (i = 0; i < g->n_entries; i++)
917                 if (!(g->entries[i].flags & G_OPTION_FLAG_IN_MAIN))
918                   print_entry (g, max_length, &g->entries[i], string);
919
920               g_string_append (string, "\n");
921             }
922
923           list = list->next;
924         }
925     }
926
927   /* Print application options if --help or --help-all has been specified */
928   if ((main_help || !group) &&
929       (group_has_visible_entries (context, context->main_group, TRUE) ||
930        group_list_has_visible_entires (context, context->groups, TRUE)))
931     {
932       list = context->groups;
933
934       g_string_append (string,  _("Application Options:"));
935       g_string_append (string, "\n");
936       if (context->main_group)
937         for (i = 0; i < context->main_group->n_entries; i++)
938           print_entry (context->main_group, max_length,
939                        &context->main_group->entries[i], string);
940
941       while (list != NULL)
942         {
943           GOptionGroup *g = list->data;
944
945           /* Print main entries from other groups */
946           for (i = 0; i < g->n_entries; i++)
947             if (g->entries[i].flags & G_OPTION_FLAG_IN_MAIN)
948               print_entry (g, max_length, &g->entries[i], string);
949
950           list = list->next;
951         }
952
953       g_string_append (string, "\n");
954     }
955
956   if (context->description)
957     {
958       g_string_append (string, TRANSLATE (context, context->description));
959       g_string_append (string, "\n");
960     }
961
962   return g_string_free (string, FALSE);
963 }
964
965 G_GNUC_NORETURN
966 static void
967 print_help (GOptionContext *context,
968             gboolean        main_help,
969             GOptionGroup   *group)
970 {
971   gchar *help;
972
973   help = g_option_context_get_help (context, main_help, group);
974   g_print ("%s", help);
975   g_free (help);
976
977   exit (0);
978 }
979
980 static gboolean
981 parse_int (const gchar *arg_name,
982            const gchar *arg,
983            gint        *result,
984            GError     **error)
985 {
986   gchar *end;
987   glong tmp;
988
989   errno = 0;
990   tmp = strtol (arg, &end, 0);
991
992   if (*arg == '\0' || *end != '\0')
993     {
994       g_set_error (error,
995                    G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
996                    _("Cannot parse integer value '%s' for %s"),
997                    arg, arg_name);
998       return FALSE;
999     }
1000
1001   *result = tmp;
1002   if (*result != tmp || errno == ERANGE)
1003     {
1004       g_set_error (error,
1005                    G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1006                    _("Integer value '%s' for %s out of range"),
1007                    arg, arg_name);
1008       return FALSE;
1009     }
1010
1011   return TRUE;
1012 }
1013
1014
1015 static gboolean
1016 parse_double (const gchar *arg_name,
1017            const gchar *arg,
1018            gdouble        *result,
1019            GError     **error)
1020 {
1021   gchar *end;
1022   gdouble tmp;
1023
1024   errno = 0;
1025   tmp = g_strtod (arg, &end);
1026
1027   if (*arg == '\0' || *end != '\0')
1028     {
1029       g_set_error (error,
1030                    G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1031                    _("Cannot parse double value '%s' for %s"),
1032                    arg, arg_name);
1033       return FALSE;
1034     }
1035   if (errno == ERANGE)
1036     {
1037       g_set_error (error,
1038                    G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1039                    _("Double value '%s' for %s out of range"),
1040                    arg, arg_name);
1041       return FALSE;
1042     }
1043
1044   *result = tmp;
1045
1046   return TRUE;
1047 }
1048
1049
1050 static gboolean
1051 parse_int64 (const gchar *arg_name,
1052              const gchar *arg,
1053              gint64      *result,
1054              GError     **error)
1055 {
1056   gchar *end;
1057   gint64 tmp;
1058
1059   errno = 0;
1060   tmp = g_ascii_strtoll (arg, &end, 0);
1061
1062   if (*arg == '\0' || *end != '\0')
1063     {
1064       g_set_error (error,
1065                    G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1066                    _("Cannot parse integer value '%s' for %s"),
1067                    arg, arg_name);
1068       return FALSE;
1069     }
1070   if (errno == ERANGE)
1071     {
1072       g_set_error (error,
1073                    G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1074                    _("Integer value '%s' for %s out of range"),
1075                    arg, arg_name);
1076       return FALSE;
1077     }
1078
1079   *result = tmp;
1080
1081   return TRUE;
1082 }
1083
1084
1085 static Change *
1086 get_change (GOptionContext *context,
1087             GOptionArg      arg_type,
1088             gpointer        arg_data)
1089 {
1090   GList *list;
1091   Change *change = NULL;
1092
1093   for (list = context->changes; list != NULL; list = list->next)
1094     {
1095       change = list->data;
1096
1097       if (change->arg_data == arg_data)
1098         goto found;
1099     }
1100
1101   change = g_new0 (Change, 1);
1102   change->arg_type = arg_type;
1103   change->arg_data = arg_data;
1104
1105   context->changes = g_list_prepend (context->changes, change);
1106
1107  found:
1108
1109   return change;
1110 }
1111
1112 static void
1113 add_pending_null (GOptionContext *context,
1114                   gchar         **ptr,
1115                   gchar          *value)
1116 {
1117   PendingNull *n;
1118
1119   n = g_new0 (PendingNull, 1);
1120   n->ptr = ptr;
1121   n->value = value;
1122
1123   context->pending_nulls = g_list_prepend (context->pending_nulls, n);
1124 }
1125
1126 static gboolean
1127 parse_arg (GOptionContext *context,
1128            GOptionGroup   *group,
1129            GOptionEntry   *entry,
1130            const gchar    *value,
1131            const gchar    *option_name,
1132            GError        **error)
1133
1134 {
1135   Change *change;
1136
1137   g_assert (value || OPTIONAL_ARG (entry) || NO_ARG (entry));
1138
1139   switch (entry->arg)
1140     {
1141     case G_OPTION_ARG_NONE:
1142       {
1143         change = get_change (context, G_OPTION_ARG_NONE,
1144                              entry->arg_data);
1145
1146         *(gboolean *)entry->arg_data = !(entry->flags & G_OPTION_FLAG_REVERSE);
1147         break;
1148       }
1149     case G_OPTION_ARG_STRING:
1150       {
1151         gchar *data;
1152
1153         data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1154
1155         if (!data)
1156           return FALSE;
1157
1158         change = get_change (context, G_OPTION_ARG_STRING,
1159                              entry->arg_data);
1160         g_free (change->allocated.str);
1161
1162         change->prev.str = *(gchar **)entry->arg_data;
1163         change->allocated.str = data;
1164
1165         *(gchar **)entry->arg_data = data;
1166         break;
1167       }
1168     case G_OPTION_ARG_STRING_ARRAY:
1169       {
1170         gchar *data;
1171
1172         data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1173
1174         if (!data)
1175           return FALSE;
1176
1177         change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
1178                              entry->arg_data);
1179
1180         if (change->allocated.array.len == 0)
1181           {
1182             change->prev.array = *(gchar ***)entry->arg_data;
1183             change->allocated.array.data = g_new (gchar *, 2);
1184           }
1185         else
1186           change->allocated.array.data =
1187             g_renew (gchar *, change->allocated.array.data,
1188                      change->allocated.array.len + 2);
1189
1190         change->allocated.array.data[change->allocated.array.len] = data;
1191         change->allocated.array.data[change->allocated.array.len + 1] = NULL;
1192
1193         change->allocated.array.len ++;
1194
1195         *(gchar ***)entry->arg_data = change->allocated.array.data;
1196
1197         break;
1198       }
1199
1200     case G_OPTION_ARG_FILENAME:
1201       {
1202         gchar *data;
1203
1204 #ifdef G_OS_WIN32
1205         data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1206
1207         if (!data)
1208           return FALSE;
1209 #else
1210         data = g_strdup (value);
1211 #endif
1212         change = get_change (context, G_OPTION_ARG_FILENAME,
1213                              entry->arg_data);
1214         g_free (change->allocated.str);
1215
1216         change->prev.str = *(gchar **)entry->arg_data;
1217         change->allocated.str = data;
1218
1219         *(gchar **)entry->arg_data = data;
1220         break;
1221       }
1222
1223     case G_OPTION_ARG_FILENAME_ARRAY:
1224       {
1225         gchar *data;
1226
1227 #ifdef G_OS_WIN32
1228         data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1229
1230         if (!data)
1231           return FALSE;
1232 #else
1233         data = g_strdup (value);
1234 #endif
1235         change = get_change (context, G_OPTION_ARG_STRING_ARRAY,
1236                              entry->arg_data);
1237
1238         if (change->allocated.array.len == 0)
1239           {
1240             change->prev.array = *(gchar ***)entry->arg_data;
1241             change->allocated.array.data = g_new (gchar *, 2);
1242           }
1243         else
1244           change->allocated.array.data =
1245             g_renew (gchar *, change->allocated.array.data,
1246                      change->allocated.array.len + 2);
1247
1248         change->allocated.array.data[change->allocated.array.len] = data;
1249         change->allocated.array.data[change->allocated.array.len + 1] = NULL;
1250
1251         change->allocated.array.len ++;
1252
1253         *(gchar ***)entry->arg_data = change->allocated.array.data;
1254
1255         break;
1256       }
1257
1258     case G_OPTION_ARG_INT:
1259       {
1260         gint data;
1261
1262         if (!parse_int (option_name, value,
1263                         &data,
1264                         error))
1265           return FALSE;
1266
1267         change = get_change (context, G_OPTION_ARG_INT,
1268                              entry->arg_data);
1269         change->prev.integer = *(gint *)entry->arg_data;
1270         *(gint *)entry->arg_data = data;
1271         break;
1272       }
1273     case G_OPTION_ARG_CALLBACK:
1274       {
1275         gchar *data;
1276         gboolean retval;
1277
1278         if (!value && entry->flags & G_OPTION_FLAG_OPTIONAL_ARG)
1279           data = NULL;
1280         else if (entry->flags & G_OPTION_FLAG_NO_ARG)
1281           data = NULL;
1282         else if (entry->flags & G_OPTION_FLAG_FILENAME)
1283           {
1284 #ifdef G_OS_WIN32
1285             data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1286 #else
1287             data = g_strdup (value);
1288 #endif
1289           }
1290         else
1291           data = g_locale_to_utf8 (value, -1, NULL, NULL, error);
1292
1293         if (!(entry->flags & (G_OPTION_FLAG_NO_ARG|G_OPTION_FLAG_OPTIONAL_ARG)) &&
1294             !data)
1295           return FALSE;
1296
1297         retval = (* (GOptionArgFunc) entry->arg_data) (option_name, data, group->user_data, error);
1298
1299         if (!retval && error != NULL && *error == NULL)
1300           g_set_error (error,
1301                        G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
1302                        _("Error parsing option %s"), option_name);
1303
1304         g_free (data);
1305
1306         return retval;
1307
1308         break;
1309       }
1310     case G_OPTION_ARG_DOUBLE:
1311       {
1312         gdouble data;
1313
1314         if (!parse_double (option_name, value,
1315                         &data,
1316                         error))
1317           {
1318             return FALSE;
1319           }
1320
1321         change = get_change (context, G_OPTION_ARG_DOUBLE,
1322                              entry->arg_data);
1323         change->prev.dbl = *(gdouble *)entry->arg_data;
1324         *(gdouble *)entry->arg_data = data;
1325         break;
1326       }
1327     case G_OPTION_ARG_INT64:
1328       {
1329         gint64 data;
1330
1331         if (!parse_int64 (option_name, value,
1332                          &data,
1333                          error))
1334           {
1335             return FALSE;
1336           }
1337
1338         change = get_change (context, G_OPTION_ARG_INT64,
1339                              entry->arg_data);
1340         change->prev.int64 = *(gint64 *)entry->arg_data;
1341         *(gint64 *)entry->arg_data = data;
1342         break;
1343       }
1344     default:
1345       g_assert_not_reached ();
1346     }
1347
1348   return TRUE;
1349 }
1350
1351 static gboolean
1352 parse_short_option (GOptionContext *context,
1353                     GOptionGroup   *group,
1354                     gint            idx,
1355                     gint           *new_idx,
1356                     gchar           arg,
1357                     gint           *argc,
1358                     gchar        ***argv,
1359                     GError        **error,
1360                     gboolean       *parsed)
1361 {
1362   gint j;
1363
1364   for (j = 0; j < group->n_entries; j++)
1365     {
1366       if (arg == group->entries[j].short_name)
1367         {
1368           gchar *option_name;
1369           gchar *value = NULL;
1370
1371           option_name = g_strdup_printf ("-%c", group->entries[j].short_name);
1372
1373           if (NO_ARG (&group->entries[j]))
1374             value = NULL;
1375           else
1376             {
1377               if (*new_idx > idx)
1378                 {
1379                   g_set_error (error,
1380                                G_OPTION_ERROR, G_OPTION_ERROR_FAILED,
1381                                _("Error parsing option %s"), option_name);
1382                   g_free (option_name);
1383                   return FALSE;
1384                 }
1385
1386               if (idx < *argc - 1)
1387                 {
1388                   if (!OPTIONAL_ARG (&group->entries[j]))
1389                     {
1390                       value = (*argv)[idx + 1];
1391                       add_pending_null (context, &((*argv)[idx + 1]), NULL);
1392                       *new_idx = idx + 1;
1393                     }
1394                   else
1395                     {
1396                       if ((*argv)[idx + 1][0] == '-')
1397                         value = NULL;
1398                       else
1399                         {
1400                           value = (*argv)[idx + 1];
1401                           add_pending_null (context, &((*argv)[idx + 1]), NULL);
1402                           *new_idx = idx + 1;
1403                         }
1404                     }
1405                 }
1406               else if (idx >= *argc - 1 && OPTIONAL_ARG (&group->entries[j]))
1407                 value = NULL;
1408               else
1409                 {
1410                   g_set_error (error,
1411                                G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1412                                _("Missing argument for %s"), option_name);
1413                   g_free (option_name);
1414                   return FALSE;
1415                 }
1416             }
1417
1418           if (!parse_arg (context, group, &group->entries[j],
1419                           value, option_name, error))
1420             {
1421               g_free (option_name);
1422               return FALSE;
1423             }
1424
1425           g_free (option_name);
1426           *parsed = TRUE;
1427         }
1428     }
1429
1430   return TRUE;
1431 }
1432
1433 static gboolean
1434 parse_long_option (GOptionContext *context,
1435                    GOptionGroup   *group,
1436                    gint           *idx,
1437                    gchar          *arg,
1438                    gboolean        aliased,
1439                    gint           *argc,
1440                    gchar        ***argv,
1441                    GError        **error,
1442                    gboolean       *parsed)
1443 {
1444   gint j;
1445
1446   for (j = 0; j < group->n_entries; j++)
1447     {
1448       if (*idx >= *argc)
1449         return TRUE;
1450
1451       if (aliased && (group->entries[j].flags & G_OPTION_FLAG_NOALIAS))
1452         continue;
1453
1454       if (NO_ARG (&group->entries[j]) &&
1455           strcmp (arg, group->entries[j].long_name) == 0)
1456         {
1457           gchar *option_name;
1458           gboolean retval;
1459
1460           option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1461           retval = parse_arg (context, group, &group->entries[j],
1462                               NULL, option_name, error);
1463           g_free (option_name);
1464
1465           add_pending_null (context, &((*argv)[*idx]), NULL);
1466           *parsed = TRUE;
1467
1468           return retval;
1469         }
1470       else
1471         {
1472           gint len = strlen (group->entries[j].long_name);
1473
1474           if (strncmp (arg, group->entries[j].long_name, len) == 0 &&
1475               (arg[len] == '=' || arg[len] == 0))
1476             {
1477               gchar *value = NULL;
1478               gchar *option_name;
1479
1480               add_pending_null (context, &((*argv)[*idx]), NULL);
1481               option_name = g_strconcat ("--", group->entries[j].long_name, NULL);
1482
1483               if (arg[len] == '=')
1484                 value = arg + len + 1;
1485               else if (*idx < *argc - 1)
1486                 {
1487                   if (!OPTIONAL_ARG (&group->entries[j]))
1488                     {
1489                       value = (*argv)[*idx + 1];
1490                       add_pending_null (context, &((*argv)[*idx + 1]), NULL);
1491                       (*idx)++;
1492                     }
1493                   else
1494                     {
1495                       if ((*argv)[*idx + 1][0] == '-')
1496                         {
1497                           gboolean retval;
1498                           retval = parse_arg (context, group, &group->entries[j],
1499                                               NULL, option_name, error);
1500                           *parsed = TRUE;
1501                           g_free (option_name);
1502                           return retval;
1503                         }
1504                       else
1505                         {
1506                           value = (*argv)[*idx + 1];
1507                           add_pending_null (context, &((*argv)[*idx + 1]), NULL);
1508                           (*idx)++;
1509                         }
1510                     }
1511                 }
1512               else if (*idx >= *argc - 1 && OPTIONAL_ARG (&group->entries[j]))
1513                 {
1514                     gboolean retval;
1515                     retval = parse_arg (context, group, &group->entries[j],
1516                                         NULL, option_name, error);
1517                     *parsed = TRUE;
1518                     g_free (option_name);
1519                     return retval;
1520                 }
1521               else
1522                 {
1523                   g_set_error (error,
1524                                G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
1525                                _("Missing argument for %s"), option_name);
1526                   g_free (option_name);
1527                   return FALSE;
1528                 }
1529
1530               if (!parse_arg (context, group, &group->entries[j],
1531                               value, option_name, error))
1532                 {
1533                   g_free (option_name);
1534                   return FALSE;
1535                 }
1536
1537               g_free (option_name);
1538               *parsed = TRUE;
1539             }
1540         }
1541     }
1542
1543   return TRUE;
1544 }
1545
1546 static gboolean
1547 parse_remaining_arg (GOptionContext *context,
1548                      GOptionGroup   *group,
1549                      gint           *idx,
1550                      gint           *argc,
1551                      gchar        ***argv,
1552                      GError        **error,
1553                      gboolean       *parsed)
1554 {
1555   gint j;
1556
1557   for (j = 0; j < group->n_entries; j++)
1558     {
1559       if (*idx >= *argc)
1560         return TRUE;
1561
1562       if (group->entries[j].long_name[0])
1563         continue;
1564
1565       g_return_val_if_fail (group->entries[j].arg == G_OPTION_ARG_CALLBACK ||
1566                             group->entries[j].arg == G_OPTION_ARG_STRING_ARRAY ||
1567                             group->entries[j].arg == G_OPTION_ARG_FILENAME_ARRAY, FALSE);
1568
1569       add_pending_null (context, &((*argv)[*idx]), NULL);
1570
1571       if (!parse_arg (context, group, &group->entries[j], (*argv)[*idx], "", error))
1572         return FALSE;
1573
1574       *parsed = TRUE;
1575       return TRUE;
1576     }
1577
1578   return TRUE;
1579 }
1580
1581 static void
1582 free_changes_list (GOptionContext *context,
1583                    gboolean        revert)
1584 {
1585   GList *list;
1586
1587   for (list = context->changes; list != NULL; list = list->next)
1588     {
1589       Change *change = list->data;
1590
1591       if (revert)
1592         {
1593           switch (change->arg_type)
1594             {
1595             case G_OPTION_ARG_NONE:
1596               *(gboolean *)change->arg_data = change->prev.bool;
1597               break;
1598             case G_OPTION_ARG_INT:
1599               *(gint *)change->arg_data = change->prev.integer;
1600               break;
1601             case G_OPTION_ARG_STRING:
1602             case G_OPTION_ARG_FILENAME:
1603               g_free (change->allocated.str);
1604               *(gchar **)change->arg_data = change->prev.str;
1605               break;
1606             case G_OPTION_ARG_STRING_ARRAY:
1607             case G_OPTION_ARG_FILENAME_ARRAY:
1608               g_strfreev (change->allocated.array.data);
1609               *(gchar ***)change->arg_data = change->prev.array;
1610               break;
1611             case G_OPTION_ARG_DOUBLE:
1612               *(gdouble *)change->arg_data = change->prev.dbl;
1613               break;
1614             case G_OPTION_ARG_INT64:
1615               *(gint64 *)change->arg_data = change->prev.int64;
1616               break;
1617             default:
1618               g_assert_not_reached ();
1619             }
1620         }
1621
1622       g_free (change);
1623     }
1624
1625   g_list_free (context->changes);
1626   context->changes = NULL;
1627 }
1628
1629 static void
1630 free_pending_nulls (GOptionContext *context,
1631                     gboolean        perform_nulls)
1632 {
1633   GList *list;
1634
1635   for (list = context->pending_nulls; list != NULL; list = list->next)
1636     {
1637       PendingNull *n = list->data;
1638
1639       if (perform_nulls)
1640         {
1641           if (n->value)
1642             {
1643               /* Copy back the short options */
1644               *(n->ptr)[0] = '-';
1645               strcpy (*n->ptr + 1, n->value);
1646             }
1647           else
1648             *n->ptr = NULL;
1649         }
1650
1651       g_free (n->value);
1652       g_free (n);
1653     }
1654
1655   g_list_free (context->pending_nulls);
1656   context->pending_nulls = NULL;
1657 }
1658
1659 /* Use a platform-specific mechanism to look up the first argument to
1660  * the current process. 
1661  * Note if you implement this for other platforms, also add it to
1662  * tests/option-argv0.c
1663  */
1664 static char *
1665 platform_get_argv0 (void)
1666 {
1667 #ifdef __linux
1668   char *cmdline;
1669   char *base_arg0;
1670   gsize len;
1671
1672   if (!g_file_get_contents ("/proc/self/cmdline",
1673                             &cmdline,
1674                             &len,
1675                             NULL))
1676     return NULL;
1677   /* Sanity check for a NUL terminator. */
1678   if (!memchr (cmdline, 0, len))
1679     return NULL;
1680   /* We could just return cmdline, but I think it's better
1681    * to hold on to a smaller malloc block; the arguments
1682    * could be large.
1683    */
1684   base_arg0 = g_path_get_basename (cmdline);
1685   g_free (cmdline);
1686   return base_arg0;
1687 #endif
1688
1689   return NULL;
1690 }
1691
1692 /**
1693  * g_option_context_parse:
1694  * @context: a #GOptionContext
1695  * @argc: (inout) (allow-none): a pointer to the number of command line arguments
1696  * @argv: (inout) (array length=argc) (allow-none): a pointer to the array of command line arguments
1697  * @error: a return location for errors
1698  *
1699  * Parses the command line arguments, recognizing options
1700  * which have been added to @context. A side-effect of
1701  * calling this function is that g_set_prgname() will be
1702  * called.
1703  *
1704  * If the parsing is successful, any parsed arguments are
1705  * removed from the array and @argc and @argv are updated
1706  * accordingly. A '--' option is stripped from @argv
1707  * unless there are unparsed options before and after it,
1708  * or some of the options after it start with '-'. In case
1709  * of an error, @argc and @argv are left unmodified.
1710  *
1711  * If automatic <option>--help</option> support is enabled
1712  * (see g_option_context_set_help_enabled()), and the
1713  * @argv array contains one of the recognized help options,
1714  * this function will produce help output to stdout and
1715  * call <literal>exit (0)</literal>.
1716  *
1717  * Note that function depends on the
1718  * <link linkend="setlocale">current locale</link> for
1719  * automatic character set conversion of string and filename
1720  * arguments.
1721  *
1722  * Return value: %TRUE if the parsing was successful,
1723  *               %FALSE if an error occurred
1724  *
1725  * Since: 2.6
1726  **/
1727 gboolean
1728 g_option_context_parse (GOptionContext   *context,
1729                         gint             *argc,
1730                         gchar          ***argv,
1731                         GError          **error)
1732 {
1733   gint i, j, k;
1734   GList *list;
1735
1736   /* Set program name */
1737   if (!g_get_prgname())
1738     {
1739       gchar *prgname;
1740
1741       if (argc && argv && *argc)
1742         prgname = g_path_get_basename ((*argv)[0]);
1743       else
1744         prgname = platform_get_argv0 ();
1745
1746       if (prgname)
1747         g_set_prgname (prgname);
1748       else
1749         g_set_prgname ("<unknown>");
1750
1751       g_free (prgname);
1752     }
1753
1754   /* Call pre-parse hooks */
1755   list = context->groups;
1756   while (list)
1757     {
1758       GOptionGroup *group = list->data;
1759
1760       if (group->pre_parse_func)
1761         {
1762           if (!(* group->pre_parse_func) (context, group,
1763                                           group->user_data, error))
1764             goto fail;
1765         }
1766
1767       list = list->next;
1768     }
1769
1770   if (context->main_group && context->main_group->pre_parse_func)
1771     {
1772       if (!(* context->main_group->pre_parse_func) (context, context->main_group,
1773                                                     context->main_group->user_data, error))
1774         goto fail;
1775     }
1776
1777   if (argc && argv)
1778     {
1779       gboolean stop_parsing = FALSE;
1780       gboolean has_unknown = FALSE;
1781       gint separator_pos = 0;
1782
1783       for (i = 1; i < *argc; i++)
1784         {
1785           gchar *arg, *dash;
1786           gboolean parsed = FALSE;
1787
1788           if ((*argv)[i][0] == '-' && (*argv)[i][1] != '\0' && !stop_parsing)
1789             {
1790               if ((*argv)[i][1] == '-')
1791                 {
1792                   /* -- option */
1793
1794                   arg = (*argv)[i] + 2;
1795
1796                   /* '--' terminates list of arguments */
1797                   if (*arg == 0)
1798                     {
1799                       separator_pos = i;
1800                       stop_parsing = TRUE;
1801                       continue;
1802                     }
1803
1804                   /* Handle help options */
1805                   if (context->help_enabled)
1806                     {
1807                       if (strcmp (arg, "help") == 0)
1808                         print_help (context, TRUE, NULL);
1809                       else if (strcmp (arg, "help-all") == 0)
1810                         print_help (context, FALSE, NULL);
1811                       else if (strncmp (arg, "help-", 5) == 0)
1812                         {
1813                           list = context->groups;
1814
1815                           while (list)
1816                             {
1817                               GOptionGroup *group = list->data;
1818
1819                               if (strcmp (arg + 5, group->name) == 0)
1820                                 print_help (context, FALSE, group);
1821
1822                               list = list->next;
1823                             }
1824                         }
1825                     }
1826
1827                   if (context->main_group &&
1828                       !parse_long_option (context, context->main_group, &i, arg,
1829                                           FALSE, argc, argv, error, &parsed))
1830                     goto fail;
1831
1832                   if (parsed)
1833                     continue;
1834
1835                   /* Try the groups */
1836                   list = context->groups;
1837                   while (list)
1838                     {
1839                       GOptionGroup *group = list->data;
1840
1841                       if (!parse_long_option (context, group, &i, arg,
1842                                               FALSE, argc, argv, error, &parsed))
1843                         goto fail;
1844
1845                       if (parsed)
1846                         break;
1847
1848                       list = list->next;
1849                     }
1850
1851                   if (parsed)
1852                     continue;
1853
1854                   /* Now look for --<group>-<option> */
1855                   dash = strchr (arg, '-');
1856                   if (dash)
1857                     {
1858                       /* Try the groups */
1859                       list = context->groups;
1860                       while (list)
1861                         {
1862                           GOptionGroup *group = list->data;
1863
1864                           if (strncmp (group->name, arg, dash - arg) == 0)
1865                             {
1866                               if (!parse_long_option (context, group, &i, dash + 1,
1867                                                       TRUE, argc, argv, error, &parsed))
1868                                 goto fail;
1869
1870                               if (parsed)
1871                                 break;
1872                             }
1873
1874                           list = list->next;
1875                         }
1876                     }
1877
1878                   if (context->ignore_unknown)
1879                     continue;
1880                 }
1881               else
1882                 { /* short option */
1883                   gint new_i = i, arg_length;
1884                   gboolean *nulled_out = NULL;
1885                   gboolean has_h_entry = context_has_h_entry (context);
1886                   arg = (*argv)[i] + 1;
1887                   arg_length = strlen (arg);
1888                   nulled_out = g_newa (gboolean, arg_length);
1889                   memset (nulled_out, 0, arg_length * sizeof (gboolean));
1890                   for (j = 0; j < arg_length; j++)
1891                     {
1892                       if (context->help_enabled && (arg[j] == '?' ||
1893                         (arg[j] == 'h' && !has_h_entry)))
1894                         print_help (context, TRUE, NULL);
1895                       parsed = FALSE;
1896                       if (context->main_group &&
1897                           !parse_short_option (context, context->main_group,
1898                                                i, &new_i, arg[j],
1899                                                argc, argv, error, &parsed))
1900                         goto fail;
1901                       if (!parsed)
1902                         {
1903                           /* Try the groups */
1904                           list = context->groups;
1905                           while (list)
1906                             {
1907                               GOptionGroup *group = list->data;
1908                               if (!parse_short_option (context, group, i, &new_i, arg[j],
1909                                                        argc, argv, error, &parsed))
1910                                 goto fail;
1911                               if (parsed)
1912                                 break;
1913                               list = list->next;
1914                             }
1915                         }
1916
1917                       if (context->ignore_unknown && parsed)
1918                         nulled_out[j] = TRUE;
1919                       else if (context->ignore_unknown)
1920                         continue;
1921                       else if (!parsed)
1922                         break;
1923                       /* !context->ignore_unknown && parsed */
1924                     }
1925                   if (context->ignore_unknown)
1926                     {
1927                       gchar *new_arg = NULL;
1928                       gint arg_index = 0;
1929                       for (j = 0; j < arg_length; j++)
1930                         {
1931                           if (!nulled_out[j])
1932                             {
1933                               if (!new_arg)
1934                                 new_arg = g_malloc (arg_length + 1);
1935                               new_arg[arg_index++] = arg[j];
1936                             }
1937                         }
1938                       if (new_arg)
1939                         new_arg[arg_index] = '\0';
1940                       add_pending_null (context, &((*argv)[i]), new_arg);
1941                     }
1942                   else if (parsed)
1943                     {
1944                       add_pending_null (context, &((*argv)[i]), NULL);
1945                       i = new_i;
1946                     }
1947                 }
1948
1949               if (!parsed)
1950                 has_unknown = TRUE;
1951
1952               if (!parsed && !context->ignore_unknown)
1953                 {
1954                   g_set_error (error,
1955                                G_OPTION_ERROR, G_OPTION_ERROR_UNKNOWN_OPTION,
1956                                    _("Unknown option %s"), (*argv)[i]);
1957                   goto fail;
1958                 }
1959             }
1960           else
1961             {
1962               /* Collect remaining args */
1963               if (context->main_group &&
1964                   !parse_remaining_arg (context, context->main_group, &i,
1965                                         argc, argv, error, &parsed))
1966                 goto fail;
1967
1968               if (!parsed && (has_unknown || (*argv)[i][0] == '-'))
1969                 separator_pos = 0;
1970             }
1971         }
1972
1973       if (separator_pos > 0)
1974         add_pending_null (context, &((*argv)[separator_pos]), NULL);
1975
1976     }
1977
1978   /* Call post-parse hooks */
1979   list = context->groups;
1980   while (list)
1981     {
1982       GOptionGroup *group = list->data;
1983
1984       if (group->post_parse_func)
1985         {
1986           if (!(* group->post_parse_func) (context, group,
1987                                            group->user_data, error))
1988             goto fail;
1989         }
1990
1991       list = list->next;
1992     }
1993
1994   if (context->main_group && context->main_group->post_parse_func)
1995     {
1996       if (!(* context->main_group->post_parse_func) (context, context->main_group,
1997                                                      context->main_group->user_data, error))
1998         goto fail;
1999     }
2000
2001   if (argc && argv)
2002     {
2003       free_pending_nulls (context, TRUE);
2004
2005       for (i = 1; i < *argc; i++)
2006         {
2007           for (k = i; k < *argc; k++)
2008             if ((*argv)[k] != NULL)
2009               break;
2010
2011           if (k > i)
2012             {
2013               k -= i;
2014               for (j = i + k; j < *argc; j++)
2015                 {
2016                   (*argv)[j-k] = (*argv)[j];
2017                   (*argv)[j] = NULL;
2018                 }
2019               *argc -= k;
2020             }
2021         }
2022     }
2023
2024   return TRUE;
2025
2026  fail:
2027
2028   /* Call error hooks */
2029   list = context->groups;
2030   while (list)
2031     {
2032       GOptionGroup *group = list->data;
2033
2034       if (group->error_func)
2035         (* group->error_func) (context, group,
2036                                group->user_data, error);
2037
2038       list = list->next;
2039     }
2040
2041   if (context->main_group && context->main_group->error_func)
2042     (* context->main_group->error_func) (context, context->main_group,
2043                                          context->main_group->user_data, error);
2044
2045   free_changes_list (context, TRUE);
2046   free_pending_nulls (context, FALSE);
2047
2048   return FALSE;
2049 }
2050
2051 /**
2052  * g_option_group_new:
2053  * @name: the name for the option group, this is used to provide
2054  *   help for the options in this group with <option>--help-</option>@name
2055  * @description: a description for this group to be shown in
2056  *   <option>--help</option>. This string is translated using the translation
2057  *   domain or translation function of the group
2058  * @help_description: a description for the <option>--help-</option>@name option.
2059  *   This string is translated using the translation domain or translation function
2060  *   of the group
2061  * @user_data: user data that will be passed to the pre- and post-parse hooks,
2062  *   the error hook and to callbacks of %G_OPTION_ARG_CALLBACK options, or %NULL
2063  * @destroy: a function that will be called to free @user_data, or %NULL
2064  *
2065  * Creates a new #GOptionGroup.
2066  *
2067  * Return value: a newly created option group. It should be added
2068  *   to a #GOptionContext or freed with g_option_group_free().
2069  *
2070  * Since: 2.6
2071  **/
2072 GOptionGroup *
2073 g_option_group_new (const gchar    *name,
2074                     const gchar    *description,
2075                     const gchar    *help_description,
2076                     gpointer        user_data,
2077                     GDestroyNotify  destroy)
2078
2079 {
2080   GOptionGroup *group;
2081
2082   group = g_new0 (GOptionGroup, 1);
2083   group->name = g_strdup (name);
2084   group->description = g_strdup (description);
2085   group->help_description = g_strdup (help_description);
2086   group->user_data = user_data;
2087   group->destroy_notify = destroy;
2088
2089   return group;
2090 }
2091
2092
2093 /**
2094  * g_option_group_free:
2095  * @group: a #GOptionGroup
2096  *
2097  * Frees a #GOptionGroup. Note that you must <emphasis>not</emphasis>
2098  * free groups which have been added to a #GOptionContext.
2099  *
2100  * Since: 2.6
2101  **/
2102 void
2103 g_option_group_free (GOptionGroup *group)
2104 {
2105   g_return_if_fail (group != NULL);
2106
2107   g_free (group->name);
2108   g_free (group->description);
2109   g_free (group->help_description);
2110
2111   g_free (group->entries);
2112
2113   if (group->destroy_notify)
2114     (* group->destroy_notify) (group->user_data);
2115
2116   if (group->translate_notify)
2117     (* group->translate_notify) (group->translate_data);
2118
2119   g_free (group);
2120 }
2121
2122
2123 /**
2124  * g_option_group_add_entries:
2125  * @group: a #GOptionGroup
2126  * @entries: a %NULL-terminated array of #GOptionEntry<!-- -->s
2127  *
2128  * Adds the options specified in @entries to @group.
2129  *
2130  * Since: 2.6
2131  **/
2132 void
2133 g_option_group_add_entries (GOptionGroup       *group,
2134                             const GOptionEntry *entries)
2135 {
2136   gint i, n_entries;
2137
2138   g_return_if_fail (entries != NULL);
2139
2140   for (n_entries = 0; entries[n_entries].long_name != NULL; n_entries++) ;
2141
2142   group->entries = g_renew (GOptionEntry, group->entries, group->n_entries + n_entries);
2143
2144   memcpy (group->entries + group->n_entries, entries, sizeof (GOptionEntry) * n_entries);
2145
2146   for (i = group->n_entries; i < group->n_entries + n_entries; i++)
2147     {
2148       gchar c = group->entries[i].short_name;
2149
2150       if (c == '-' || (c != 0 && !g_ascii_isprint (c)))
2151         {
2152           g_warning (G_STRLOC ": ignoring invalid short option '%c' (%d)", c, c);
2153           group->entries[i].short_name = 0;
2154         }
2155
2156       if (group->entries[i].arg != G_OPTION_ARG_NONE &&
2157           (group->entries[i].flags & G_OPTION_FLAG_REVERSE) != 0)
2158         {
2159           g_warning (G_STRLOC ": ignoring reverse flag on option of type %d", group->entries[i].arg);
2160
2161           group->entries[i].flags &= ~G_OPTION_FLAG_REVERSE;
2162         }
2163
2164       if (group->entries[i].arg != G_OPTION_ARG_CALLBACK &&
2165           (group->entries[i].flags & (G_OPTION_FLAG_NO_ARG|G_OPTION_FLAG_OPTIONAL_ARG|G_OPTION_FLAG_FILENAME)) != 0)
2166         {
2167           g_warning (G_STRLOC ": ignoring no-arg, optional-arg or filename flags (%d) on option of type %d", group->entries[i].flags, group->entries[i].arg);
2168
2169           group->entries[i].flags &= ~(G_OPTION_FLAG_NO_ARG|G_OPTION_FLAG_OPTIONAL_ARG|G_OPTION_FLAG_FILENAME);
2170         }
2171     }
2172
2173   group->n_entries += n_entries;
2174 }
2175
2176 /**
2177  * g_option_group_set_parse_hooks:
2178  * @group: a #GOptionGroup
2179  * @pre_parse_func: a function to call before parsing, or %NULL
2180  * @post_parse_func: a function to call after parsing, or %NULL
2181  *
2182  * Associates two functions with @group which will be called
2183  * from g_option_context_parse() before the first option is parsed
2184  * and after the last option has been parsed, respectively.
2185  *
2186  * Note that the user data to be passed to @pre_parse_func and
2187  * @post_parse_func can be specified when constructing the group
2188  * with g_option_group_new().
2189  *
2190  * Since: 2.6
2191  **/
2192 void
2193 g_option_group_set_parse_hooks (GOptionGroup     *group,
2194                                 GOptionParseFunc  pre_parse_func,
2195                                 GOptionParseFunc  post_parse_func)
2196 {
2197   g_return_if_fail (group != NULL);
2198
2199   group->pre_parse_func = pre_parse_func;
2200   group->post_parse_func = post_parse_func;
2201 }
2202
2203 /**
2204  * g_option_group_set_error_hook:
2205  * @group: a #GOptionGroup
2206  * @error_func: a function to call when an error occurs
2207  *
2208  * Associates a function with @group which will be called
2209  * from g_option_context_parse() when an error occurs.
2210  *
2211  * Note that the user data to be passed to @error_func can be
2212  * specified when constructing the group with g_option_group_new().
2213  *
2214  * Since: 2.6
2215  **/
2216 void
2217 g_option_group_set_error_hook (GOptionGroup     *group,
2218                                GOptionErrorFunc  error_func)
2219 {
2220   g_return_if_fail (group != NULL);
2221
2222   group->error_func = error_func;
2223 }
2224
2225
2226 /**
2227  * g_option_group_set_translate_func:
2228  * @group: a #GOptionGroup
2229  * @func: the #GTranslateFunc, or %NULL
2230  * @data: user data to pass to @func, or %NULL
2231  * @destroy_notify: a function which gets called to free @data, or %NULL
2232  *
2233  * Sets the function which is used to translate user-visible
2234  * strings, for <option>--help</option> output. Different
2235  * groups can use different #GTranslateFunc<!-- -->s. If @func
2236  * is %NULL, strings are not translated.
2237  *
2238  * If you are using gettext(), you only need to set the translation
2239  * domain, see g_option_group_set_translation_domain().
2240  *
2241  * Since: 2.6
2242  **/
2243 void
2244 g_option_group_set_translate_func (GOptionGroup   *group,
2245                                    GTranslateFunc  func,
2246                                    gpointer        data,
2247                                    GDestroyNotify  destroy_notify)
2248 {
2249   g_return_if_fail (group != NULL);
2250
2251   if (group->translate_notify)
2252     group->translate_notify (group->translate_data);
2253
2254   group->translate_func = func;
2255   group->translate_data = data;
2256   group->translate_notify = destroy_notify;
2257 }
2258
2259 static const gchar *
2260 dgettext_swapped (const gchar *msgid,
2261                   const gchar *domainname)
2262 {
2263   return g_dgettext (domainname, msgid);
2264 }
2265
2266 /**
2267  * g_option_group_set_translation_domain:
2268  * @group: a #GOptionGroup
2269  * @domain: the domain to use
2270  *
2271  * A convenience function to use gettext() for translating
2272  * user-visible strings.
2273  *
2274  * Since: 2.6
2275  **/
2276 void
2277 g_option_group_set_translation_domain (GOptionGroup *group,
2278                                        const gchar  *domain)
2279 {
2280   g_return_if_fail (group != NULL);
2281
2282   g_option_group_set_translate_func (group,
2283                                      (GTranslateFunc)dgettext_swapped,
2284                                      g_strdup (domain),
2285                                      g_free);
2286 }
2287
2288 /**
2289  * g_option_context_set_translate_func:
2290  * @context: a #GOptionContext
2291  * @func: the #GTranslateFunc, or %NULL
2292  * @data: user data to pass to @func, or %NULL
2293  * @destroy_notify: a function which gets called to free @data, or %NULL
2294  *
2295  * Sets the function which is used to translate the contexts
2296  * user-visible strings, for <option>--help</option> output.
2297  * If @func is %NULL, strings are not translated.
2298  *
2299  * Note that option groups have their own translation functions,
2300  * this function only affects the @parameter_string (see g_option_context_new()),
2301  * the summary (see g_option_context_set_summary()) and the description
2302  * (see g_option_context_set_description()).
2303  *
2304  * If you are using gettext(), you only need to set the translation
2305  * domain, see g_option_context_set_translation_domain().
2306  *
2307  * Since: 2.12
2308  **/
2309 void
2310 g_option_context_set_translate_func (GOptionContext *context,
2311                                      GTranslateFunc func,
2312                                      gpointer       data,
2313                                      GDestroyNotify destroy_notify)
2314 {
2315   g_return_if_fail (context != NULL);
2316
2317   if (context->translate_notify)
2318     context->translate_notify (context->translate_data);
2319
2320   context->translate_func = func;
2321   context->translate_data = data;
2322   context->translate_notify = destroy_notify;
2323 }
2324
2325 /**
2326  * g_option_context_set_translation_domain:
2327  * @context: a #GOptionContext
2328  * @domain: the domain to use
2329  *
2330  * A convenience function to use gettext() for translating
2331  * user-visible strings.
2332  *
2333  * Since: 2.12
2334  **/
2335 void
2336 g_option_context_set_translation_domain (GOptionContext *context,
2337                                          const gchar     *domain)
2338 {
2339   g_return_if_fail (context != NULL);
2340
2341   g_option_context_set_translate_func (context,
2342                                        (GTranslateFunc)dgettext_swapped,
2343                                        g_strdup (domain),
2344                                        g_free);
2345 }
2346
2347 /**
2348  * g_option_context_set_summary:
2349  * @context: a #GOptionContext
2350  * @summary: a string to be shown in <option>--help</option> output
2351  *  before the list of options, or %NULL
2352  *
2353  * Adds a string to be displayed in <option>--help</option> output
2354  * before the list of options. This is typically a summary of the
2355  * program functionality.
2356  *
2357  * Note that the summary is translated (see
2358  * g_option_context_set_translate_func() and
2359  * g_option_context_set_translation_domain()).
2360  *
2361  * Since: 2.12
2362  */
2363 void
2364 g_option_context_set_summary (GOptionContext *context,
2365                               const gchar    *summary)
2366 {
2367   g_return_if_fail (context != NULL);
2368
2369   g_free (context->summary);
2370   context->summary = g_strdup (summary);
2371 }
2372
2373
2374 /**
2375  * g_option_context_get_summary:
2376  * @context: a #GOptionContext
2377  *
2378  * Returns the summary. See g_option_context_set_summary().
2379  *
2380  * Returns: the summary
2381  *
2382  * Since: 2.12
2383  */
2384 const gchar *
2385 g_option_context_get_summary (GOptionContext *context)
2386 {
2387   g_return_val_if_fail (context != NULL, NULL);
2388
2389   return context->summary;
2390 }
2391
2392 /**
2393  * g_option_context_set_description:
2394  * @context: a #GOptionContext
2395  * @description: a string to be shown in <option>--help</option> output
2396  *   after the list of options, or %NULL
2397  *
2398  * Adds a string to be displayed in <option>--help</option> output
2399  * after the list of options. This text often includes a bug reporting
2400  * address.
2401  *
2402  * Note that the summary is translated (see
2403  * g_option_context_set_translate_func()).
2404  *
2405  * Since: 2.12
2406  */
2407 void
2408 g_option_context_set_description (GOptionContext *context,
2409                                   const gchar    *description)
2410 {
2411   g_return_if_fail (context != NULL);
2412
2413   g_free (context->description);
2414   context->description = g_strdup (description);
2415 }
2416
2417
2418 /**
2419  * g_option_context_get_description:
2420  * @context: a #GOptionContext
2421  *
2422  * Returns the description. See g_option_context_set_description().
2423  *
2424  * Returns: the description
2425  *
2426  * Since: 2.12
2427  */
2428 const gchar *
2429 g_option_context_get_description (GOptionContext *context)
2430 {
2431   g_return_val_if_fail (context != NULL, NULL);
2432
2433   return context->description;
2434 }