[gjs/gnome-3-34] console: Exit more gracefully when unknown command line option given



commit 640c2c8e95c0b773b3c354b9b07a8eccaea7d373
Author: Philip Chimento <philip chimento gmail com>
Date:   Sat Oct 26 16:39:24 2019 -0700

    console: Exit more gracefully when unknown command line option given
    
    Previously the program would abort if an unknown option was given.
    Instead, print the help text and exit cleanly with code 1.

 gjs/console.cpp                            | 9 +++++++--
 installed-tests/scripts/testCommandLine.sh | 5 +++++
 2 files changed, 12 insertions(+), 2 deletions(-)
---
diff --git a/gjs/console.cpp b/gjs/console.cpp
index b094f07e..e8abcfb2 100644
--- a/gjs/console.cpp
+++ b/gjs/console.cpp
@@ -246,8 +246,13 @@ main(int argc, char **argv)
     debugging = false;
     g_option_context_set_ignore_unknown_options(context, false);
     g_option_context_set_help_enabled(context, true);
-    if (!g_option_context_parse_strv(context, &gjs_argv, &error))
-        g_error("option parsing failed: %s", error->message);
+    if (!g_option_context_parse_strv(context, &gjs_argv, &error)) {
+        char* help_text = g_option_context_get_help(context, true, nullptr);
+        g_printerr("%s\n\n%s\n", error->message, help_text);
+        g_free(help_text);
+        g_option_context_free(context);
+        exit(1);
+    }
 
     g_option_context_free (context);
 
diff --git a/installed-tests/scripts/testCommandLine.sh b/installed-tests/scripts/testCommandLine.sh
index 79c7a5ab..b77d66f4 100755
--- a/installed-tests/scripts/testCommandLine.sh
+++ b/installed-tests/scripts/testCommandLine.sh
@@ -83,6 +83,11 @@ skip () {
     echo "ok $total - $1 # SKIP $2"
 }
 
+$gjs --invalid-option
+report_xfail "Invalid option should exit with failure"
+$gjs --invalid-option 2>&1 | grep -q invalid-option
+report "Invalid option should print a relevant message"
+
 # Test that System.exit() works in gjs-console
 $gjs -c 'imports.system.exit(0)'
 report "System.exit(0) should exit successfully"


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