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

Zsh Completion #646

Merged
merged 28 commits into from
Jun 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
3100f0b
First try at better zsh completions:
babysnakes Feb 24, 2018
892ebc0
zsh-completion fixed reference to cmd name
babysnakes Feb 25, 2018
7091b88
zsh-completion ignores hidden commands and flags :)
babysnakes Feb 25, 2018
c978c0b
Merge remote-tracking branch 'spf13/master' into zsh-completion
babysnakes Feb 25, 2018
991feef
zsh-completion: added support for subcommand description.
babysnakes Feb 26, 2018
91a14a0
zsh-completion template refactoring:
babysnakes Feb 28, 2018
f7c1ed9
zsh-completions: revised flags completion rendering + new features:
babysnakes Feb 28, 2018
c99a50b
Merge upstream into zsh-completion
babysnakes Feb 28, 2018
be021cb
zsh-completion: removed the _dep_ files.
babysnakes Feb 28, 2018
9df4b2f
zsh-completion: two fixes for identifying flag usage:
babysnakes Feb 28, 2018
d925cc6
zsh-completion: removed forgotten function.
babysnakes Mar 1, 2018
43f884f
zsh-completion: added zsh-completion documentation.
babysnakes Mar 1, 2018
11cf991
zsh-completions: fixed zsh completion markdown file name.
babysnakes Mar 1, 2018
6ada43c
zsh-completion: added support for filename globbing.
babysnakes Mar 2, 2018
7dc860d
Merge remote-tracking branch 'spf13/master' into zsh-completion
babysnakes Mar 3, 2018
d5412f0
zsh-completion: remove temporary file
babysnakes Mar 3, 2018
ee1d5ce
zsh-completion: remove bad test
babysnakes Mar 3, 2018
70f3e35
zsh-completion: completion should always parse the root command!
babysnakes Mar 4, 2018
f57cec3
zsh-completion: --version and --help still doesn't work correctly
babysnakes Mar 4, 2018
70fa250
zsh-completion: test to verify that we're always running on root cmd.
babysnakes Mar 4, 2018
d57d049
zsh-completion: added escapinng of single quotes in flag description.
babysnakes Mar 17, 2018
00618a4
zsh-completion: tidy up function and variable names
babysnakes Mar 22, 2018
4c6a1bc
Merge remote-tracking branch 'spf13/master' into zsh-completion
babysnakes Mar 22, 2018
484eff9
zsh-completions: implemented argument completion.
babysnakes Mar 23, 2018
1fc860f
typo in zsh-completions.md
babysnakes Mar 23, 2018
a223d1b
Merge remote-tracking branch 'spf13/master' into zsh-completion
babysnakes Apr 19, 2018
13b42c0
Merge remote-tracking branch 'spf13/master' into zsh-completion
babysnakes Jun 2, 2018
61ca3af
Started working on Unified API for the various shell completions:
babysnakes Jun 3, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Many of the most widely used Go projects are built using Cobra including:
* [Suggestions when "unknown command" happens](#suggestions-when-unknown-command-happens)
* [Generating documentation for your command](#generating-documentation-for-your-command)
* [Generating bash completions](#generating-bash-completions)
* [Generating zsh completions](#generating-zsh-completions)
- [Contributing](#contributing)
- [License](#license)

Expand Down Expand Up @@ -721,6 +722,11 @@ Cobra can generate documentation based on subcommands, flags, etc. in the follow

Cobra can generate a bash-completion file. If you add more information to your command, these completions can be amazingly powerful and flexible. Read more about it in [Bash Completions](bash_completions.md).

## Generating zsh completions

Cobra can generate zsh-completion file. Read more about it in
[Zsh Completions](zsh_completions.md).

# Contributing

1. Fork it
Expand Down
48 changes: 0 additions & 48 deletions bash_completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,51 +534,3 @@ func (c *Command) GenBashCompletionFile(filename string) error {

return c.GenBashCompletion(outFile)
}

// MarkFlagRequired adds the BashCompOneRequiredFlag annotation to the named flag if it exists,
// and causes your command to report an error if invoked without the flag.
func (c *Command) MarkFlagRequired(name string) error {
return MarkFlagRequired(c.Flags(), name)
}

// MarkPersistentFlagRequired adds the BashCompOneRequiredFlag annotation to the named persistent flag if it exists,
// and causes your command to report an error if invoked without the flag.
func (c *Command) MarkPersistentFlagRequired(name string) error {
return MarkFlagRequired(c.PersistentFlags(), name)
}

// MarkFlagRequired adds the BashCompOneRequiredFlag annotation to the named flag if it exists,
// and causes your command to report an error if invoked without the flag.
func MarkFlagRequired(flags *pflag.FlagSet, name string) error {
return flags.SetAnnotation(name, BashCompOneRequiredFlag, []string{"true"})
}

// MarkFlagFilename adds the BashCompFilenameExt annotation to the named flag, if it exists.
// Generated bash autocompletion will select filenames for the flag, limiting to named extensions if provided.
func (c *Command) MarkFlagFilename(name string, extensions ...string) error {
return MarkFlagFilename(c.Flags(), name, extensions...)
}

// MarkFlagCustom adds the BashCompCustom annotation to the named flag, if it exists.
// Generated bash autocompletion will call the bash function f for the flag.
func (c *Command) MarkFlagCustom(name string, f string) error {
return MarkFlagCustom(c.Flags(), name, f)
}

// MarkPersistentFlagFilename adds the BashCompFilenameExt annotation to the named persistent flag, if it exists.
// Generated bash autocompletion will select filenames for the flag, limiting to named extensions if provided.
func (c *Command) MarkPersistentFlagFilename(name string, extensions ...string) error {
return MarkFlagFilename(c.PersistentFlags(), name, extensions...)
}

// MarkFlagFilename adds the BashCompFilenameExt annotation to the named flag in the flag set, if it exists.
// Generated bash autocompletion will select filenames for the flag, limiting to named extensions if provided.
func MarkFlagFilename(flags *pflag.FlagSet, name string, extensions ...string) error {
return flags.SetAnnotation(name, BashCompFilenameExt, extensions)
}

// MarkFlagCustom adds the BashCompCustom annotation to the named flag in the flag set, if it exists.
// Generated bash autocompletion will call the bash function f for the flag.
func MarkFlagCustom(flags *pflag.FlagSet, name string, f string) error {
return flags.SetAnnotation(name, BashCompCustom, []string{f})
}
85 changes: 85 additions & 0 deletions shell_completions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package cobra

import (
"github.com/spf13/pflag"
)

// MarkFlagRequired adds the BashCompOneRequiredFlag annotation to the named flag if it exists,
// and causes your command to report an error if invoked without the flag.
func (c *Command) MarkFlagRequired(name string) error {
return MarkFlagRequired(c.Flags(), name)
}

// MarkPersistentFlagRequired adds the BashCompOneRequiredFlag annotation to the named persistent flag if it exists,
// and causes your command to report an error if invoked without the flag.
func (c *Command) MarkPersistentFlagRequired(name string) error {
return MarkFlagRequired(c.PersistentFlags(), name)
}

// MarkFlagRequired adds the BashCompOneRequiredFlag annotation to the named flag if it exists,
// and causes your command to report an error if invoked without the flag.
func MarkFlagRequired(flags *pflag.FlagSet, name string) error {
return flags.SetAnnotation(name, BashCompOneRequiredFlag, []string{"true"})
}

// MarkFlagFilename adds the BashCompFilenameExt annotation to the named flag, if it exists.
// Generated bash autocompletion will select filenames for the flag, limiting to named extensions if provided.
func (c *Command) MarkFlagFilename(name string, extensions ...string) error {
return MarkFlagFilename(c.Flags(), name, extensions...)
}

// MarkFlagCustom adds the BashCompCustom annotation to the named flag, if it exists.
// Generated bash autocompletion will call the bash function f for the flag.
func (c *Command) MarkFlagCustom(name string, f string) error {
return MarkFlagCustom(c.Flags(), name, f)
}

// MarkPersistentFlagFilename instructs the various shell completion
// implementations to limit completions for this persistent flag to the
// specified extensions (patterns).
//
// Shell Completion compatibility matrix: bash, zsh
func (c *Command) MarkPersistentFlagFilename(name string, extensions ...string) error {
return MarkFlagFilename(c.PersistentFlags(), name, extensions...)
}

// MarkFlagFilename instructs the various shell completion implementations to
// limit completions for this flag to the specified extensions (patterns).
//
// Shell Completion compatibility matrix: bash, zsh
func MarkFlagFilename(flags *pflag.FlagSet, name string, extensions ...string) error {
return flags.SetAnnotation(name, BashCompFilenameExt, extensions)
}

// MarkFlagCustom instructs the various shell completion implementations to
// limit completions for this flag to the specified extensions (patterns).
//
// Shell Completion compatibility matrix: bash, zsh
func MarkFlagCustom(flags *pflag.FlagSet, name string, f string) error {
return flags.SetAnnotation(name, BashCompCustom, []string{f})
}

// MarkFlagDirname instructs the various shell completion implementations to
// complete only directories with this named flag.
//
// Shell Completion compatibility matrix: zsh
func (c *Command) MarkFlagDirname(name string) error {
return MarkFlagDirname(c.Flags(), name)
}

// MarkPersistentFlagDirname instructs the various shell completion
// implementations to complete only directories with this persistent named flag.
//
// Shell Completion compatibility matrix: zsh
func (c *Command) MarkPersistentFlagDirname(name string) error {
return MarkFlagDirname(c.PersistentFlags(), name)
}

// MarkFlagDirname instructs the various shell completion implementations to
// complete only directories with this specified flag.
//
// Shell Completion compatibility matrix: zsh
func MarkFlagDirname(flags *pflag.FlagSet, name string) error {
zshPattern := "-(/)"
return flags.SetAnnotation(name, zshCompDirname, []string{zshPattern})
}
Loading