mirror of
https://github.com/spf13/cobra
synced 2025-05-06 13:27:26 +00:00
support ValidArgs completion of commands in fish.
This commit is contained in:
parent
640b79d2cb
commit
575081ea12
2 changed files with 16 additions and 4 deletions
|
@ -50,6 +50,12 @@ func writeFishCommandCompletion(rootCmd, cmd *Command, buf *bytes.Buffer) {
|
||||||
condition := commandCompletionCondition(rootCmd, cmd)
|
condition := commandCompletionCondition(rootCmd, cmd)
|
||||||
buf.WriteString(fmt.Sprintf("complete -c %s -f %s -a %s -d '%s'\n", rootCmd.Name(), condition, subCmd.Name(), subCmd.Short))
|
buf.WriteString(fmt.Sprintf("complete -c %s -f %s -a %s -d '%s'\n", rootCmd.Name(), condition, subCmd.Name(), subCmd.Short))
|
||||||
})
|
})
|
||||||
|
for _, validArg := range cmd.ValidArgs {
|
||||||
|
condition := commandCompletionCondition(rootCmd, cmd)
|
||||||
|
buf.WriteString(
|
||||||
|
fmt.Sprintf("complete -c %s -f %s -a %s -d '%s'\n",
|
||||||
|
rootCmd.Name(), condition, validArg, fmt.Sprintf("Positional Argument to %s", cmd.Name())))
|
||||||
|
}
|
||||||
writeCommandFlagsCompletion(rootCmd, cmd, buf)
|
writeCommandFlagsCompletion(rootCmd, cmd, buf)
|
||||||
rangeCommands(cmd, func(subCmd *Command) {
|
rangeCommands(cmd, func(subCmd *Command) {
|
||||||
writeFishCommandCompletion(rootCmd, subCmd, buf)
|
writeFishCommandCompletion(rootCmd, subCmd, buf)
|
||||||
|
|
|
@ -7,10 +7,10 @@ import (
|
||||||
|
|
||||||
func TestFishCompletions(t *testing.T) {
|
func TestFishCompletions(t *testing.T) {
|
||||||
rootCmd := &Command{
|
rootCmd := &Command{
|
||||||
Use: "root",
|
Use: "root",
|
||||||
ArgAliases: []string{"pods", "nodes", "services", "replicationcontrollers", "po", "no", "svc", "rc"},
|
ArgAliases: []string{"pods", "nodes", "services", "replicationcontrollers", "po", "no", "svc", "rc"},
|
||||||
ValidArgs: []string{"pod", "node", "service", "replicationcontroller"},
|
ValidArgs: []string{"pod", "node", "service", "replicationcontroller"},
|
||||||
Run: emptyRun,
|
Run: emptyRun,
|
||||||
}
|
}
|
||||||
rootCmd.Flags().IntP("introot", "i", -1, "help message for flag introot")
|
rootCmd.Flags().IntP("introot", "i", -1, "help message for flag introot")
|
||||||
rootCmd.MarkFlagRequired("introot")
|
rootCmd.MarkFlagRequired("introot")
|
||||||
|
@ -111,4 +111,10 @@ func TestFishCompletions(t *testing.T) {
|
||||||
check(t, output, "-n '__fish_seen_subcommand_from echo' -r -l persistent-filename")
|
check(t, output, "-n '__fish_seen_subcommand_from echo' -r -l persistent-filename")
|
||||||
check(t, output, "-n '__fish_seen_subcommand_from echo times' -r -l persistent-filename")
|
check(t, output, "-n '__fish_seen_subcommand_from echo times' -r -l persistent-filename")
|
||||||
check(t, output, "-n '__fish_seen_subcommand_from print' -r -l persistent-filename")
|
check(t, output, "-n '__fish_seen_subcommand_from print' -r -l persistent-filename")
|
||||||
|
|
||||||
|
// check for positional arguments to a command
|
||||||
|
checkRegex(t, output, `-n '__fish_root_no_subcommand(; and[^']*)?' -a pod`)
|
||||||
|
checkRegex(t, output, `-n '__fish_root_no_subcommand(; and[^']*)?' -a node`)
|
||||||
|
checkRegex(t, output, `-n '__fish_root_no_subcommand(; and[^']*)?' -a service`)
|
||||||
|
checkRegex(t, output, `-n '__fish_root_no_subcommand(; and[^']*)?' -a replicationcontroller`)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue