mirror of
https://github.com/spf13/cobra
synced 2025-05-06 05:17:21 +00:00
escape description of commands & flags.
This commit is contained in:
parent
157737b787
commit
01850df9a1
2 changed files with 9 additions and 4 deletions
|
@ -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 {
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Add table
Reference in a new issue