From ba69f05e9977d858b26998bb09515f7a389a4632 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Fri, 30 Nov 2018 23:29:19 -0500 Subject: [PATCH] remove backtrace from stderr for unknown CLI options - close #69 --- lib/rscons/cli.rb | 14 ++++++++++++-- spec/build_tests_spec.rb | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/lib/rscons/cli.rb b/lib/rscons/cli.rb index 7906986..bd93a4a 100644 --- a/lib/rscons/cli.rb +++ b/lib/rscons/cli.rb @@ -43,6 +43,18 @@ module Rscons # @return [void] def run(argv) argv = argv.dup + begin + exit run_toplevel(argv) + rescue OptionParser::InvalidOption => io + $stderr.puts io.message + $stderr.puts USAGE + exit 2 + end + end + + private + + def run_toplevel(argv) rsconscript = nil do_help = false @@ -115,8 +127,6 @@ module Rscons exit Rscons.application.run(operation, script, operation_options) end - private - def parse_operation_args(operation, argv) case operation when "configure" diff --git a/spec/build_tests_spec.rb b/spec/build_tests_spec.rb index 97e7bdb..e4dc677 100644 --- a/spec/build_tests_spec.rb +++ b/spec/build_tests_spec.rb @@ -1531,6 +1531,24 @@ EOF expect(result.stderr).to match /Unknown operation: unknownop/ expect(result.status).to_not eq 0 end + + it "displays usage and error message without a backtrace for an invalid CLI option" do + test_dir "simple" + result = run_rscons(rscons_args: %w[--xyz]) + expect(result.stderr).to_not match /Traceback/ + expect(result.stderr).to match /invalid option.*--xyz/ + expect(result.stderr).to match /Usage:/ + expect(result.status).to_not eq 0 + end + + it "displays usage and error message without a backtrace for an invalid CLI option to a valid subcommand" do + test_dir "simple" + result = run_rscons(op: %w[configure --xyz]) + expect(result.stderr).to_not match /Traceback/ + expect(result.stderr).to match /invalid option.*--xyz/ + expect(result.stderr).to match /Usage:/ + expect(result.status).to_not eq 0 + end end context "configure" do