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

View File

@ -13,14 +13,13 @@ module Homebrew
Print the version numbers of Homebrew, Homebrew/homebrew-core and Homebrew/homebrew-cask Print the version numbers of Homebrew, Homebrew/homebrew-core and Homebrew/homebrew-cask
(if tapped) to standard output. (if tapped) to standard output.
EOS EOS
max_named 0
end end
end end
def __version def __version
__version_args.parse __version_args.parse
odie "This command does not take arguments." if ARGV.any?
puts "Homebrew #{HOMEBREW_VERSION}" puts "Homebrew #{HOMEBREW_VERSION}"
puts "#{CoreTap.instance.full_name} #{CoreTap.instance.version_string}" 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? 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 EOS
switch :verbose switch :verbose
switch :debug switch :debug
max_named 1
end end
end end
def analytics def analytics
analytics_args.parse analytics_args.parse
raise UsageError if args.remaining.size > 1
case args.remaining.first case args.remaining.first
when nil, "state" when nil, "state"
if Utils::Analytics.disabled? if Utils::Analytics.disabled?

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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