option parser: check for extra named args

This commit is contained in:
EricFromCanada 2019-12-13 16:50:54 -05:00
parent 0c8eae8d9c
commit aea6d79d21
24 changed files with 52 additions and 22 deletions

View File

@ -34,6 +34,7 @@ module Homebrew
@conflicts = []
@switch_sources = {}
@processed_options = []
@max_named_args = nil
@hide_from_man_page = false
instance_eval(&block)
post_initialize
@ -139,6 +140,7 @@ module Homebrew
raise e
end
check_constraint_violations
check_named_args(remaining_args)
@args[:remaining] = remaining_args
@args.freeze_processed_options!(@processed_options)
Homebrew.args = @args
@ -178,6 +180,10 @@ module Homebrew
[]
end
def max_named(count)
@max_named_args = count
end
def hide_from_man_page!
@hide_from_man_page = true
end
@ -269,6 +275,10 @@ module Homebrew
check_constraints
end
def check_named_args(args)
raise NamedArgumentsError, @max_named_args if !@max_named_args.nil? && args.size > @max_named_args
end
def process_option(*args)
option, = @parser.make_switch(args)
@processed_options << [option.short.first, option.long.first, option.arg, option.desc.first]
@ -277,14 +287,10 @@ module Homebrew
class OptionConstraintError < RuntimeError
def initialize(arg1, arg2, missing: false)
if !missing
message = <<~EOS
`#{arg1}` and `#{arg2}` should be passed together.
EOS
message = if !missing
"`#{arg1}` and `#{arg2}` should be passed together."
else
message = <<~EOS
`#{arg2}` cannot be passed without `#{arg1}`.
EOS
"`#{arg2}` cannot be passed without `#{arg1}`."
end
super message
end
@ -294,17 +300,27 @@ module Homebrew
def initialize(args)
args_list = args.map(&Formatter.public_method(:option))
.join(" and ")
super <<~EOS
Options #{args_list} are mutually exclusive.
EOS
super "Options #{args_list} are mutually exclusive."
end
end
class InvalidConstraintError < RuntimeError
def initialize(arg1, arg2)
super <<~EOS
`#{arg1}` and `#{arg2}` cannot be mutually exclusive and mutually dependent simultaneously.
EOS
super "`#{arg1}` and `#{arg2}` cannot be mutually exclusive and mutually dependent simultaneously."
end
end
class NamedArgumentsError < UsageError
def initialize(maximum)
message = case maximum
when 0
"This command does not take named arguments."
when 1
"This command does not take multiple named arguments."
else
"This command does not take more than #{maximum} named arguments."
end
super message
end
end
end

View File

@ -13,14 +13,13 @@ module Homebrew
Print the version numbers of Homebrew, Homebrew/homebrew-core and Homebrew/homebrew-cask
(if tapped) to standard output.
EOS
max_named 0
end
end
def __version
__version_args.parse
odie "This command does not take arguments." if ARGV.any?
puts "Homebrew #{HOMEBREW_VERSION}"
puts "#{CoreTap.instance.full_name} #{CoreTap.instance.version_string}"
puts "#{Tap.default_cask_tap.full_name} #{Tap.default_cask_tap.version_string}" if Tap.default_cask_tap.installed?

View File

@ -19,14 +19,13 @@ module Homebrew
EOS
switch :verbose
switch :debug
max_named 1
end
end
def analytics
analytics_args.parse
raise UsageError if args.remaining.size > 1
case args.remaining.first
when nil, "state"
if Utils::Analytics.disabled?

View File

@ -12,6 +12,7 @@ module Homebrew
Display the source of <formula>.
EOS
max_named 1
end
end
@ -22,7 +23,6 @@ module Homebrew
# `brew cat` multiple times.
formulae = Homebrew.args.formulae
raise FormulaUnspecifiedError if formulae.empty?
raise "`brew cat` doesn't support multiple arguments" if args.remaining.size > 1
cd HOMEBREW_REPOSITORY
pager = if ENV["HOMEBREW_BAT"].nil?

