Skip to content

Commit 4534026

Browse files
authored
Add completions for fish shell (#363)
As per title, adds shell completions for [fish shell](https://github.com/fish-shell/fish-shell) Closes #259
1 parent 5e8f9d9 commit 4534026

4 files changed

Lines changed: 40 additions & 4 deletions

File tree

README.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
- [Default Values](#default-values)
3838
- [Place-holders in Help](#place-holders-in-help)
3939
- [Consuming all remaining arguments](#consuming-all-remaining-arguments)
40-
- [Bash/ZSH Shell Completion](#bashzsh-shell-completion)
40+
- [Bash/ZSH/Fish Shell Completion](#bashzshfish-shell-completion)
4141
- [Supporting -h for help](#supporting--h-for-help)
4242
- [Custom help](#custom-help)
4343

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

534-
### Bash/ZSH Shell Completion
534+
### Bash/ZSH/Fish Shell Completion
535535

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

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

554555
**Installation by Package**
555556

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

578+
Or for fish
579+
580+
```
581+
your-cli-tool --completion-script-fish | source
582+
```
583+
584+
Or, to install the fish completions permanently:
585+
586+
```
587+
your-cli-tool --completion-script-fish > ~/.config/fish/completions/your-cli-tool.fish
588+
```
589+
577590
#### Additional API
578591
To provide more flexibility, a completion option API has been
579592
exposed for flags to allow user defined completion options, to extend

app.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func New(name, help string) *Application {
6868
a.Flag("completion-bash", "Output possible completions for the given args.").Hidden().BoolVar(&a.completion)
6969
a.Flag("completion-script-bash", "Generate completion script for bash.").Hidden().PreAction(a.generateBashCompletionScript).Bool()
7070
a.Flag("completion-script-zsh", "Generate completion script for ZSH.").Hidden().PreAction(a.generateZSHCompletionScript).Bool()
71+
a.Flag("completion-script-fish", "Generate completion script for fish.").Hidden().PreAction(a.generateFishCompletionScript).Bool()
7172

7273
return a
7374
}
@@ -108,6 +109,15 @@ func (a *Application) generateZSHCompletionScript(c *ParseContext) error {
108109
return nil
109110
}
110111

112+
func (a *Application) generateFishCompletionScript(c *ParseContext) error {
113+
a.Writer(os.Stdout)
114+
if err := a.UsageForContextWithTemplate(c, 2, FishCompletionTemplate); err != nil {
115+
return err
116+
}
117+
a.terminate(0)
118+
return nil
119+
}
120+
111121
// DefaultEnvars configures all flags (that do not already have an associated
112122
// envar) to use a default environment variable in the form "<app>_<flag>".
113123
//

model.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var (
1616
"completion-bash": true,
1717
"completion-script-bash": true,
1818
"completion-script-zsh": true,
19+
"completion-script-fish": true,
1920
}
2021
)
2122

templates.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,15 @@ if [[ "$(basename -- ${(%):-%x})" != "_{{.App.Name}}" ]]; then
260260
compdef _{{.App.Name}} {{.App.Name}}
261261
fi
262262
`
263+
264+
var FishCompletionTemplate = `complete -c {{.App.Name}} -f -a '(
265+
set -l c (commandline -opc)
266+
set -e c[1]
267+
set -l r ({{.App.Name}} --completion-bash $c)
268+
if test -n "$r"
269+
echo $r
270+
else
271+
__fish_complete_path (commandline -ct)
272+
end
273+
)'
274+
`

0 commit comments

Comments
 (0)