Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli_parser: Output help text on invalid option passed #5530

Merged
merged 3 commits into from
Jan 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion Library/Homebrew/cli_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,12 @@ def summary
end

def parse(cmdline_args = ARGV)
remaining_args = @parser.parse(cmdline_args)
begin
remaining_args = @parser.parse(cmdline_args)
rescue OptionParser::InvalidOption => e
$stderr.puts generate_help_text
raise e
end
check_constraint_violations
Homebrew.args[:remaining] = remaining_args
Homebrew.args.freeze
Expand Down
5 changes: 3 additions & 2 deletions Library/Homebrew/test/cli_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
}

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
Expand All @@ -38,8 +38,9 @@
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/)
.and output(/Usage: brew/).to_stderr
end

it "maps environment var to an option" do
Expand Down