Implement cmd_args block

This commit is contained in:
Douglas Eichelberger 2024-03-05 10:40:02 -08:00
parent 2dceb65b42
commit fd652148fa
3 changed files with 76 additions and 69 deletions

View File

@ -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

View File

@ -13,59 +13,56 @@ 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 description <<~EOS
Homebrew::CLI::Parser.new do List all installed formulae and casks.
description <<~EOS If <formula> is provided, summarise the paths within its current keg.
List all installed formulae and casks. If <cask> is provided, list its artifacts.
If <formula> is provided, summarise the paths within its current keg. EOS
If <cask> is provided, list its artifacts. switch "--formula", "--formulae",
EOS description: "List only formulae, or treat all named arguments as formulae."
switch "--formula", "--formulae", switch "--cask", "--casks",
description: "List only formulae, or treat all named arguments as formulae." description: "List only casks, or treat all named arguments as casks."
switch "--cask", "--casks", switch "--full-name",
description: "List only casks, or treat all named arguments as casks." description: "Print formulae with fully-qualified names. Unless `--full-name`, `--versions` " \
switch "--full-name", "or `--pinned` are passed, other options (i.e. `-1`, `-l`, `-r` and `-t`) are " \
description: "Print formulae with fully-qualified names. Unless `--full-name`, `--versions` " \ "passed to `ls`(1) which produces the actual output."
"or `--pinned` are passed, other options (i.e. `-1`, `-l`, `-r` and `-t`) are " \ switch "--versions",
"passed to `ls`(1) which produces the actual output." description: "Show the version number for installed formulae, or only the specified " \
switch "--versions", "formulae if <formula> are provided."
description: "Show the version number for installed formulae, or only the specified " \ switch "--multiple",
"formulae if <formula> are provided." depends_on: "--versions",
switch "--multiple", description: "Only show formulae with multiple versions installed."
depends_on: "--versions", switch "--pinned",
description: "Only show formulae with multiple versions installed." description: "List only pinned formulae, or only the specified (pinned) " \
switch "--pinned", "formulae if <formula> are provided. See also `pin`, `unpin`."
description: "List only pinned formulae, or only the specified (pinned) " \ # passed through to ls
"formulae if <formula> are provided. See also `pin`, `unpin`." switch "-1",
# passed through to ls description: "Force output to be one entry per line. " \
switch "-1", "This is the default when output is not to a terminal."
description: "Force output to be one entry per line. " \ switch "-l",
"This is the default when output is not to a terminal." description: "List formulae and/or casks in long format. " \
switch "-l", "Has no effect when a formula or cask name is passed as an argument."
description: "List formulae and/or casks in long format. " \ switch "-r",
"Has no effect when a formula or cask name is passed as an argument." description: "Reverse the order of the formulae and/or casks sort to list the oldest entries first. " \
switch "-r", "Has no effect when a formula or cask name is passed as an argument."
description: "Reverse the order of the formulae and/or casks sort to list the oldest entries " \ switch "-t",
"first. Has no effect when a formula or cask name is passed as an argument." description: "Sort formulae and/or casks by time modified, listing most recently modified first. " \
switch "-t", "Has no effect when a formula or cask name is passed as an argument."
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."
conflicts "--formula", "--cask" conflicts "--formula", "--cask"
conflicts "--pinned", "--cask" conflicts "--pinned", "--cask"
conflicts "--multiple", "--cask" conflicts "--multiple", "--cask"
conflicts "--pinned", "--multiple" conflicts "--pinned", "--multiple"
["-1", "-l", "-r", "-t"].each do |flag| ["-1", "-l", "-r", "-t"].each do |flag|
conflicts "--versions", flag conflicts "--versions", flag
conflicts "--pinned", flag conflicts "--pinned", flag
end
["--versions", "--pinned", "-l", "-r", "-t"].each do |flag|
conflicts "--full-name", flag
end
named_args [:installed_formula, :installed_cask]
end end
["--versions", "--pinned", "-l", "-r", "-t"].each do |flag|
conflicts "--full-name", flag
end
named_args [:installed_formula, :installed_cask]
end end
sig { override.void } sig { override.void }

View File

@ -7,17 +7,14 @@ 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 description <<~EOS
Homebrew::CLI::Parser.new do Run Homebrew with a Ruby profiler. For example, `brew prof readall`.
description <<~EOS EOS
Run Homebrew with a Ruby profiler. For example, `brew prof readall`. switch "--stackprof",
EOS description: "Use `stackprof` instead of `ruby-prof` (the default)."
switch "--stackprof",
description: "Use `stackprof` instead of `ruby-prof` (the default)."
named_args :command, min: 1 named_args :command, min: 1
end
end end
sig { override.void } sig { override.void }