Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update External-Commands docs for new command abstraction #17139

Merged
merged 2 commits into from
Apr 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 25 additions & 23 deletions docs/External-Commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,36 +56,38 @@ 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`).

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 using `args`.

```ruby
# frozen_string_literal: true

module Homebrew
module_function

def foo_args
Homebrew::CLI::Parser.new do
description <<~EOS
Do something. Place a description here.
EOS
switch "-f", "--force",
description: "Force doing something in the command."
flag "--file=",
description: "Specify a file to do something with in the command."
comma_array "--names",
description: "Add a list of names to the command."

named_args [:formula, :cask], min: 1
module Cmd
class Foo < AbstractCommand
cmd_args do
description <<~EOS
Do something. Place a description here.
EOS
switch "-f", "--force",
description: "Force doing something in the command."
flag "--file=",
description: "Specify a file to do something with in the command."
comma_array "--names",
description: "Add a list of names to the command."

named_args [:formula, :cask], min: 1
end

def run
something if args.force?
something_else if args.file == "file.txt"
end
end
end

def foo
args = foo_args.parse

something if args.force?
something_else if args.file == "file.txt"
end
end
```

Expand Down