From 9c7fe3dcb9dcc1e451fca46e17da2650dff72157 Mon Sep 17 00:00:00 2001 From: Gautham Goli Date: Sat, 12 Jan 2019 18:52:07 +0530 Subject: [PATCH 1/3] cli_parser: Output help text on invalid option passed --- Library/Homebrew/cli_parser.rb | 7 ++++++- Library/Homebrew/test/cli_parser_spec.rb | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Library/Homebrew/cli_parser.rb b/Library/Homebrew/cli_parser.rb index 56ca66be31..e487e8580a 100644 --- a/Library/Homebrew/cli_parser.rb +++ b/Library/Homebrew/cli_parser.rb @@ -123,7 +123,12 @@ module Homebrew end def parse(cmdline_args = ARGV) - remaining_args = @parser.parse(cmdline_args) + begin + remaining_args = @parser.parse(cmdline_args) + rescue OptionParser::InvalidOption => e + puts generate_help_text + raise e + end check_constraint_violations Homebrew.args[:remaining] = remaining_args Homebrew.args.freeze diff --git a/Library/Homebrew/test/cli_parser_spec.rb b/Library/Homebrew/test/cli_parser_spec.rb index 891de621eb..b26cf6b444 100644 --- a/Library/Homebrew/test/cli_parser_spec.rb +++ b/Library/Homebrew/test/cli_parser_spec.rb @@ -42,6 +42,10 @@ describe Homebrew::CLI::Parser do expect { parser.parse(["--random"]) }.to raise_error(OptionParser::InvalidOption, /--random/) end + it "outputs help text on failure" do + expect { parser.parse(["--random"]) }.to raise_error(OptionParser::InvalidOption, /Usage: brew/) + end + it "maps environment var to an option" do parser.parse([]) expect(Homebrew.args.pry?).to be true From 0a0e2fa0c15b0110b1f765be906ddc6faa5a2067 Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Sat, 12 Jan 2019 19:16:44 +0000 Subject: [PATCH 2/3] cli_parser: output help failure text to stderr. --- Library/Homebrew/cli_parser.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Homebrew/cli_parser.rb b/Library/Homebrew/cli_parser.rb index e487e8580a..01310d42e3 100644 --- a/Library/Homebrew/cli_parser.rb +++ b/Library/Homebrew/cli_parser.rb @@ -126,7 +126,7 @@ module Homebrew begin remaining_args = @parser.parse(cmdline_args) rescue OptionParser::InvalidOption => e - puts generate_help_text + $stderr.puts generate_help_text raise e end check_constraint_violations From b71fcedb43bf2e80ce94c20cf29961446ca72481 Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Sat, 12 Jan 2019 19:17:07 +0000 Subject: [PATCH 3/3] cli_parser_spec: tweak/fix help output text test, --- Library/Homebrew/test/cli_parser_spec.rb | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/Library/Homebrew/test/cli_parser_spec.rb b/Library/Homebrew/test/cli_parser_spec.rb index b26cf6b444..975f5ed86b 100644 --- a/Library/Homebrew/test/cli_parser_spec.rb +++ b/Library/Homebrew/test/cli_parser_spec.rb @@ -11,8 +11,8 @@ describe Homebrew::CLI::Parser do } before do + allow(ENV).to receive(:[]) allow(ENV).to receive(:[]).with("HOMEBREW_PRY").and_return("1") - allow(ENV).to receive(:[]).with("HOMEBREW_VERBOSE") end it "parses short option" do @@ -38,12 +38,9 @@ describe Homebrew::CLI::Parser do expect(Homebrew.args.more_verbose?).to be nil end - it "raises an exception when an invalid option is passed" do + it "raises an exception and outputs help text when an invalid option is passed" do expect { parser.parse(["--random"]) }.to raise_error(OptionParser::InvalidOption, /--random/) - end - - it "outputs help text on failure" do - expect { parser.parse(["--random"]) }.to raise_error(OptionParser::InvalidOption, /Usage: brew/) + .and output(/Usage: brew/).to_stderr end it "maps environment var to an option" do