8000 Add completion for flags with pre-defined choices (#211) · stern/stern@e03646c · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit e03646c

Browse files
author
Takashi Kusumi
authored
Add completion for flags with pre-defined choices (#211)
1 parent 1bbee8c commit e03646c

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ stern -p
234234
Stern supports command-line auto completion for bash, zsh or fish. `stern
235235
--completion=(bash|zsh|fish)` outputs the shell completion code which work by being
236236
evaluated in `.bashrc`, etc for the specified shell. In addition, Stern
237-
supports dynamic completion for `--namespace`, `--context` and a resource query
238-
in the form `<resource>/<name>`.
237+
supports dynamic completion for `--namespace`, `--context`, a resource query
238+
in the form `<resource>/<name>`, and flags with pre-defined choices.
239239

240240
If you use bash, stern bash completion code depends on the
241241
[bash-completion](https://github.com/scop/bash-completion). On the macOS, you

cmd/flag_completion.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ import (
2929
clientset "k8s.io/client-go/kubernetes"
3030
)
3131

32+
var flagChoices = map[string][]string{
33+
"color": []string{"always", "never", "auto"},
34+
"completion": []string{"bash", "zsh", "fish"},
35+
"container-state": []string{stern.RUNNING, stern.WAITING, stern.TERMINATED},
36+
"output": []string{"default", "raw", "json", "extjson", "ppextjson"},
37+
}
38+
3239
func runCompletion(shell string, cmd *cobra.Command, out io.Writer) error {
3340
var err error
3441

@@ -72,6 +79,14 @@ func registerCompletionFuncForFlags(cmd *cobra.Command, o *options) error {
7279
return err
7380
}
7481

82+
// flags with pre-defined choices
83+
for flag, choices := range flagChoices {
84+
if err := cmd.RegisterFlagCompletionFunc(flag,
85+
cobra.FixedCompletions(choices, cobra.ShellCompDirectiveNoFileComp)); err != nil {
86+
return err
87+
}
88+
}
89+
7590
return nil
7691
}
7792

0 commit comments

Comments
 (0)
0