diff --git a/Library/Homebrew/cli/named_args.rb b/Library/Homebrew/cli/named_args.rb index 1c90f6032f..34b9b04049 100644 --- a/Library/Homebrew/cli/named_args.rb +++ b/Library/Homebrew/cli/named_args.rb @@ -188,6 +188,18 @@ module Homebrew .map(&:freeze).freeze end + sig { returns(T::Array[Tap]) } + def to_taps + @to_taps ||= downcased_unique_named.map { |name| Tap.fetch name }.uniq.freeze + end + + sig { returns(T::Array[Tap]) } + def to_installed_taps + @to_installed_taps ||= to_taps.each do |tap| + raise TapUnavailableError, tap.name unless tap.installed? + end.uniq.freeze + end + sig { returns(T::Array[String]) } def homebrew_tap_cask_names downcased_unique_named.grep(HOMEBREW_CASK_TAP_CASK_REGEX) diff --git a/Library/Homebrew/cli/parser.rb b/Library/Homebrew/cli/parser.rb index 3b635e7e5b..d2454f0bc5 100644 --- a/Library/Homebrew/cli/parser.rb +++ b/Library/Homebrew/cli/parser.rb @@ -126,9 +126,9 @@ module Homebrew @conflicts = [] @switch_sources = {} @processed_options = [] + @named_args_type = nil @max_named_args = nil @min_named_args = nil - @min_named_type = nil @hide_from_man_page = false @formula_options = false @@ -355,33 +355,71 @@ module Homebrew @formula_options = true end + sig do + params( + type: T.any(Symbol, T::Array[String], T::Array[Symbol]), + number: T.nilable(Integer), + min: T.nilable(Integer), + max: T.nilable(Integer), + ).void + end + def named_args(type = nil, number: nil, min: nil, max: nil) + if number.present? && (min.present? || max.present?) + raise ArgumentError, "Do not specify both `number` and `min` or `max`" + end + + if type == :none && (number.present? || min.present? || max.present?) + raise ArgumentError, "Do not specify both `number`, `min` or `max` with `named_args :none`" + end + + @named_args_type = type + + if type == :none + @max_named_args = 0 + elsif number.present? + @min_named_args = @max_named_args = number + elsif min.present? || max.present? + @min_named_args = min + @max_named_args = max + end + end + def max_named(count) + # TODO: (2.8) uncomment for the next major/minor release + # odeprecated "`max_named`", "`named_args max:`" + raise TypeError, "Unsupported type #{count.class.name} for max_named" unless count.is_a?(Integer) @max_named_args = count end def min_named(count_or_type) + # TODO: (2.8) uncomment for the next major/minor release + # odeprecated "`min_named`", "`named_args min:`" + case count_or_type when Integer @min_named_args = count_or_type - @min_named_type = nil + @named_args_type = nil when Symbol @min_named_args = 1 - @min_named_type = count_or_type + @named_args_type = count_or_type else raise TypeError, "Unsupported type #{count_or_type.class.name} for min_named" end end def named(count_or_type) + # TODO: (2.8) uncomment for the next major/minor release + # odeprecated "`named`", "`named_args`" + case count_or_type when Integer @max_named_args = @min_named_args = count_or_type - @min_named_type = nil + @named_args_type = nil when Symbol @max_named_args = @min_named_args = 1 - @min_named_type = count_or_type + @named_args_type = count_or_type else raise TypeError, "Unsupported type #{count_or_type.class.name} for named" end @@ -480,15 +518,15 @@ module Homebrew def check_named_args(args) exception = if @min_named_args && args.size < @min_named_args - case @min_named_type - when :cask - Cask::CaskUnspecifiedError - when :formula - FormulaUnspecifiedError - when :formula_or_cask - FormulaOrCaskUnspecifiedError - when :keg - KegUnspecifiedError + if @named_args_type.present? + types = @named_args_type.is_a?(Array) ? @named_args_type : [@named_args_type] + if types.any? { |arg| arg.is_a? String } + MinNamedArgumentsError.new(@min_named_args) + else + list = types.map { |type| type.to_s.tr("_", " ") } + list = list.to_sentence two_words_connector: " or ", last_word_connector: " or " + UsageError.new("this command requires a #{list} argument") + end else MinNamedArgumentsError.new(@min_named_args) end diff --git a/Library/Homebrew/cmd/--cache.rb b/Library/Homebrew/cmd/--cache.rb index 0641cc0613..3ea31bdffa 100644 --- a/Library/Homebrew/cmd/--cache.rb +++ b/Library/Homebrew/cmd/--cache.rb @@ -32,6 +32,8 @@ module Homebrew description: "Only show cache files for casks." conflicts "--build-from-source", "--force-bottle" conflicts "--formula", "--cask" + + named_args [:formula, :cask] end end diff --git a/Library/Homebrew/cmd/--caskroom.rb b/Library/Homebrew/cmd/--caskroom.rb index 6facedc9c8..cf65933cb2 100644 --- a/Library/Homebrew/cmd/--caskroom.rb +++ b/Library/Homebrew/cmd/--caskroom.rb @@ -17,6 +17,8 @@ module Homebrew If is provided, display the location in the Caskroom where would be installed, without any sort of versioned directory as the last path. EOS + + named_args :cask end end diff --git a/Library/Homebrew/cmd/--cellar.rb b/Library/Homebrew/cmd/--cellar.rb index a4be852c20..a6f88377a3 100644 --- a/Library/Homebrew/cmd/--cellar.rb +++ b/Library/Homebrew/cmd/--cellar.rb @@ -17,6 +17,8 @@ module Homebrew If is provided, display the location in the Cellar where would be installed, without any sort of versioned directory as the last path. EOS + + named_args :formula end end diff --git a/Library/Homebrew/cmd/--env.rb b/Library/Homebrew/cmd/--env.rb index b8c9107e05..483a6c0b0d 100644 --- a/Library/Homebrew/cmd/--env.rb +++ b/Library/Homebrew/cmd/--env.rb @@ -27,6 +27,8 @@ module Homebrew "or `--shell=auto` to detect the current shell." switch "--plain", description: "Generate plain output even when piped." + + named_args :formula end end diff --git a/Library/Homebrew/cmd/--prefix.rb b/Library/Homebrew/cmd/--prefix.rb index 8e281c7764..dce8c6e8c1 100644 --- a/Library/Homebrew/cmd/--prefix.rb +++ b/Library/Homebrew/cmd/--prefix.rb @@ -25,6 +25,8 @@ module Homebrew EOS switch "--unbrewed", description: "List files in Homebrew's prefix not installed by Homebrew." + + named_args :formula end end diff --git a/Library/Homebrew/cmd/--repository.rb b/Library/Homebrew/cmd/--repository.rb index 3f68fa9c4c..4a301ae45d 100644 --- a/Library/Homebrew/cmd/--repository.rb +++ b/Library/Homebrew/cmd/--repository.rb @@ -18,6 +18,8 @@ module Homebrew If `/` are provided, display where tap `/`'s directory is located. EOS + + named_args :tap end end @@ -27,7 +29,7 @@ module Homebrew if args.no_named? puts HOMEBREW_REPOSITORY else - puts args.named.map { |tap| Tap.fetch(tap).path } + puts args.named.to_taps.map(&:path) end end end diff --git a/Library/Homebrew/cmd/--version.rb b/Library/Homebrew/cmd/--version.rb index de305d3579..5a06e7efed 100644 --- a/Library/Homebrew/cmd/--version.rb +++ b/Library/Homebrew/cmd/--version.rb @@ -18,7 +18,7 @@ module Homebrew (if tapped) to standard output. EOS - max_named 0 + named_args :none end end diff --git a/Library/Homebrew/cmd/analytics.rb b/Library/Homebrew/cmd/analytics.rb index aa64043274..a1b296835b 100644 --- a/Library/Homebrew/cmd/analytics.rb +++ b/Library/Homebrew/cmd/analytics.rb @@ -27,7 +27,7 @@ module Homebrew Regenerate the UUID used for Homebrew's analytics. EOS - max_named 1 + named_args %w[state on off regenerate-uuid], max: 1 end end diff --git a/Library/Homebrew/cmd/autoremove.rb b/Library/Homebrew/cmd/autoremove.rb index e65d3a59b0..51b5b07047 100644 --- a/Library/Homebrew/cmd/autoremove.rb +++ b/Library/Homebrew/cmd/autoremove.rb @@ -18,7 +18,7 @@ module Homebrew switch "-n", "--dry-run", description: "List what would be uninstalled, but do not actually uninstall anything." - max_named 0 + named_args :none end end diff --git a/Library/Homebrew/cmd/cleanup.rb b/Library/Homebrew/cmd/cleanup.rb index 2900e3e2da..290bac0618 100644 --- a/Library/Homebrew/cmd/cleanup.rb +++ b/Library/Homebrew/cmd/cleanup.rb @@ -31,6 +31,8 @@ module Homebrew "If you want to delete those too: `rm -rf \"$(brew --cache)\"`" switch "--prune-prefix", description: "Only prune the symlinks and directories from the prefix and remove no other files." + + named_args [:formula, :cask] end end diff --git a/Library/Homebrew/cmd/commands.rb b/Library/Homebrew/cmd/commands.rb index 92fed7e063..137b3de924 100644 --- a/Library/Homebrew/cmd/commands.rb +++ b/Library/Homebrew/cmd/commands.rb @@ -22,7 +22,7 @@ module Homebrew depends_on: "--quiet", description: "Include aliases of internal commands." - max_named 0 + named_args :none end end diff --git a/Library/Homebrew/cmd/completions.rb b/Library/Homebrew/cmd/completions.rb index 3aad3aa9c8..d0a6ebcc96 100644 --- a/Library/Homebrew/cmd/completions.rb +++ b/Library/Homebrew/cmd/completions.rb @@ -25,7 +25,7 @@ module Homebrew Link or unlink Homebrew's completions. EOS - max_named 1 + named_args %w[state link unlink], max: 1 end end diff --git a/Library/Homebrew/cmd/config.rb b/Library/Homebrew/cmd/config.rb index 3153cdd54b..7a93e543d5 100644 --- a/Library/Homebrew/cmd/config.rb +++ b/Library/Homebrew/cmd/config.rb @@ -19,7 +19,7 @@ module Homebrew a bug report, you will be required to provide this information. EOS - max_named 0 + named_args :none end end diff --git a/Library/Homebrew/cmd/deps.rb b/Library/Homebrew/cmd/deps.rb index bddd8cd80d..1ed15e5d3d 100644 --- a/Library/Homebrew/cmd/deps.rb +++ b/Library/Homebrew/cmd/deps.rb @@ -66,6 +66,8 @@ module Homebrew conflicts "--installed", "--all" conflicts "--formula", "--cask" formula_options + + named_args [:formula, :cask] end end diff --git a/Library/Homebrew/cmd/desc.rb b/Library/Homebrew/cmd/desc.rb index b2b0b8675e..6c4b9344fb 100644 --- a/Library/Homebrew/cmd/desc.rb +++ b/Library/Homebrew/cmd/desc.rb @@ -34,7 +34,8 @@ module Homebrew "it is interpreted as a regular expression." conflicts "--search", "--name", "--description" - min_named 1 + + named_args :formula end end @@ -50,10 +51,14 @@ module Homebrew end results = if search_type.nil? + raise FormulaUnspecifiedError if args.no_named? + desc = {} args.named.to_formulae.each { |f| desc[f.full_name] = f.desc } Descriptions.new(desc) else + raise UsageError, "this command requires a search term" if args.no_named? + query = args.named.join(" ") string_or_regex = query_regexp(query) CacheStoreDatabase.use(:descriptions) do |db| diff --git a/Library/Homebrew/cmd/doctor.rb b/Library/Homebrew/cmd/doctor.rb index b243cd802f..b43b7a4c37 100644 --- a/Library/Homebrew/cmd/doctor.rb +++ b/Library/Homebrew/cmd/doctor.rb @@ -27,6 +27,8 @@ module Homebrew "if provided as arguments." switch "-D", "--audit-debug", description: "Enable debugging and profiling of audit methods." + + named_args :diagnostic_check end end diff --git a/Library/Homebrew/cmd/fetch.rb b/Library/Homebrew/cmd/fetch.rb index 7736cc249d..978d944d42 100644 --- a/Library/Homebrew/cmd/fetch.rb +++ b/Library/Homebrew/cmd/fetch.rb @@ -58,7 +58,7 @@ module Homebrew conflicts "--cask", "--build-bottle" conflicts "--cask", "--force-bottle" - min_named :formula_or_cask + named_args [:formula, :cask], min: 1 end end diff --git a/Library/Homebrew/cmd/gist-logs.rb b/Library/Homebrew/cmd/gist-logs.rb index b8735643b4..a94d7c4513 100644 --- a/Library/Homebrew/cmd/gist-logs.rb +++ b/Library/Homebrew/cmd/gist-logs.rb @@ -33,7 +33,7 @@ module Homebrew description: "The Gist will be marked private and will not appear in listings but will "\ "be accessible with its link." - named :formula + named_args :formula, number: 1 end end diff --git a/Library/Homebrew/cmd/home.rb b/Library/Homebrew/cmd/home.rb index 89ca716e6b..58d7c1126a 100644 --- a/Library/Homebrew/cmd/home.rb +++ b/Library/Homebrew/cmd/home.rb @@ -23,6 +23,8 @@ module Homebrew switch "--cask", "--casks", description: "Treat all named arguments as casks." conflicts "--formula", "--cask" + + named_args [:formula, :cask] end end diff --git a/Library/Homebrew/cmd/info.rb b/Library/Homebrew/cmd/info.rb index 2d6032f5e1..7956ac39f8 100644 --- a/Library/Homebrew/cmd/info.rb +++ b/Library/Homebrew/cmd/info.rb @@ -68,6 +68,8 @@ module Homebrew conflicts "--formula", "--cask" conflicts "--installed", "--all" + + named_args [:formula, :cask] end end diff --git a/Library/Homebrew/cmd/install.rb b/Library/Homebrew/cmd/install.rb index 292bd59b7a..06fc5e2504 100644 --- a/Library/Homebrew/cmd/install.rb +++ b/Library/Homebrew/cmd/install.rb @@ -125,7 +125,8 @@ module Homebrew conflicts "--ignore-dependencies", "--only-dependencies" conflicts "--build-from-source", "--build-bottle", "--force-bottle" - min_named :formula_or_cask + + named_args [:formula, :cask], min: 1 end end diff --git a/Library/Homebrew/cmd/leaves.rb b/Library/Homebrew/cmd/leaves.rb index 11563f2e30..04efcdd3b9 100644 --- a/Library/Homebrew/cmd/leaves.rb +++ b/Library/Homebrew/cmd/leaves.rb @@ -18,7 +18,7 @@ module Homebrew List installed formulae that are not dependencies of another installed formula. EOS - max_named 0 + named_args :none end end diff --git a/Library/Homebrew/cmd/link.rb b/Library/Homebrew/cmd/link.rb index b8a3204712..29365dd003 100644 --- a/Library/Homebrew/cmd/link.rb +++ b/Library/Homebrew/cmd/link.rb @@ -29,7 +29,7 @@ module Homebrew switch "-f", "--force", description: "Allow keg-only formulae to be linked." - min_named :keg + named_args :installed_formula, min: 1 end end diff --git a/Library/Homebrew/cmd/list.rb b/Library/Homebrew/cmd/list.rb index ab38eae271..57446422e6 100644 --- a/Library/Homebrew/cmd/list.rb +++ b/Library/Homebrew/cmd/list.rb @@ -72,6 +72,8 @@ module Homebrew conflicts "--full-name", flag conflicts "--cask", flag end + + named_args [:installed_formula, :installed_cask] end end diff --git a/Library/Homebrew/cmd/log.rb b/Library/Homebrew/cmd/log.rb index 391d213f52..981c36c9cf 100644 --- a/Library/Homebrew/cmd/log.rb +++ b/Library/Homebrew/cmd/log.rb @@ -30,7 +30,8 @@ module Homebrew description: "Print only a specified number of commits." conflicts "-1", "--max-count" - max_named 1 + + named_args :formula, max: 1 end end diff --git a/Library/Homebrew/cmd/migrate.rb b/Library/Homebrew/cmd/migrate.rb index 6f7cfbca13..e2455dc6b5 100644 --- a/Library/Homebrew/cmd/migrate.rb +++ b/Library/Homebrew/cmd/migrate.rb @@ -22,7 +22,7 @@ module Homebrew description: "Treat installed and provided as if they are from "\ "the same taps and migrate them anyway." - min_named :formula + named_args :installed_formula, min: 1 end end diff --git a/Library/Homebrew/cmd/missing.rb b/Library/Homebrew/cmd/missing.rb index 4eabe779ee..1b0f2ef1d6 100644 --- a/Library/Homebrew/cmd/missing.rb +++ b/Library/Homebrew/cmd/missing.rb @@ -24,6 +24,8 @@ module Homebrew comma_array "--hide", description: "Act as if none of the specified are installed. should be "\ "a comma-separated list of formulae." + + named_args :formula end end diff --git a/Library/Homebrew/cmd/options.rb b/Library/Homebrew/cmd/options.rb index b94c0aa6f3..f87a2f81ec 100644 --- a/Library/Homebrew/cmd/options.rb +++ b/Library/Homebrew/cmd/options.rb @@ -29,6 +29,8 @@ module Homebrew description: "Show options for the specified ." conflicts "--installed", "--all", "--command" + + named_args :formula end end diff --git a/Library/Homebrew/cmd/outdated.rb b/Library/Homebrew/cmd/outdated.rb index 2a41e1752f..112fcf0cd4 100644 --- a/Library/Homebrew/cmd/outdated.rb +++ b/Library/Homebrew/cmd/outdated.rb @@ -42,6 +42,8 @@ module Homebrew conflicts "--quiet", "--verbose", "--json" conflicts "--formula", "--cask" + + named_args [:formula, :cask] end end diff --git a/Library/Homebrew/cmd/pin.rb b/Library/Homebrew/cmd/pin.rb index ce0b48ffa7..c9367ea006 100644 --- a/Library/Homebrew/cmd/pin.rb +++ b/Library/Homebrew/cmd/pin.rb @@ -19,7 +19,7 @@ module Homebrew issuing the `brew upgrade` command. See also `unpin`. EOS - min_named :formula + named_args :installed_formula, min: 1 end end diff --git a/Library/Homebrew/cmd/postinstall.rb b/Library/Homebrew/cmd/postinstall.rb index de70cae3d5..a1d5cb2ff7 100644 --- a/Library/Homebrew/cmd/postinstall.rb +++ b/Library/Homebrew/cmd/postinstall.rb @@ -19,7 +19,7 @@ module Homebrew Rerun the post-install steps for . EOS - min_named :keg + named_args :installed_formula, min: 1 end end diff --git a/Library/Homebrew/cmd/readall.rb b/Library/Homebrew/cmd/readall.rb index 353733bc9d..0ee287edef 100644 --- a/Library/Homebrew/cmd/readall.rb +++ b/Library/Homebrew/cmd/readall.rb @@ -24,6 +24,8 @@ module Homebrew description: "Verify any alias symlinks in each tap." switch "--syntax", description: "Syntax-check all of Homebrew's Ruby files (if no `` is passed)." + + named_args :tap end end @@ -41,7 +43,7 @@ module Homebrew taps = if args.no_named? Tap else - args.named.map { |t| Tap.fetch(t) } + args.named.to_installed_taps end taps.each do |tap| Homebrew.failed = true unless Readall.valid_tap?(tap, options) diff --git a/Library/Homebrew/cmd/reinstall.rb b/Library/Homebrew/cmd/reinstall.rb index 1f1c87d417..8ac00a61f4 100644 --- a/Library/Homebrew/cmd/reinstall.rb +++ b/Library/Homebrew/cmd/reinstall.rb @@ -75,7 +75,8 @@ module Homebrew cask_options conflicts "--build-from-source", "--force-bottle" - min_named :formula_or_cask + + named_args [:formula, :cask], min: 1 end end diff --git a/Library/Homebrew/cmd/search.rb b/Library/Homebrew/cmd/search.rb index 346b7beae1..0b08fa14ea 100644 --- a/Library/Homebrew/cmd/search.rb +++ b/Library/Homebrew/cmd/search.rb @@ -66,6 +66,9 @@ module Homebrew conflicts("--desc", "--pull-request") conflicts(*package_manager_switches) + + # TODO: (2.9) uncomment when the `odeprecated`/`odisabled` for `brew search` with no arguments is removed + # named_args min: 1 end end diff --git a/Library/Homebrew/cmd/tap-info.rb b/Library/Homebrew/cmd/tap-info.rb index 4d6cd7436d..29bff4ddf0 100644 --- a/Library/Homebrew/cmd/tap-info.rb +++ b/Library/Homebrew/cmd/tap-info.rb @@ -24,6 +24,8 @@ module Homebrew description: "Print a JSON representation of . Currently the default and only accepted "\ "value for is `v1`. See the docs for examples of using the JSON "\ "output: " + + named_args :tap end end @@ -33,9 +35,7 @@ module Homebrew taps = if args.installed? Tap else - args.named.sort.map do |name| - Tap.fetch(name) - end + args.named.to_taps end if args.json diff --git a/Library/Homebrew/cmd/tap.rb b/Library/Homebrew/cmd/tap.rb index 8fe4f1a5be..13661a4f0b 100644 --- a/Library/Homebrew/cmd/tap.rb +++ b/Library/Homebrew/cmd/tap.rb @@ -41,7 +41,7 @@ module Homebrew switch "--list-pinned", description: "List all pinned taps." - max_named 2 + named_args :tap, max: 2 end end diff --git a/Library/Homebrew/cmd/uninstall.rb b/Library/Homebrew/cmd/uninstall.rb index 179fdc032c..5d76f42b01 100644 --- a/Library/Homebrew/cmd/uninstall.rb +++ b/Library/Homebrew/cmd/uninstall.rb @@ -40,7 +40,7 @@ module Homebrew description: "Treat all named arguments as casks." conflicts "--formula", "--cask" - min_named :formula_or_cask + named_args [:installed_formula, :installed_cask], min: 1 end end diff --git a/Library/Homebrew/cmd/unlink.rb b/Library/Homebrew/cmd/unlink.rb index 43a9a8dca1..02bbbab6e8 100644 --- a/Library/Homebrew/cmd/unlink.rb +++ b/Library/Homebrew/cmd/unlink.rb @@ -24,7 +24,7 @@ module Homebrew description: "List files which would be unlinked without actually unlinking or "\ "deleting any files." - min_named :keg + named_args :installed_formula, min: 1 end end diff --git a/Library/Homebrew/cmd/unpin.rb b/Library/Homebrew/cmd/unpin.rb index 0c1b7fcbcf..f763a80e1b 100644 --- a/Library/Homebrew/cmd/unpin.rb +++ b/Library/Homebrew/cmd/unpin.rb @@ -19,7 +19,7 @@ module Homebrew See also `pin`. EOS - min_named :formula + named_args :installed_formula, min: 1 end end diff --git a/Library/Homebrew/cmd/untap.rb b/Library/Homebrew/cmd/untap.rb index 66529cc027..1bdb6b3f5b 100644 --- a/Library/Homebrew/cmd/untap.rb +++ b/Library/Homebrew/cmd/untap.rb @@ -17,15 +17,14 @@ module Homebrew Remove a tapped formula repository. EOS - min_named 1 + named_args :tap, min: 1 end end def untap args = untap_args.parse - args.named.each do |tapname| - tap = Tap.fetch(tapname) + args.named.to_installed_taps.each do |tap| odie "Untapping #{tap} is not allowed" if tap.core_tap? installed_tap_formulae = Formula.installed.select { |formula| formula.tap == tap } diff --git a/Library/Homebrew/cmd/upgrade.rb b/Library/Homebrew/cmd/upgrade.rb index f134f773e8..038083fcff 100644 --- a/Library/Homebrew/cmd/upgrade.rb +++ b/Library/Homebrew/cmd/upgrade.rb @@ -89,6 +89,8 @@ module Homebrew cask_options conflicts "--build-from-source", "--force-bottle" + + named_args [:installed_formula, :installed_cask] end end diff --git a/Library/Homebrew/cmd/uses.rb b/Library/Homebrew/cmd/uses.rb index a16c763035..7ad2e3c6a4 100644 --- a/Library/Homebrew/cmd/uses.rb +++ b/Library/Homebrew/cmd/uses.rb @@ -46,7 +46,8 @@ module Homebrew description: "Include only casks." conflicts "--formula", "--cask" - min_named :formula + + named_args :formula, min: 1 end end diff --git a/Library/Homebrew/dev-cmd/audit.rb b/Library/Homebrew/dev-cmd/audit.rb index 264239680d..3a8e3fcfa0 100644 --- a/Library/Homebrew/dev-cmd/audit.rb +++ b/Library/Homebrew/dev-cmd/audit.rb @@ -90,6 +90,8 @@ module Homebrew conflicts "--display-cop-names", "--skip-style" conflicts "--display-cop-names", "--only-cops" conflicts "--display-cop-names", "--except-cops" + + named_args [:formula, :cask] end end diff --git a/Library/Homebrew/dev-cmd/bottle.rb b/Library/Homebrew/dev-cmd/bottle.rb index f406ce4344..02b46eb88b 100644 --- a/Library/Homebrew/dev-cmd/bottle.rb +++ b/Library/Homebrew/dev-cmd/bottle.rb @@ -86,7 +86,8 @@ module Homebrew description: "Use the specified as the root of the bottle's URL instead of Homebrew's default." conflicts "--no-rebuild", "--keep-old" - min_named 1 + + named_args :installed_formula, min: 1 end end diff --git a/Library/Homebrew/dev-cmd/bump-cask-pr.rb b/Library/Homebrew/dev-cmd/bump-cask-pr.rb index 56a4841802..d9c7c3f66b 100644 --- a/Library/Homebrew/dev-cmd/bump-cask-pr.rb +++ b/Library/Homebrew/dev-cmd/bump-cask-pr.rb @@ -52,7 +52,8 @@ module Homebrew conflicts "--dry-run", "--write" conflicts "--no-audit", "--online" - named 1 + + named_args :cask, number: 1 end end diff --git a/Library/Homebrew/dev-cmd/bump-formula-pr.rb b/Library/Homebrew/dev-cmd/bump-formula-pr.rb index 93f9b0cd61..c038cf9d99 100644 --- a/Library/Homebrew/dev-cmd/bump-formula-pr.rb +++ b/Library/Homebrew/dev-cmd/bump-formula-pr.rb @@ -79,7 +79,8 @@ module Homebrew conflicts "--no-audit", "--strict" conflicts "--no-audit", "--online" conflicts "--url", "--tag" - max_named 1 + + named_args :formula, max: 1 end end diff --git a/Library/Homebrew/dev-cmd/bump-revision.rb b/Library/Homebrew/dev-cmd/bump-revision.rb index ba33a6494c..c15adc09d1 100644 --- a/Library/Homebrew/dev-cmd/bump-revision.rb +++ b/Library/Homebrew/dev-cmd/bump-revision.rb @@ -23,7 +23,7 @@ module Homebrew flag "--message=", description: "Append to the default commit message." - min_named :formula + named_args :formula, min: 1 end end diff --git a/Library/Homebrew/dev-cmd/bump-unversioned-casks.rb b/Library/Homebrew/dev-cmd/bump-unversioned-casks.rb index 9712474d5d..83811e9651 100644 --- a/Library/Homebrew/dev-cmd/bump-unversioned-casks.rb +++ b/Library/Homebrew/dev-cmd/bump-unversioned-casks.rb @@ -29,7 +29,7 @@ module Homebrew flag "--state-file=", description: "File for caching state." - min_named 1 + named_args [:cask, :tap], min: 1 end end diff --git a/Library/Homebrew/dev-cmd/bump.rb b/Library/Homebrew/dev-cmd/bump.rb index adf8878735..9e23cb6bdf 100644 --- a/Library/Homebrew/dev-cmd/bump.rb +++ b/Library/Homebrew/dev-cmd/bump.rb @@ -21,6 +21,8 @@ module Homebrew description: "Limit number of package results returned." switch :verbose switch :debug + + named_args :formula end end diff --git a/Library/Homebrew/dev-cmd/cat.rb b/Library/Homebrew/dev-cmd/cat.rb index 3cdb7acb82..6c49fd8fab 100644 --- a/Library/Homebrew/dev-cmd/cat.rb +++ b/Library/Homebrew/dev-cmd/cat.rb @@ -23,7 +23,7 @@ module Homebrew description: "Treat all named arguments as casks." conflicts "--formula", "--cask" - named :formula_or_cask + named_args [:formula, :cask], number: 1 end end diff --git a/Library/Homebrew/dev-cmd/command.rb b/Library/Homebrew/dev-cmd/command.rb index eb946cf636..8b9ecc6e24 100644 --- a/Library/Homebrew/dev-cmd/command.rb +++ b/Library/Homebrew/dev-cmd/command.rb @@ -18,7 +18,7 @@ module Homebrew Display the path to the file being used when invoking `brew` . EOS - min_named 1 + named_args :command, min: 1 end end diff --git a/Library/Homebrew/dev-cmd/create.rb b/Library/Homebrew/dev-cmd/create.rb index 3815de0604..3e9fbc359d 100644 --- a/Library/Homebrew/dev-cmd/create.rb +++ b/Library/Homebrew/dev-cmd/create.rb @@ -69,7 +69,7 @@ module Homebrew conflicts "--cask", "--HEAD" conflicts "--cask", "--set-license" - named 1 + named_args number: 1 end end diff --git a/Library/Homebrew/dev-cmd/dispatch-build-bottle.rb b/Library/Homebrew/dev-cmd/dispatch-build-bottle.rb index 9732e9bce6..f160bd8696 100644 --- a/Library/Homebrew/dev-cmd/dispatch-build-bottle.rb +++ b/Library/Homebrew/dev-cmd/dispatch-build-bottle.rb @@ -28,7 +28,7 @@ module Homebrew switch "--upload", description: "Upload built bottles to Bintray." - min_named :formula + named_args :formula, min: 1 end end diff --git a/Library/Homebrew/dev-cmd/edit.rb b/Library/Homebrew/dev-cmd/edit.rb index 0894b3f00b..83505116dd 100644 --- a/Library/Homebrew/dev-cmd/edit.rb +++ b/Library/Homebrew/dev-cmd/edit.rb @@ -24,6 +24,8 @@ module Homebrew switch "--cask", "--casks", description: "Treat all named arguments as casks." conflicts "--formula", "--cask" + + named_args [:formula, :cask] end end diff --git a/Library/Homebrew/dev-cmd/extract.rb b/Library/Homebrew/dev-cmd/extract.rb index 63f518f69f..7cb5c238da 100644 --- a/Library/Homebrew/dev-cmd/extract.rb +++ b/Library/Homebrew/dev-cmd/extract.rb @@ -97,7 +97,7 @@ module Homebrew switch "-f", "--force", description: "Overwrite the destination formula if it already exists." - named 2 + named_args :formula, number: 2 end end diff --git a/Library/Homebrew/dev-cmd/formula.rb b/Library/Homebrew/dev-cmd/formula.rb index 17bfd4a806..39f960eb4b 100644 --- a/Library/Homebrew/dev-cmd/formula.rb +++ b/Library/Homebrew/dev-cmd/formula.rb @@ -18,7 +18,7 @@ module Homebrew Display the path where is located. EOS - min_named :formula + named_args :formula, min: 1 end end diff --git a/Library/Homebrew/dev-cmd/install-bundler-gems.rb b/Library/Homebrew/dev-cmd/install-bundler-gems.rb index d1c3bd88d6..419e5eee6c 100644 --- a/Library/Homebrew/dev-cmd/install-bundler-gems.rb +++ b/Library/Homebrew/dev-cmd/install-bundler-gems.rb @@ -18,7 +18,7 @@ module Homebrew Install Homebrew's Bundler gems. EOS - max_named 0 + named_args :none end end diff --git a/Library/Homebrew/dev-cmd/linkage.rb b/Library/Homebrew/dev-cmd/linkage.rb index 63ae8066a1..dcd7782403 100644 --- a/Library/Homebrew/dev-cmd/linkage.rb +++ b/Library/Homebrew/dev-cmd/linkage.rb @@ -28,6 +28,8 @@ module Homebrew switch "--cached", description: "Print the cached linkage values stored in `HOMEBREW_CACHE`, set by a previous "\ "`brew linkage` run." + + named_args :installed_formula end end diff --git a/Library/Homebrew/dev-cmd/livecheck.rb b/Library/Homebrew/dev-cmd/livecheck.rb index a074f51933..b27443b2cd 100644 --- a/Library/Homebrew/dev-cmd/livecheck.rb +++ b/Library/Homebrew/dev-cmd/livecheck.rb @@ -49,6 +49,8 @@ module Homebrew conflicts "--debug", "--json" conflicts "--tap=", "--all", "--installed" conflicts "--cask", "--formula" + + named_args [:formula, :cask] end end diff --git a/Library/Homebrew/dev-cmd/man.rb b/Library/Homebrew/dev-cmd/man.rb index 06372647ea..e0f6ce82f4 100644 --- a/Library/Homebrew/dev-cmd/man.rb +++ b/Library/Homebrew/dev-cmd/man.rb @@ -31,7 +31,7 @@ module Homebrew switch "--link", description: "This is now done automatically by `brew update`." - max_named 0 + named_args :none end end diff --git a/Library/Homebrew/dev-cmd/mirror.rb b/Library/Homebrew/dev-cmd/mirror.rb index ec6f44d8fc..435923c0cd 100644 --- a/Library/Homebrew/dev-cmd/mirror.rb +++ b/Library/Homebrew/dev-cmd/mirror.rb @@ -24,7 +24,7 @@ module Homebrew switch "--no-publish", description: "Upload to Bintray, but don't publish." - min_named :formula + named_args :formula, min: 1 end end diff --git a/Library/Homebrew/dev-cmd/pr-automerge.rb b/Library/Homebrew/dev-cmd/pr-automerge.rb index 9b4b5c41cb..0e582b168a 100644 --- a/Library/Homebrew/dev-cmd/pr-automerge.rb +++ b/Library/Homebrew/dev-cmd/pr-automerge.rb @@ -34,7 +34,7 @@ module Homebrew switch "--ignore-failures", description: "Include pull requests that have failing status checks." - max_named 0 + named_args :none end end diff --git a/Library/Homebrew/dev-cmd/pr-publish.rb b/Library/Homebrew/dev-cmd/pr-publish.rb index de376b1cee..7e38180d50 100644 --- a/Library/Homebrew/dev-cmd/pr-publish.rb +++ b/Library/Homebrew/dev-cmd/pr-publish.rb @@ -29,7 +29,7 @@ module Homebrew flag "--workflow=", description: "Target workflow filename (default: `publish-commit-bottles.yml`)." - min_named 1 + named_args number: 1 end end diff --git a/Library/Homebrew/dev-cmd/pr-pull.rb b/Library/Homebrew/dev-cmd/pr-pull.rb index d35caf73fa..ce569c7afd 100644 --- a/Library/Homebrew/dev-cmd/pr-pull.rb +++ b/Library/Homebrew/dev-cmd/pr-pull.rb @@ -70,7 +70,8 @@ module Homebrew description: "Comma-separated list of workflows which can be ignored if they have not been run." conflicts "--clean", "--autosquash" - min_named 1 + + named_args number: 1 end end diff --git a/Library/Homebrew/dev-cmd/pr-upload.rb b/Library/Homebrew/dev-cmd/pr-upload.rb index 1daa6cf19d..584696d4e4 100644 --- a/Library/Homebrew/dev-cmd/pr-upload.rb +++ b/Library/Homebrew/dev-cmd/pr-upload.rb @@ -33,6 +33,8 @@ module Homebrew description: "Upload to the specified Bintray organisation (default: `homebrew`)." flag "--root-url=", description: "Use the specified as the root of the bottle's URL instead of Homebrew's default." + + named_args :none end end diff --git a/Library/Homebrew/dev-cmd/prof.rb b/Library/Homebrew/dev-cmd/prof.rb index add6a5ae01..c57829fb95 100644 --- a/Library/Homebrew/dev-cmd/prof.rb +++ b/Library/Homebrew/dev-cmd/prof.rb @@ -18,6 +18,8 @@ module Homebrew EOS switch "--stackprof", description: "Use `stackprof` instead of `ruby-prof` (the default)." + + named_args :command end end diff --git a/Library/Homebrew/dev-cmd/release-notes.rb b/Library/Homebrew/dev-cmd/release-notes.rb index 004915f284..c09eb35031 100644 --- a/Library/Homebrew/dev-cmd/release-notes.rb +++ b/Library/Homebrew/dev-cmd/release-notes.rb @@ -25,7 +25,7 @@ module Homebrew switch "--markdown", description: "Print as a Markdown list." - max_named 2 + named_args max: 2 end end diff --git a/Library/Homebrew/dev-cmd/sh.rb b/Library/Homebrew/dev-cmd/sh.rb index ca5afd9df0..c8ee8ff7bb 100644 --- a/Library/Homebrew/dev-cmd/sh.rb +++ b/Library/Homebrew/dev-cmd/sh.rb @@ -27,7 +27,7 @@ module Homebrew flag "-c=", "--cmd=", description: "Execute commands in a non-interactive shell." - max_named 1 + named_args max: 1 end end diff --git a/Library/Homebrew/dev-cmd/sponsors.rb b/Library/Homebrew/dev-cmd/sponsors.rb index b6d03a839d..27bf7ba277 100644 --- a/Library/Homebrew/dev-cmd/sponsors.rb +++ b/Library/Homebrew/dev-cmd/sponsors.rb @@ -17,6 +17,8 @@ module Homebrew Print a Markdown summary of Homebrew's GitHub Sponsors, suitable for pasting into a README. EOS + + named_args :none end end diff --git a/Library/Homebrew/dev-cmd/style.rb b/Library/Homebrew/dev-cmd/style.rb index d43bd19f9a..df9e5bf302 100644 --- a/Library/Homebrew/dev-cmd/style.rb +++ b/Library/Homebrew/dev-cmd/style.rb @@ -42,6 +42,8 @@ module Homebrew conflicts "--formula", "--cask" conflicts "--only-cops", "--except-cops" + + named_args [:formula, :cask, :tap] end end diff --git a/Library/Homebrew/dev-cmd/tap-new.rb b/Library/Homebrew/dev-cmd/tap-new.rb index fb63340591..a1665c44d3 100644 --- a/Library/Homebrew/dev-cmd/tap-new.rb +++ b/Library/Homebrew/dev-cmd/tap-new.rb @@ -26,7 +26,8 @@ module Homebrew description: "Initialize Git repository with the specified branch name (default: `main`)." conflicts "--no-git", "--branch" - named 1 + + named_args :tap, number: 1 end end @@ -36,8 +37,7 @@ module Homebrew label = args.pull_label || "pr-pull" branch = args.branch || "main" - tap_name = args.named.first - tap = Tap.fetch(tap_name) + tap = args.named.to_taps.first raise "Invalid tap name '#{tap_name}'" unless tap.path.to_s.match?(HOMEBREW_TAP_PATH_REGEX) titleized_user = tap.user.dup diff --git a/Library/Homebrew/dev-cmd/test.rb b/Library/Homebrew/dev-cmd/test.rb index b45cd1dfab..0cd00db0f4 100644 --- a/Library/Homebrew/dev-cmd/test.rb +++ b/Library/Homebrew/dev-cmd/test.rb @@ -30,7 +30,7 @@ module Homebrew switch "--retry", description: "Retry if a testing fails." - min_named :formula + named_args :installed_formula, min: 1 end end diff --git a/Library/Homebrew/dev-cmd/tests.rb b/Library/Homebrew/dev-cmd/tests.rb index 1da41139bc..cf98431b93 100644 --- a/Library/Homebrew/dev-cmd/tests.rb +++ b/Library/Homebrew/dev-cmd/tests.rb @@ -34,7 +34,7 @@ module Homebrew flag "--seed=", description: "Randomise tests with the specified instead of a random seed." - max_named 0 + named_args :none end end diff --git a/Library/Homebrew/dev-cmd/typecheck.rb b/Library/Homebrew/dev-cmd/typecheck.rb index 7dd326dc03..ccbe230b78 100644 --- a/Library/Homebrew/dev-cmd/typecheck.rb +++ b/Library/Homebrew/dev-cmd/typecheck.rb @@ -37,7 +37,8 @@ module Homebrew "in their paths (relative to the input path passed to Sorbet)." conflicts "--dir", "--file" - max_named 0 + + named_args :none end end diff --git a/Library/Homebrew/dev-cmd/unbottled.rb b/Library/Homebrew/dev-cmd/unbottled.rb index f570b5e48a..31c1d7005b 100644 --- a/Library/Homebrew/dev-cmd/unbottled.rb +++ b/Library/Homebrew/dev-cmd/unbottled.rb @@ -24,6 +24,8 @@ module Homebrew switch "--total", description: "Output the number of unbottled and total formulae." conflicts "--dependents", "--total" + + named_args :formula end end diff --git a/Library/Homebrew/dev-cmd/unpack.rb b/Library/Homebrew/dev-cmd/unpack.rb index 529bfcc046..ccce3a938c 100644 --- a/Library/Homebrew/dev-cmd/unpack.rb +++ b/Library/Homebrew/dev-cmd/unpack.rb @@ -30,7 +30,8 @@ module Homebrew description: "Overwrite the destination directory if it already exists." conflicts "--git", "--patch" - min_named :formula + + named_args :formula, min: 1 end end diff --git a/Library/Homebrew/dev-cmd/update-license-data.rb b/Library/Homebrew/dev-cmd/update-license-data.rb index ef51979c7e..284ee6842c 100644 --- a/Library/Homebrew/dev-cmd/update-license-data.rb +++ b/Library/Homebrew/dev-cmd/update-license-data.rb @@ -21,7 +21,7 @@ module Homebrew description: "Return a failing status code if current license data's version is the same as " \ "the upstream. This can be used to notify CI when the SPDX license data is out of date." - max_named 0 + named_args :none end end diff --git a/Library/Homebrew/dev-cmd/update-python-resources.rb b/Library/Homebrew/dev-cmd/update-python-resources.rb index a66710f516..e9883c99ac 100644 --- a/Library/Homebrew/dev-cmd/update-python-resources.rb +++ b/Library/Homebrew/dev-cmd/update-python-resources.rb @@ -34,7 +34,7 @@ module Homebrew comma_array "--exclude-packages=", description: "Exclude these packages when finding resources." - min_named :formula + named_args :formula, number: 1 end end diff --git a/Library/Homebrew/dev-cmd/update-test.rb b/Library/Homebrew/dev-cmd/update-test.rb index 789ec21aa9..3659375d1e 100644 --- a/Library/Homebrew/dev-cmd/update-test.rb +++ b/Library/Homebrew/dev-cmd/update-test.rb @@ -26,7 +26,7 @@ module Homebrew flag "--before=", description: "Use the commit at the specified as the start commit." - max_named 0 + named_args :none end end diff --git a/Library/Homebrew/dev-cmd/vendor-gems.rb b/Library/Homebrew/dev-cmd/vendor-gems.rb index a9394c8905..1047daa96d 100644 --- a/Library/Homebrew/dev-cmd/vendor-gems.rb +++ b/Library/Homebrew/dev-cmd/vendor-gems.rb @@ -21,7 +21,7 @@ module Homebrew comma_array "--update", description: "Update all vendored Gems to the latest version." - max_named 0 + named_args :none end end diff --git a/Library/Homebrew/test/cask/cmd/shared_examples/requires_cask_token.rb b/Library/Homebrew/test/cask/cmd/shared_examples/requires_cask_token.rb index a3faaff589..c3eb1a0708 100644 --- a/Library/Homebrew/test/cask/cmd/shared_examples/requires_cask_token.rb +++ b/Library/Homebrew/test/cask/cmd/shared_examples/requires_cask_token.rb @@ -6,7 +6,7 @@ shared_examples "a command that requires a Cask token" do it "raises an exception " do expect { described_class.run - }.to raise_error(Cask::CaskUnspecifiedError, "This command requires a Cask token.") + }.to raise_error(UsageError, /this command requires a .*cask.* argument/) end end end diff --git a/Library/Homebrew/test/cli/named_args_spec.rb b/Library/Homebrew/test/cli/named_args_spec.rb index b8efc96cde..10fe73660c 100644 --- a/Library/Homebrew/test/cli/named_args_spec.rb +++ b/Library/Homebrew/test/cli/named_args_spec.rb @@ -162,4 +162,37 @@ describe Homebrew::CLI::NamedArgs do expect(described_class.new("foo", "baz").to_paths(only: :cask)).to eq [cask_path, Cask::CaskLoader.path("baz")] end end + + describe "#to_taps" do + it "returns taps" do + taps = described_class.new("homebrew/foo", "bar/baz") + expect(taps.to_taps.map(&:name)).to eq %w[homebrew/foo bar/baz] + end + + it "raises an error for invalid tap" do + taps = described_class.new("homebrew/foo", "barbaz") + expect { taps.to_taps }.to raise_error(RuntimeError, /Invalid tap name/) + end + end + + describe "#to_installed_taps" do + before do + (HOMEBREW_REPOSITORY/"Library/Taps/homebrew/homebrew-foo").mkpath + end + + it "returns installed taps" do + taps = described_class.new("homebrew/foo") + expect(taps.to_installed_taps.map(&:name)).to eq %w[homebrew/foo] + end + + it "raises an error for uninstalled tap" do + taps = described_class.new("homebrew/foo", "bar/baz") + expect { taps.to_installed_taps }.to raise_error(TapUnavailableError) + end + + it "raises an error for invalid tap" do + taps = described_class.new("homebrew/foo", "barbaz") + expect { taps.to_installed_taps }.to raise_error(RuntimeError, /Invalid tap name/) + end + end end