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
This commit is contained in:
Lars Uebernickel 2014-11-15 10:30:41 +01:00
parent 61cecd5a68
commit 021c4ad050
2 changed files with 59 additions and 14 deletions

View File

@ -476,6 +476,20 @@ g_application_parse_command_line (GApplication *application,
context = g_option_context_new (NULL); 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 */ /* Add the main option group, if it exists */
if (application->priv->main_options) if (application->priv->main_options)
{ {
@ -494,20 +508,6 @@ g_application_parse_command_line (GApplication *application,
application->priv->option_groups); 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 /* 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 * launcher then we want to add the "--gapplication-service" option to
* allow the process to be made into a service. * allow the process to be made into a service.

View File

@ -705,6 +705,50 @@ test_resource_path (void)
g_object_unref (app); 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 int
main (int argc, char **argv) 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/local-command-line", test_local_command_line);
/* g_test_add_func ("/gapplication/remote-command-line", test_remote_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/resource-path", test_resource_path);
g_test_add_func ("/gapplication/test-help", test_help);
return g_test_run (); return g_test_run ();
} }