Update External-Commands docs for new command abstraction

This commit is contained in:
Douglas Eichelberger 2024-04-23 14:46:01 -07:00
parent 9682681a78
commit 5772ba12b4

View File

@ -56,16 +56,19 @@ External commands can be hosted in a [tap](Taps.md) to allow users to easily ins
External commands should be added to a `cmd` directory in the tap. An external command `extcmd` implemented as a Ruby command should live in `cmd/extcmd.rb` (don't forget to `chmod +x`). External commands should be added to a `cmd` directory in the tap. An external command `extcmd` implemented as a Ruby command should live in `cmd/extcmd.rb` (don't forget to `chmod +x`).
To easily use Homebrew's argument parser, replicate the following Ruby template for external commands (replacing all instances of `foo` with the name of the command): To easily use Homebrew's argument parser, replicate the Ruby template below for external commands. Your implementation must include the following:
- The class name should be the command name in CamelCase (e.g. `my-cmd` should be named `MyCmd`).
- Provide a `cmd_args` block that describes the command and its arguments.
- Implement the `run` method, which will be invoked when the command is executed. Within the `run` method, the parsed arguments are available asusing `args`.
```ruby ```ruby
# frozen_string_literal: true # frozen_string_literal: true
module Homebrew module Homebrew
module_function module Cmd
class Foo < AbstractCommand
def foo_args cmd_args do
Homebrew::CLI::Parser.new do
description <<~EOS description <<~EOS
Do something. Place a description here. Do something. Place a description here.
EOS EOS
@ -78,14 +81,13 @@ module Homebrew
named_args [:formula, :cask], min: 1 named_args [:formula, :cask], min: 1
end end
end
def foo
args = foo_args.parse
def run
something if args.force? something if args.force?
something_else if args.file == "file.txt" something_else if args.file == "file.txt"
end end
end
end
end end
``` ```