Add test for parsing with ignore_invalid_options.
This commit is contained in:
parent
d4c2ffd705
commit
4381c32524
@ -20,6 +20,7 @@ module Homebrew
|
|||||||
# Can set these because they will be overwritten by freeze_named_args!
|
# Can set these because they will be overwritten by freeze_named_args!
|
||||||
# (whereas other values below will only be overwritten if passed).
|
# (whereas other values below will only be overwritten if passed).
|
||||||
self[:named_args] = argv.reject { |arg| arg.start_with?("-") }
|
self[:named_args] = argv.reject { |arg| arg.start_with?("-") }
|
||||||
|
self[:remaining] = []
|
||||||
|
|
||||||
# Set values needed before Parser#parse has been run.
|
# Set values needed before Parser#parse has been run.
|
||||||
return unless set_default_args
|
return unless set_default_args
|
||||||
@ -32,6 +33,11 @@ module Homebrew
|
|||||||
self[:universal?] = argv.include?("--universal")
|
self[:universal?] = argv.include?("--universal")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def freeze_remaining_args!(remaining_args)
|
||||||
|
self[:remaining] = remaining_args
|
||||||
|
self[:remaining].freeze
|
||||||
|
end
|
||||||
|
|
||||||
def freeze_named_args!(named_args)
|
def freeze_named_args!(named_args)
|
||||||
# Reset cache values reliant on named_args
|
# Reset cache values reliant on named_args
|
||||||
@formulae = nil
|
@formulae = nil
|
||||||
|
|||||||
@ -156,9 +156,7 @@ module Homebrew
|
|||||||
@parser.to_s
|
@parser.to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse(argv = @argv, ignore_invalid_options: false)
|
def parse_remaining(argv, ignore_invalid_options: false)
|
||||||
raise "Arguments were already parsed!" if @args_parsed
|
|
||||||
|
|
||||||
i = 0
|
i = 0
|
||||||
remaining = []
|
remaining = []
|
||||||
|
|
||||||
@ -189,6 +187,14 @@ module Homebrew
|
|||||||
i += 1
|
i += 1
|
||||||
end
|
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
|
named_args = if ignore_invalid_options
|
||||||
[]
|
[]
|
||||||
else
|
else
|
||||||
@ -198,6 +204,7 @@ module Homebrew
|
|||||||
check_constraint_violations
|
check_constraint_violations
|
||||||
check_named_args(named_args)
|
check_named_args(named_args)
|
||||||
@args.freeze_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.freeze_processed_options!(@processed_options)
|
||||||
|
|
||||||
@args_parsed = true
|
@args_parsed = true
|
||||||
|
|||||||
@ -15,6 +15,14 @@ describe Homebrew::CLI::Parser do
|
|||||||
allow(Homebrew::EnvConfig).to receive(:pry?).and_return(true)
|
allow(Homebrew::EnvConfig).to receive(:pry?).and_return(true)
|
||||||
end
|
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
|
it "parses short option" do
|
||||||
args = parser.parse(["-v"])
|
args = parser.parse(["-v"])
|
||||||
expect(args).to be_verbose
|
expect(args).to be_verbose
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user