mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-02-13 22:44:57 -05:00
**What type of PR is this?**
Feature
**What does this PR do? Why is it needed?**
Introduces a `completion` subcommand to `beacon-chain` and `validator`
that outputs shell completion scripts. Supports Bash, Zsh, and Fish
shells.
```bash
# Load completions in current session
source <(beacon-chain completion bash)
# Persist for future sessions
beacon-chain completion zsh > "${fpath[1]}/_beacon-chain"
validator completion fish > ~/.config/fish/completions/validator.fish
```
Once loaded, users can press TAB to complete subcommands, nested
commands, and flags. Flag completion supports prefix matching (e.g.,
typing `--exec<TAB>` suggests `--execution-endpoint`,
`--execution-headers`).
**Which issues(s) does this PR fix?**
Fixes #16244
**Other notes for review**
The implementation adds three files to the existing `cmd` package:
- `completion.go` - Defines `CompletionCommand()` returning a
`*cli.Command` with `bash`, `zsh`, `fish` subcommands
- `completion_scripts.go` - Contains the shell script templates
- `completion_test.go` - Unit tests for command structure and script
content
Changes to `beacon-chain` and `validator`:
- Import `cmd.CompletionCommand("binary-name")` in the Commands slice
- Set `EnableBashCompletion: true` on the cli.App to activate
urfave/cli's `--generate-bash-completion` hidden flag
The shell scripts call the binary with `--generate-bash-completion`
appended to get context-aware suggestions. This means completions
automatically reflect the current binary's flags and commands.
**Acknowledgements**
- [x] I have read
[CONTRIBUTING.md](https://github.com/prysmaticlabs/prysm/blob/develop/CONTRIBUTING.md).
- [x] I have included a uniquely named [changelog fragment
file](https://github.com/prysmaticlabs/prysm/blob/develop/CONTRIBUTING.md#maintaining-changelogmd).
- [x] I have added a description with sufficient context for reviewers
to understand this PR.
- [x] I have tested that my changes work as expected and I added a
testing plan to the PR description (if applicable).
Signed-off-by: Willian Paixao <willian@ufpa.br>
64 lines
1.6 KiB
Go
64 lines
1.6 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
)
|
|
|
|
// CompletionCommand returns the completion command for the given binary name.
|
|
// The binaryName parameter should be "beacon-chain" or "validator".
|
|
func CompletionCommand(binaryName string) *cli.Command {
|
|
return &cli.Command{
|
|
Name: "completion",
|
|
Category: "completion",
|
|
Usage: "Generate shell completion scripts",
|
|
Description: fmt.Sprintf(`Generate shell completion scripts for bash, zsh, or fish.
|
|
|
|
To load completions:
|
|
|
|
Bash:
|
|
$ source <(%[1]s completion bash)
|
|
# To load completions for each session, execute once:
|
|
$ %[1]s completion bash > /etc/bash_completion.d/%[1]s
|
|
|
|
Zsh:
|
|
# To load completions for each session, execute once:
|
|
$ %[1]s completion zsh > "${fpath[1]}/_%[1]s"
|
|
|
|
# You may need to start a new shell for completions to take effect.
|
|
|
|
Fish:
|
|
$ %[1]s completion fish | source
|
|
# To load completions for each session, execute once:
|
|
$ %[1]s completion fish > ~/.config/fish/completions/%[1]s.fish
|
|
`, binaryName),
|
|
Subcommands: []*cli.Command{
|
|
{
|
|
Name: "bash",
|
|
Usage: "Generate bash completion script",
|
|
Action: func(_ *cli.Context) error {
|
|
fmt.Println(bashCompletionScript(binaryName))
|
|
return nil
|
|
},
|
|
},
|
|
{
|
|
Name: "zsh",
|
|
Usage: "Generate zsh completion script",
|
|
Action: func(_ *cli.Context) error {
|
|
fmt.Println(zshCompletionScript(binaryName))
|
|
return nil
|
|
},
|
|
},
|
|
{
|
|
Name: "fish",
|
|
Usage: "Generate fish completion script",
|
|
Action: func(_ *cli.Context) error {
|
|
fmt.Println(fishCompletionScript(binaryName))
|
|
return nil
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|