From 582ef5667aabf973207e853e5ab1dc9f561466dc Mon Sep 17 00:00:00 2001 From: Tim Reddehase Date: Tue, 25 Sep 2018 13:07:54 +0200 Subject: [PATCH] support ValidArgs completion of commands in fish. --- fish_completions.go | 6 ++++++ fish_completions_test.go | 14 ++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/fish_completions.go b/fish_completions.go index 201bd331..5fc8df09 100644 --- a/fish_completions.go +++ b/fish_completions.go @@ -50,6 +50,12 @@ func writeFishCommandCompletion(rootCmd, cmd *Command, buf *bytes.Buffer) { 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)) }) + 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) rangeCommands(cmd, func(subCmd *Command) { writeFishCommandCompletion(rootCmd, subCmd, buf) diff --git a/fish_completions_test.go b/fish_completions_test.go index 56ff8728..4ba8eb71 100644 --- a/fish_completions_test.go +++ b/fish_completions_test.go @@ -7,10 +7,10 @@ import ( func TestFishCompletions(t *testing.T) { rootCmd := &Command{ - Use: "root", - ArgAliases: []string{"pods", "nodes", "services", "replicationcontrollers", "po", "no", "svc", "rc"}, - ValidArgs: []string{"pod", "node", "service", "replicationcontroller"}, - Run: emptyRun, + Use: "root", + ArgAliases: []string{"pods", "nodes", "services", "replicationcontrollers", "po", "no", "svc", "rc"}, + ValidArgs: []string{"pod", "node", "service", "replicationcontroller"}, + Run: emptyRun, } rootCmd.Flags().IntP("introot", "i", -1, "help message for flag 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 times' -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`) }