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