mirror of
https://github.com/Homebrew/brew.git
synced 2025-07-14 16:09:03 +08:00
Implement cmd_args block
This commit is contained in:
parent
2dceb65b42
commit
fd652148fa
@ -4,36 +4,49 @@
|
|||||||
require "command_registry"
|
require "command_registry"
|
||||||
|
|
||||||
module Homebrew
|
module Homebrew
|
||||||
|
# Subclass this to implement a `brew` command. This is preferred to declaring a named function in the `Homebrew`
|
||||||
|
# module, because:
|
||||||
|
# - Each Command lives in an isolated namespace.
|
||||||
|
# - Each Command implements a defined interface.
|
||||||
|
#
|
||||||
|
# To subclass, implement a `run` method and provide a `cmd_args` block to document the command and its allowed args.
|
||||||
class AbstractCommand
|
class AbstractCommand
|
||||||
extend T::Helpers
|
extend T::Helpers
|
||||||
|
|
||||||
abstract!
|
abstract!
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
|
sig { returns(T.nilable(T.proc.void)) }
|
||||||
|
attr_reader :parser_block
|
||||||
|
|
||||||
|
sig { returns(String) }
|
||||||
|
def command_name = T.must(name).split("::").fetch(-1).downcase
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
sig { params(block: T.nilable(T.proc.bind(CLI::Parser).void)).void }
|
||||||
|
def cmd_args(&block)
|
||||||
|
@parser_block = T.let(block, T.nilable(T.proc.void))
|
||||||
|
end
|
||||||
|
|
||||||
# registers subclasses for lookup by command name
|
# registers subclasses for lookup by command name
|
||||||
sig { params(subclass: T.class_of(AbstractCommand)).void }
|
sig { params(subclass: T.class_of(AbstractCommand)).void }
|
||||||
def inherited(subclass)
|
def inherited(subclass)
|
||||||
super
|
super
|
||||||
CommandRegistry.register(subclass)
|
CommandRegistry.register(subclass)
|
||||||
end
|
end
|
||||||
|
|
||||||
sig { returns(String) }
|
|
||||||
def command_name = T.must(name).split("::").fetch(-1).downcase
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# @note because `Args` makes use `OpenStruct`, subclasses may need to use a tapioca compiler,
|
# @note because `Args` makes use `OpenStruct`, subclasses may need to use a tapioca compiler,
|
||||||
# hash accessors, args.rbi, or other means to make this work with legacy commands:
|
# hash accessors, args.rbi, or other means to make this work with legacy commands:
|
||||||
sig { returns(Homebrew::CLI::Args) }
|
sig { returns(CLI::Args) }
|
||||||
attr_reader :args
|
attr_reader :args
|
||||||
|
|
||||||
sig { void }
|
sig { void }
|
||||||
def initialize
|
def initialize
|
||||||
@args = T.let(raw_args.parse, Homebrew::CLI::Args)
|
@args = T.let(CLI::Parser.new(&self.class.parser_block).parse, CLI::Args)
|
||||||
end
|
end
|
||||||
|
|
||||||
sig { abstract.returns(CLI::Parser) }
|
|
||||||
def raw_args; end
|
|
||||||
|
|
||||||
sig { abstract.void }
|
sig { abstract.void }
|
||||||
def run; end
|
def run; end
|
||||||
end
|
end
|
||||||
|
@ -13,9 +13,7 @@ module Homebrew
|
|||||||
class List < AbstractCommand
|
class List < AbstractCommand
|
||||||
include SystemCommand::Mixin
|
include SystemCommand::Mixin
|
||||||
|
|
||||||
sig { override.returns(CLI::Parser) }
|
cmd_args do
|
||||||
def raw_args
|
|
||||||
Homebrew::CLI::Parser.new do
|
|
||||||
description <<~EOS
|
description <<~EOS
|
||||||
List all installed formulae and casks.
|
List all installed formulae and casks.
|
||||||
If <formula> is provided, summarise the paths within its current keg.
|
If <formula> is provided, summarise the paths within its current keg.
|
||||||
@ -46,8 +44,8 @@ module Homebrew
|
|||||||
description: "List formulae and/or casks in long format. " \
|
description: "List formulae and/or casks in long format. " \
|
||||||
"Has no effect when a formula or cask name is passed as an argument."
|
"Has no effect when a formula or cask name is passed as an argument."
|
||||||
switch "-r",
|
switch "-r",
|
||||||
description: "Reverse the order of the formulae and/or casks sort to list the oldest entries " \
|
description: "Reverse the order of the formulae and/or casks sort to list the oldest entries first. " \
|
||||||
"first. Has no effect when a formula or cask name is passed as an argument."
|
"Has no effect when a formula or cask name is passed as an argument."
|
||||||
switch "-t",
|
switch "-t",
|
||||||
description: "Sort formulae and/or casks by time modified, listing most recently modified first. " \
|
description: "Sort formulae and/or casks by time modified, listing most recently modified first. " \
|
||||||
"Has no effect when a formula or cask name is passed as an argument."
|
"Has no effect when a formula or cask name is passed as an argument."
|
||||||
@ -66,7 +64,6 @@ module Homebrew
|
|||||||
|
|
||||||
named_args [:installed_formula, :installed_cask]
|
named_args [:installed_formula, :installed_cask]
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
sig { override.void }
|
sig { override.void }
|
||||||
def run
|
def run
|
||||||
|
@ -7,9 +7,7 @@ require "cli/parser"
|
|||||||
module Homebrew
|
module Homebrew
|
||||||
module DevCmd
|
module DevCmd
|
||||||
class Prof < AbstractCommand
|
class Prof < AbstractCommand
|
||||||
sig { override.returns(CLI::Parser) }
|
cmd_args do
|
||||||
def raw_args
|
|
||||||
Homebrew::CLI::Parser.new do
|
|
||||||
description <<~EOS
|
description <<~EOS
|
||||||
Run Homebrew with a Ruby profiler. For example, `brew prof readall`.
|
Run Homebrew with a Ruby profiler. For example, `brew prof readall`.
|
||||||
EOS
|
EOS
|
||||||
@ -18,7 +16,6 @@ module Homebrew
|
|||||||
|
|
||||||
named_args :command, min: 1
|
named_args :command, min: 1
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
sig { override.void }
|
sig { override.void }
|
||||||
def run
|
def run
|
||||||
|
Loading…
x
Reference in New Issue
Block a user