From 4f3e5f496b455b68777740e16fa77ec09ffba639 Mon Sep 17 00:00:00 2001 From: Tim Reddehase Date: Tue, 25 Sep 2018 17:32:11 +0200 Subject: [PATCH] escape description of commands & flags. --- fish_completions.go | 6 ++++-- fish_completions_test.go | 7 +++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/fish_completions.go b/fish_completions.go index d1f0a6df..65a8ef55 100644 --- a/fish_completions.go +++ b/fish_completions.go @@ -67,7 +67,8 @@ end func writeFishCommandCompletion(rootCmd, cmd *Command, buf *bytes.Buffer) { rangeCommands(cmd, func(subCmd *Command) { 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)) + escapedDescription := strings.Replace(subCmd.Short, "'", "\\'", -1) + buf.WriteString(fmt.Sprintf("complete -c %s -f %s -a %s -d '%s'\n", rootCmd.Name(), condition, subCmd.Name(), escapedDescription)) }) for _, validArg := range append(cmd.ValidArgs, cmd.ArgAliases...) { condition := commandCompletionCondition(rootCmd, cmd) @@ -102,8 +103,9 @@ func writeCommandFlagCompletion(rootCmd, cmd *Command, buf *bytes.Buffer, flag * shortHandPortion = fmt.Sprintf("-s %s", flag.Shorthand) } condition := completionCondition(rootCmd, cmd) + escapedUsage := strings.Replace(flag.Usage, "'", "\\'", -1) buf.WriteString(fmt.Sprintf("complete -c %s -f %s %s %s -l %s -d '%s'\n", - rootCmd.Name(), condition, flagRequiresArgumentCompletion(flag), shortHandPortion, flag.Name, flag.Usage)) + rootCmd.Name(), condition, flagRequiresArgumentCompletion(flag), shortHandPortion, flag.Name, escapedUsage)) } func flagRequiresArgumentCompletion(flag *pflag.Flag) string { diff --git a/fish_completions_test.go b/fish_completions_test.go index 69eb9a94..e5f9a4e1 100644 --- a/fish_completions_test.go +++ b/fish_completions_test.go @@ -12,7 +12,7 @@ func TestFishCompletions(t *testing.T) { ValidArgs: []string{"pod", "node", "service", "replicationcontroller"}, Run: emptyRun, } - rootCmd.Flags().IntP("introot", "i", -1, "help message for flag introot") + rootCmd.Flags().IntP("introot", "i", -1, "help's message for flag introot") rootCmd.MarkFlagRequired("introot") // Filename. @@ -36,7 +36,7 @@ func TestFishCompletions(t *testing.T) { echoCmd := &Command{ Use: "echo [string to echo]", Aliases: []string{"say"}, - Short: "Echo anything to the screen", + Short: "Echo anything's to the screen", Long: "an utterly useless command for testing.", Example: "Just run cobra-test echo", Run: emptyRun, @@ -107,6 +107,9 @@ func TestFishCompletions(t *testing.T) { check(t, output, "-n '__fish_root_seen_subcommand_path echo' -r -l config") check(t, output, "-n '__fish_root_seen_subcommand_path echo' -r -l filename") + // checks escape of description in flags + check(t, output, "-n '__fish_root_no_subcommand' -r -s i -l introot -d 'help\\'s message for flag introot'") + // check for persistent flags that will take arguments check(t, output, "-n '__fish_root_seen_subcommand_path cmd:colon' -r -l persistent-filename") check(t, output, "-n '__fish_root_seen_subcommand_path echo' -r -l persistent-filename")