Skip to content

Commit

Permalink
chore(cmd): Add consts for shells
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Jul 12, 2024
1 parent 6441555 commit 8828f0f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func New() *cobra.Command {

cmd.Flags().StringP(config.FileFlag, "f", "", "Loads a pattern file on startup")
cmd.Flags().String(config.FileFormatFlag, "auto", "File format (one of: "+strings.Join(pattern.FormatStrings(), ", ")+")")
cmd.Flags().String(config.CompletionFlag, "", "Output command-line completion code for the specified shell. Can be 'bash', 'zsh', 'fish', or 'powershell'.")
cmd.Flags().String(config.CompletionFlag, "", "Output command-line completion code for the specified shell (one of: "+strings.Join(shells(), ", ")+")")

if err := errors.Join(
cmd.RegisterFlagCompletionFunc(config.FileFlag,
Expand Down
19 changes: 15 additions & 4 deletions cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,28 @@ import (
"github.com/spf13/cobra"
)

const (
ShellBash = "bash"
ShellZsh = "zsh"
ShellFish = "fish"
ShellPowerShell = "powershell"
)

func shells() []string {
return []string{ShellBash, ShellZsh, ShellFish, ShellPowerShell}
}

var ErrInvalidShell = errors.New("invalid shell")

func completion(cmd *cobra.Command, shell string) error {
switch shell {
case "bash":
case ShellBash:
return cmd.Root().GenBashCompletion(cmd.OutOrStdout())
case "zsh":
case ShellZsh:
return cmd.Root().GenZshCompletion(cmd.OutOrStdout())
case "fish":
case ShellFish:
return cmd.Root().GenFishCompletion(cmd.OutOrStdout(), true)
case "powershell":
case ShellPowerShell:
return cmd.Root().GenPowerShellCompletionWithDesc(cmd.OutOrStdout())
default:
return fmt.Errorf("%w: %s", ErrInvalidShell, shell)
Expand Down
2 changes: 1 addition & 1 deletion docs/cli-of-life.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ cli-of-life [flags]
### Options

```
--completion string Output command-line completion code for the specified shell. Can be 'bash', 'zsh', 'fish', or 'powershell'.
--completion string Output command-line completion code for the specified shell (one of: bash, zsh, fish, powershell)
-f, --file string Loads a pattern file on startup
--file-format string File format (one of: auto, rle, plaintext) (default "auto")
-h, --help help for cli-of-life
Expand Down

0 comments on commit 8828f0f

Please sign in to comment.