Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
- [Default Values](#default-values)
- [Place-holders in Help](#place-holders-in-help)
- [Consuming all remaining arguments](#consuming-all-remaining-arguments)
- [Bash/ZSH Shell Completion](#bashzsh-shell-completion)
- [Bash/ZSH/Fish Shell Completion](#bashzshfish-shell-completion)
- [Supporting -h for help](#supporting--h-for-help)
- [Custom help](#custom-help)

Expand Down Expand Up @@ -531,7 +531,7 @@ And use it like so:
ips := IPList(kingpin.Arg("ips", "IP addresses to ping."))
```

### Bash/ZSH Shell Completion
### Bash/ZSH/Fish Shell Completion

By default, all flags and commands/subcommands generate completions
internally.
Expand All @@ -548,8 +548,9 @@ for your target platform/shell). An alternative is to instruct your end
user to source a script from their `bash_profile` (or equivalent).

Fortunately Kingpin makes it easy to generate or source a script for use
with end users shells. `./yourtool --completion-script-bash` and
`./yourtool --completion-script-zsh` will generate these scripts for you.
with end users shells. `./yourtool --completion-script-bash`,
`./yourtool --completion-script-zsh`, and `./yourtool --completion-script-fish`
will generate these scripts for you.

**Installation by Package**

Expand All @@ -574,6 +575,18 @@ Or for ZSH
eval "$(your-cli-tool --completion-script-zsh)"
```

Or for fish

```
your-cli-tool --completion-script-fish | source
```

Or, to install the fish completions permanently:

```
your-cli-tool --completion-script-fish > ~/.config/fish/completions/your-cli-tool.fish
```

#### Additional API
To provide more flexibility, a completion option API has been
exposed for flags to allow user defined completion options, to extend
Expand Down
10 changes: 10 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func New(name, help string) *Application {
a.Flag("completion-bash", "Output possible completions for the given args.").Hidden().BoolVar(&a.completion)
a.Flag("completion-script-bash", "Generate completion script for bash.").Hidden().PreAction(a.generateBashCompletionScript).Bool()
a.Flag("completion-script-zsh", "Generate completion script for ZSH.").Hidden().PreAction(a.generateZSHCompletionScript).Bool()
a.Flag("completion-script-fish", "Generate completion script for fish.").Hidden().PreAction(a.generateFishCompletionScript).Bool()

return a
}
Expand Down Expand Up @@ -108,6 +109,15 @@ func (a *Application) generateZSHCompletionScript(c *ParseContext) error {
return nil
}

func (a *Application) generateFishCompletionScript(c *ParseContext) error {
a.Writer(os.Stdout)
if err := a.UsageForContextWithTemplate(c, 2, FishCompletionTemplate); err != nil {
return err
}
a.terminate(0)
return nil
}

// DefaultEnvars configures all flags (that do not already have an associated
// envar) to use a default environment variable in the form "<app>_<flag>".
//
Expand Down
1 change: 1 addition & 0 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var (
"completion-bash": true,
"completion-script-bash": true,
"completion-script-zsh": true,
"completion-script-fish": true,
}
)

Expand Down
12 changes: 12 additions & 0 deletions templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,15 @@ if [[ "$(basename -- ${(%):-%x})" != "_{{.App.Name}}" ]]; then
compdef _{{.App.Name}} {{.App.Name}}
fi
`

var FishCompletionTemplate = `complete -c {{.App.Name}} -f -a '(
set -l c (commandline -opc)
set -e c[1]
set -l r ({{.App.Name}} --completion-bash $c)
if test -n "$r"
echo $r
else
__fish_complete_path (commandline -ct)
end
)'
`