From 4381c32524c999fd7de308ef775c66b4f056497d Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Fri, 31 Jul 2020 15:07:08 +0200 Subject: [PATCH] Add test for parsing with `ignore_invalid_options`. --- Library/Homebrew/cli/args.rb | 6 ++++++ Library/Homebrew/cli/parser.rb | 13 ++++++++++--- Library/Homebrew/test/cli/parser_spec.rb | 8 ++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/Library/Homebrew/cli/args.rb b/Library/Homebrew/cli/args.rb index a959790afb..21231160bb 100644 --- a/Library/Homebrew/cli/args.rb +++ b/Library/Homebrew/cli/args.rb @@ -20,6 +20,7 @@ module Homebrew # Can set these because they will be overwritten by freeze_named_args! # (whereas other values below will only be overwritten if passed). self[:named_args] = argv.reject { |arg| arg.start_with?("-") } + self[:remaining] = [] # Set values needed before Parser#parse has been run. return unless set_default_args @@ -32,6 +33,11 @@ module Homebrew self[:universal?] = argv.include?("--universal") end + def freeze_remaining_args!(remaining_args) + self[:remaining] = remaining_args + self[:remaining].freeze + end + def freeze_named_args!(named_args) # Reset cache values reliant on named_args @formulae = nil diff --git a/Library/Homebrew/cli/parser.rb b/Library/Homebrew/cli/parser.rb index 5e03c5c81a..2e07925421 100644 --- a/Library/Homebrew/cli/parser.rb +++ b/Library/Homebrew/cli/parser.rb @@ -156,9 +156,7 @@ module Homebrew @parser.to_s end - def parse(argv = @argv, ignore_invalid_options: false) - raise "Arguments were already parsed!" if @args_parsed - + def parse_remaining(argv, ignore_invalid_options: false) i = 0 remaining = [] @@ -189,6 +187,14 @@ module Homebrew i += 1 end + [remaining, non_options] + end + + def parse(argv = @argv, ignore_invalid_options: false) + raise "Arguments were already parsed!" if @args_parsed + + remaining, non_options = parse_remaining(argv, ignore_invalid_options: ignore_invalid_options) + named_args = if ignore_invalid_options [] else @@ -198,6 +204,7 @@ module Homebrew check_constraint_violations check_named_args(named_args) @args.freeze_named_args!(named_args) + @args.freeze_remaining_args!(non_options.empty? ? remaining : [*remaining, "--", non_options]) @args.freeze_processed_options!(@processed_options) @args_parsed = true diff --git a/Library/Homebrew/test/cli/parser_spec.rb b/Library/Homebrew/test/cli/parser_spec.rb index a54a3d4c43..c85a78d03b 100644 --- a/Library/Homebrew/test/cli/parser_spec.rb +++ b/Library/Homebrew/test/cli/parser_spec.rb @@ -15,6 +15,14 @@ describe Homebrew::CLI::Parser do allow(Homebrew::EnvConfig).to receive(:pry?).and_return(true) end + context "when `ignore_invalid_options` is true" do + it "passes through invalid options" do + args = parser.parse(["-v", "named-arg", "--not-a-valid-option"], ignore_invalid_options: true) + expect(args.remaining).to eq ["named-arg", "--not-a-valid-option"] + expect(args.named_args).to be_empty + end + end + it "parses short option" do args = parser.parse(["-v"]) expect(args).to be_verbose