[glib] gapplication: enable --help when app has options



commit 021c4ad050f979e5b36c34474d5fbb189278acdf
Author: Lars Uebernickel <lars uebernickel canonical com>
Date:   Sat Nov 15 10:30:41 2014 +0100

    gapplication: enable --help when app has options
    
    This should already work according to the documentation, but doesn't
    because main_options is consumed before the check in
    g_application_parse_command_line().
    
    Fix by moving the check for main_options up.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=740157

 gio/gapplication.c       |   28 ++++++++++++++--------------
 gio/tests/gapplication.c |   45 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 59 insertions(+), 14 deletions(-)
---
diff --git a/gio/gapplication.c b/gio/gapplication.c
index 1fa1955..a918ea2 100644
--- a/gio/gapplication.c
+++ b/gio/gapplication.c
@@ -476,6 +476,20 @@ g_application_parse_command_line (GApplication   *application,
 
   context = g_option_context_new (NULL);
 
+  /* If the application has not registered local options and it has
+   * G_APPLICATION_HANDLES_COMMAND_LINE then we have to assume that
+   * their primary instance commandline handler may want to deal with
+   * the arguments.  We must therefore ignore them.
+   *
+   * We must also ignore --help in this case since some applications
+   * will try to handle this from the remote side.  See #737869.
+   */
+  if (application->priv->main_options == NULL && (application->priv->flags & 
G_APPLICATION_HANDLES_COMMAND_LINE))
+    {
+      g_option_context_set_ignore_unknown_options (context, TRUE);
+      g_option_context_set_help_enabled (context, FALSE);
+    }
+
   /* Add the main option group, if it exists */
   if (application->priv->main_options)
     {
@@ -494,20 +508,6 @@ g_application_parse_command_line (GApplication   *application,
                                                               application->priv->option_groups);
     }
 
-  /* If the application has not registered local options and it has
-   * G_APPLICATION_HANDLES_COMMAND_LINE then we have to assume that
-   * their primary instance commandline handler may want to deal with
-   * the arguments.  We must therefore ignore them.
-   *
-   * We must also ignore --help in this case since some applications
-   * will try to handle this from the remote side.  See #737869.
-   */
-  if (application->priv->main_options == NULL && (application->priv->flags & 
G_APPLICATION_HANDLES_COMMAND_LINE))
-    {
-      g_option_context_set_ignore_unknown_options (context, TRUE);
-      g_option_context_set_help_enabled (context, FALSE);
-    }
-
   /* In the case that we are not explicitly marked as a service or a
    * launcher then we want to add the "--gapplication-service" option to
    * allow the process to be made into a service.
diff --git a/gio/tests/gapplication.c b/gio/tests/gapplication.c
index 249aa6e..fca502f 100644
--- a/gio/tests/gapplication.c
+++ b/gio/tests/gapplication.c
@@ -705,6 +705,50 @@ test_resource_path (void)
   g_object_unref (app);
 }
 
+static gint
+test_help_command_line (GApplication            *app,
+                        GApplicationCommandLine *command_line,
+                        gpointer                 user_data)
+{
+  gboolean *called = user_data;
+
+  *called = TRUE;
+
+  return 0;
+}
+
+/* Test whether --help is handled when HANDLES_COMMND_LINE is set and
+ * options have been added.
+ */
+static void
+test_help (void)
+{
+  if (g_test_subprocess ())
+    {
+      char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
+      gchar *argv[] = { binpath, "--help", NULL };
+      GApplication *app;
+      gboolean called = FALSE;
+      int status;
+
+      app = g_application_new ("org.gtk.TestApplication", G_APPLICATION_HANDLES_COMMAND_LINE);
+      g_application_add_main_option (app, "foo", 'f', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, "", "");
+      g_signal_connect (app, "command-line", G_CALLBACK (test_help_command_line), &called);
+
+      status = g_application_run (app, G_N_ELEMENTS (argv) -1, argv);
+      g_assert (called == TRUE);
+      g_assert_cmpint (status, ==, 0);
+
+      g_object_unref (app);
+      g_free (binpath);
+      return;
+    }
+
+  g_test_trap_subprocess (NULL, 0, 0);
+  g_test_trap_assert_passed ();
+  g_test_trap_assert_stdout ("*Application options*");
+}
+
 int
 main (int argc, char **argv)
 {
@@ -724,6 +768,7 @@ main (int argc, char **argv)
   g_test_add_func ("/gapplication/local-command-line", test_local_command_line);
 /*  g_test_add_func ("/gapplication/remote-command-line", test_remote_command_line); */
   g_test_add_func ("/gapplication/resource-path", test_resource_path);
+  g_test_add_func ("/gapplication/test-help", test_help);
 
   return g_test_run ();
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]