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