[glib/glib-2-28] Avoid an interaction between GApplication and GOption
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib/glib-2-28] Avoid an interaction between GApplication and GOption
- Date: Fri, 8 Apr 2011 11:18:25 +0000 (UTC)
commit 2aad89ab9bbad6c743f2f1a4d1cacb31a64faf50
Author: Matthias Clasen <mclasen redhat com>
Date: Fri Apr 8 07:13:54 2011 -0400
Avoid an interaction between GApplication and GOption
When using GOption to handle commandlines, we need to disable
the builtin help handling, since it calls exit(). Also mention
this particular pitfall in the docs.
gio/gapplicationcommandline.c | 5 ++++-
gio/tests/gapplication-example-cmdline3.c | 11 +++++++++++
2 files changed, 15 insertions(+), 1 deletions(-)
---
diff --git a/gio/gapplicationcommandline.c b/gio/gapplicationcommandline.c
index b3c98f5..7e4d333 100644
--- a/gio/gapplicationcommandline.c
+++ b/gio/gapplicationcommandline.c
@@ -106,7 +106,10 @@ G_DEFINE_TYPE (GApplicationCommandLine, g_application_command_line, G_TYPE_OBJEC
* </para>
* <para>
* This example also shows how to use #GOptionContext for parsing the
- * commandline arguments.
+ * commandline arguments. Note that it is necessary to disable the
+ * built-in help-handling of #GOptionContext, since it calls exit()
+ * after printing help, which is not what you want to happen in
+ * the primary instance.
* </para>
* <programlisting>
* <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../gio/tests/gapplication-example-cmdline3.c">
diff --git a/gio/tests/gapplication-example-cmdline3.c b/gio/tests/gapplication-example-cmdline3.c
index 3d0c8b1..a573a4a 100644
--- a/gio/tests/gapplication-example-cmdline3.c
+++ b/gio/tests/gapplication-example-cmdline3.c
@@ -11,10 +11,12 @@ my_cmdline_handler (gpointer data)
gint argc;
gint arg1;
gboolean arg2;
+ gboolean help;
GOptionContext *context;
GOptionEntry entries[] = {
{ "arg1", 0, 0, G_OPTION_ARG_INT, &arg1, NULL, NULL },
{ "arg2", 0, 0, G_OPTION_ARG_NONE, &arg2, NULL, NULL },
+ { "help", '?', 0, G_OPTION_ARG_NONE, &help, NULL, NULL },
{ NULL }
};
GError *error;
@@ -30,10 +32,12 @@ my_cmdline_handler (gpointer data)
argv[i] = args[i];
context = g_option_context_new (NULL);
+ g_option_context_set_help_enabled (context, FALSE);
g_option_context_add_main_entries (context, entries, NULL);
arg1 = 0;
arg2 = FALSE;
+ help = FALSE;
error = NULL;
if (!g_option_context_parse (context, &argc, &argv, &error))
{
@@ -41,6 +45,13 @@ my_cmdline_handler (gpointer data)
g_error_free (error);
g_application_command_line_set_exit_status (cmdline, 1);
}
+ else if (help)
+ {
+ gchar *text;
+ text = g_option_context_get_help (context, FALSE, NULL);
+ g_application_command_line_print (cmdline, "%s", text);
+ g_free (text);
+ }
else
{
g_application_command_line_print (cmdline, "arg1 is %d and arg2 is %s\n",
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]