escape description of commands & flags.

This commit is contained in:
Tim Reddehase 2018-09-25 17:32:11 +02:00
parent 157737b787
commit 01850df9a1
2 changed files with 9 additions and 4 deletions

View file

@ -67,7 +67,8 @@ end
func writeFishCommandCompletion(rootCmd, cmd *Command, buf *bytes.Buffer) { func writeFishCommandCompletion(rootCmd, cmd *Command, buf *bytes.Buffer) {
rangeCommands(cmd, func(subCmd *Command) { rangeCommands(cmd, func(subCmd *Command) {
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)) 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...) { for _, validArg := range append(cmd.ValidArgs, cmd.ArgAliases...) {
condition := commandCompletionCondition(rootCmd, cmd) condition := commandCompletionCondition(rootCmd, cmd)
@ -102,8 +103,9 @@ func writeCommandFlagCompletion(rootCmd, cmd *Command, buf *bytes.Buffer, flag *
shortHandPortion = fmt.Sprintf("-s %s", flag.Shorthand) shortHandPortion = fmt.Sprintf("-s %s", flag.Shorthand)
} }
condition := completionCondition(rootCmd, cmd) 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", 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 { func flagRequiresArgumentCompletion(flag *pflag.Flag) string {

View file

@ -12,7 +12,7 @@ func TestFishCompletions(t *testing.T) {
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's message for flag introot")
rootCmd.MarkFlagRequired("introot") rootCmd.MarkFlagRequired("introot")
// Filename. // Filename.
@ -36,7 +36,7 @@ func TestFishCompletions(t *testing.T) {
echoCmd := &Command{ echoCmd := &Command{
Use: "echo [string to echo]", Use: "echo [string to echo]",
Aliases: []string{"say"}, Aliases: []string{"say"},
Short: "Echo anything to the screen", Short: "Echo anything's to the screen",
Long: "an utterly useless command for testing.", Long: "an utterly useless command for testing.",
Example: "Just run cobra-test echo", Example: "Just run cobra-test echo",
Run: emptyRun, 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 config")
check(t, output, "-n '__fish_root_seen_subcommand_path echo' -r -l filename") 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 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 cmd:colon' -r -l persistent-filename")
check(t, output, "-n '__fish_root_seen_subcommand_path echo' -r -l persistent-filename") check(t, output, "-n '__fish_root_seen_subcommand_path echo' -r -l persistent-filename")