View File

@ -19,6 +19,7 @@ module Homebrew
description: "Include aliases of internal commands."
switch :verbose
switch :debug
max_named 0
end
end

View File

@ -16,12 +16,12 @@ module Homebrew
EOS
switch :verbose
switch :debug
max_named 0
end
end
def config
config_args.parse
raise UsageError unless args.remaining.empty?
SystemConfig.dump_verbose_config
end

View File

@ -21,6 +21,7 @@ module Homebrew
description: "Explicitly set the <version> of the package being installed."
switch :verbose
switch :debug
max_named 0
end
end

View File

@ -28,6 +28,7 @@ module Homebrew
"be accessible with its link."
switch :verbose
switch :debug
max_named 1
end
end

View File

@ -15,6 +15,7 @@ module Homebrew
List installed formulae that are not dependencies of another installed formula.
EOS
switch :debug
max_named 0
end
end

View File

@ -22,6 +22,7 @@ module Homebrew
description: "Print only one line per commit."
flag "-1", "--max-count",
description: "Print only one or a specified number of commits."
max_named 1
end
end

View File

@ -22,6 +22,7 @@ module Homebrew
description: "Use the standard `PATH` instead of superenv's when `std` is passed."
switch :verbose
switch :debug
max_named 0
end
end

View File

@ -16,6 +16,7 @@ module Homebrew
EOS
switch :verbose
switch :debug
max_named 2
end
end

View File

@ -38,6 +38,7 @@ module Homebrew
switch "-q", "--quieter",
description: "Suppress any warnings."
switch :debug
max_named 2
end
end

View File

@ -65,6 +65,7 @@ module Homebrew
switch :debug
conflicts "--no-audit", "--strict"
conflicts "--url", "--tag"
max_named 1
end
end

View File

@ -22,6 +22,7 @@ module Homebrew
switch :quiet
switch :verbose
switch :debug
max_named 1
end
end
@ -34,7 +35,6 @@ module Homebrew
formulae = Homebrew.args.formulae
raise FormulaUnspecifiedError if formulae.empty?
raise "Multiple formulae given, only one is allowed." if formulae.length > 1
formula = formulae.first
current_revision = formula.revision

View File

@ -49,6 +49,7 @@ module Homebrew
switch :verbose
switch :debug
conflicts "--autotools", "--cmake", "--go", "--meson", "--perl", "--python", "--rust"
max_named 1
end
end

View File

@ -91,6 +91,7 @@ module Homebrew
description: "Extract the specified <version> of <formula> instead of the most recent."
switch :force
switch :debug
max_named 2
end
end

View File

@ -14,6 +14,7 @@ module Homebrew
Install Homebrew's Bundler gems.
EOS
switch :debug
max_named 0
end
end

View File

@ -28,14 +28,13 @@ module Homebrew
"comparison without factoring in the date)."
switch "--link",
description: "This is now done automatically by `brew update`."
max_named 0
end
end
def man
man_args.parse
raise UsageError unless ARGV.named.empty?
odie "`brew man --link` is now done automatically by `brew update`." if args.link?
regenerate_man_pages

View File

@ -16,6 +16,7 @@ module Homebrew
EOS
switch "--markdown",
description: "Print as a Markdown list."
max_named 2
end
end

View File

@ -15,6 +15,7 @@ module Homebrew
EOS
switch :verbose
switch :debug
max_named 1
end
end

View File

@ -29,6 +29,7 @@ module Homebrew
description: "Randomise tests with the specified <value> instead of a random seed."
switch :verbose
switch :debug
max_named 0
end
end

View File

@ -23,6 +23,7 @@ module Homebrew
description: "Use the commit at the specified <date> as the start commit."
switch :verbose
switch :debug
max_named 0
end
end

View File

@ -14,6 +14,7 @@ module Homebrew
Install and commit Homebrew's vendored gems.
EOS
switch :debug
max_named 0
end
end