mirror of
https://github.com/Homebrew/brew.git
synced 2025-07-14 16:09:03 +08:00
Minor CLI code improvements
This commit is contained in:
parent
405cedad23
commit
06aaeafa4f
@ -161,22 +161,18 @@ module Homebrew
|
|||||||
|
|
||||||
sig { returns(T::Array[String]) }
|
sig { returns(T::Array[String]) }
|
||||||
def cli_args
|
def cli_args
|
||||||
return @cli_args if @cli_args
|
@cli_args ||= @processed_options.filter_map do |short, long|
|
||||||
|
|
||||||
@cli_args = []
|
|
||||||
@processed_options.each do |short, long|
|
|
||||||
option = long || short
|
option = long || short
|
||||||
switch = :"#{option_to_name(option)}?"
|
switch = :"#{option_to_name(option)}?"
|
||||||
flag = option_to_name(option).to_sym
|
flag = option_to_name(option).to_sym
|
||||||
if @table[switch] == true || @table[flag] == true
|
if @table[switch] == true || @table[flag] == true
|
||||||
@cli_args << option
|
option
|
||||||
elsif @table[flag].instance_of? String
|
elsif @table[flag].instance_of? String
|
||||||
@cli_args << "#{option}=#{@table[flag]}"
|
"#{option}=#{@table[flag]}"
|
||||||
elsif @table[flag].instance_of? Array
|
elsif @table[flag].instance_of? Array
|
||||||
@cli_args << "#{option}=#{@table[flag].join(",")}"
|
"#{option}=#{@table[flag].join(",")}"
|
||||||
end
|
end
|
||||||
end
|
end.freeze
|
||||||
@cli_args.freeze
|
|
||||||
end
|
end
|
||||||
|
|
||||||
sig { params(method_name: Symbol, _include_private: T::Boolean).returns(T::Boolean) }
|
sig { params(method_name: Symbol, _include_private: T::Boolean).returns(T::Boolean) }
|
||||||
|
73
Library/Homebrew/cli/error.rb
Normal file
73
Library/Homebrew/cli/error.rb
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
# typed: strict
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require "utils/formatter"
|
||||||
|
|
||||||
|
module Homebrew
|
||||||
|
module CLI
|
||||||
|
class OptionConstraintError < UsageError
|
||||||
|
sig { params(arg1: String, arg2: String, missing: T::Boolean).void }
|
||||||
|
def initialize(arg1, arg2, missing: false)
|
||||||
|
message = if missing
|
||||||
|
"`#{arg2}` cannot be passed without `#{arg1}`."
|
||||||
|
else
|
||||||
|
"`#{arg1}` and `#{arg2}` should be passed together."
|
||||||
|
end
|
||||||
|
super message
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class OptionConflictError < UsageError
|
||||||
|
sig { params(args: T::Array[String]).void }
|
||||||
|
def initialize(args)
|
||||||
|
args_list = args.map { Formatter.option(_1) }.join(" and ")
|
||||||
|
super "Options #{args_list} are mutually exclusive."
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class InvalidConstraintError < UsageError
|
||||||
|
sig { params(arg1: String, arg2: String).void }
|
||||||
|
def initialize(arg1, arg2)
|
||||||
|
super "`#{arg1}` and `#{arg2}` cannot be mutually exclusive and mutually dependent simultaneously."
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class MaxNamedArgumentsError < UsageError
|
||||||
|
sig { params(maximum: Integer, types: T::Array[Symbol]).void }
|
||||||
|
def initialize(maximum, types: [])
|
||||||
|
super case maximum
|
||||||
|
when 0
|
||||||
|
"This command does not take named arguments."
|
||||||
|
else
|
||||||
|
types << :named if types.empty?
|
||||||
|
arg_types = types.map { |type| type.to_s.tr("_", " ") }
|
||||||
|
.to_sentence two_words_connector: " or ", last_word_connector: " or "
|
||||||
|
|
||||||
|
"This command does not take more than #{maximum} #{arg_types} #{Utils.pluralize("argument", maximum)}."
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class MinNamedArgumentsError < UsageError
|
||||||
|
sig { params(minimum: Integer, types: T::Array[Symbol]).void }
|
||||||
|
def initialize(minimum, types: [])
|
||||||
|
types << :named if types.empty?
|
||||||
|
arg_types = types.map { |type| type.to_s.tr("_", " ") }
|
||||||
|
.to_sentence two_words_connector: " or ", last_word_connector: " or "
|
||||||
|
|
||||||
|
super "This command requires at least #{minimum} #{arg_types} #{Utils.pluralize("argument", minimum)}."
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class NumberOfNamedArgumentsError < UsageError
|
||||||
|
sig { params(minimum: Integer, types: T::Array[Symbol]).void }
|
||||||
|
def initialize(minimum, types: [])
|
||||||
|
types << :named if types.empty?
|
||||||
|
arg_types = types.map { |type| type.to_s.tr("_", " ") }
|
||||||
|
.to_sentence two_words_connector: " or ", last_word_connector: " or "
|
||||||
|
|
||||||
|
super "This command requires exactly #{minimum} #{arg_types} #{Utils.pluralize("argument", minimum)}."
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -5,6 +5,7 @@ require "abstract_command"
|
|||||||
require "env_config"
|
require "env_config"
|
||||||
require "cask/config"
|
require "cask/config"
|
||||||
require "cli/args"
|
require "cli/args"
|
||||||
|
require "cli/error"
|
||||||
require "commands"
|
require "commands"
|
||||||
require "optparse"
|
require "optparse"
|
||||||
require "utils/tty"
|
require "utils/tty"
|
||||||
@ -734,71 +735,6 @@ module Homebrew
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class OptionConstraintError < UsageError
|
|
||||||
sig { params(arg1: String, arg2: String, missing: T::Boolean).void }
|
|
||||||
def initialize(arg1, arg2, missing: false)
|
|
||||||
message = if missing
|
|
||||||
"`#{arg2}` cannot be passed without `#{arg1}`."
|
|
||||||
else
|
|
||||||
"`#{arg1}` and `#{arg2}` should be passed together."
|
|
||||||
end
|
|
||||||
super message
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class OptionConflictError < UsageError
|
|
||||||
sig { params(args: T::Array[String]).void }
|
|
||||||
def initialize(args)
|
|
||||||
args_list = args.map { Formatter.option(_1) }.join(" and ")
|
|
||||||
super "Options #{args_list} are mutually exclusive."
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class InvalidConstraintError < UsageError
|
|
||||||
sig { params(arg1: String, arg2: String).void }
|
|
||||||
def initialize(arg1, arg2)
|
|
||||||
super "`#{arg1}` and `#{arg2}` cannot be mutually exclusive and mutually dependent simultaneously."
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class MaxNamedArgumentsError < UsageError
|
|
||||||
sig { params(maximum: Integer, types: T::Array[Symbol]).void }
|
|
||||||
def initialize(maximum, types: [])
|
|
||||||
super case maximum
|
|
||||||
when 0
|
|
||||||
"This command does not take named arguments."
|
|
||||||
else
|
|
||||||
types << :named if types.empty?
|
|
||||||
arg_types = types.map { |type| type.to_s.tr("_", " ") }
|
|
||||||
.to_sentence two_words_connector: " or ", last_word_connector: " or "
|
|
||||||
|
|
||||||
"This command does not take more than #{maximum} #{arg_types} #{Utils.pluralize("argument", maximum)}."
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class MinNamedArgumentsError < UsageError
|
|
||||||
sig { params(minimum: Integer, types: T::Array[Symbol]).void }
|
|
||||||
def initialize(minimum, types: [])
|
|
||||||
types << :named if types.empty?
|
|
||||||
arg_types = types.map { |type| type.to_s.tr("_", " ") }
|
|
||||||
.to_sentence two_words_connector: " or ", last_word_connector: " or "
|
|
||||||
|
|
||||||
super "This command requires at least #{minimum} #{arg_types} #{Utils.pluralize("argument", minimum)}."
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class NumberOfNamedArgumentsError < UsageError
|
|
||||||
sig { params(minimum: Integer, types: T::Array[Symbol]).void }
|
|
||||||
def initialize(minimum, types: [])
|
|
||||||
types << :named if types.empty?
|
|
||||||
arg_types = types.map { |type| type.to_s.tr("_", " ") }
|
|
||||||
.to_sentence two_words_connector: " or ", last_word_connector: " or "
|
|
||||||
|
|
||||||
super "This command requires exactly #{minimum} #{arg_types} #{Utils.pluralize("argument", minimum)}."
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user