mirror of
https://github.com/spf13/cobra
synced 2025-05-06 05:17:21 +00:00
use custom function for checking subcommand path.
This commit is contained in:
parent
b681855d47
commit
0a631b3402
2 changed files with 16 additions and 10 deletions
|
@ -34,6 +34,11 @@ function __fish_%s_no_subcommand --description 'Test if %s has yet to be given t
|
|||
end
|
||||
return 0
|
||||
end
|
||||
function __fish_%s_seen_subcommand_path --description 'Test whether the full path of subcommands is the current path'
|
||||
set -l cmd (commandline -opc)
|
||||
set -e cmd[1]
|
||||
return (test (string trim -- "$argv") = (string trim -- "$cmd"))
|
||||
end
|
||||
function __fish_%s_has_flag
|
||||
for i in (commandline -opc)
|
||||
if contains -- "--$1" $i
|
||||
|
@ -42,7 +47,7 @@ function __fish_%s_has_flag
|
|||
end
|
||||
return 1
|
||||
end
|
||||
`, cmd.Name(), cmd.Name(), strings.Join(subCommandNames, " "), cmd.Name()))
|
||||
`, cmd.Name(), cmd.Name(), strings.Join(subCommandNames, " "), cmd.Name(), cmd.Name()))
|
||||
}
|
||||
|
||||
func writeFishCommandCompletion(rootCmd, cmd *Command, buf *bytes.Buffer) {
|
||||
|
@ -120,7 +125,7 @@ func commandCompletionCondition(rootCmd, cmd *Command) string {
|
|||
localNonPersistentFlags := cmd.LocalNonPersistentFlags()
|
||||
bareConditions := []string{}
|
||||
if rootCmd != cmd {
|
||||
bareConditions = append(bareConditions, fmt.Sprintf("__fish_seen_subcommand_from %s", subCommandPath(rootCmd, cmd)))
|
||||
bareConditions = append(bareConditions, fmt.Sprintf("__fish_%s_seen_subcommand_path %s", rootCmd.Name(), subCommandPath(rootCmd, cmd)))
|
||||
} else {
|
||||
bareConditions = append(bareConditions, fmt.Sprintf("__fish_%s_no_subcommand", rootCmd.Name()))
|
||||
}
|
||||
|
@ -133,7 +138,7 @@ func commandCompletionCondition(rootCmd, cmd *Command) string {
|
|||
func completionCondition(rootCmd, cmd *Command) string {
|
||||
condition := fmt.Sprintf("-n '__fish_%s_no_subcommand'", rootCmd.Name())
|
||||
if rootCmd != cmd {
|
||||
condition = fmt.Sprintf("-n '__fish_seen_subcommand_from %s'", subCommandPath(rootCmd, cmd))
|
||||
condition = fmt.Sprintf("-n '__fish_%s_seen_subcommand_path %s'", rootCmd.Name(), subCommandPath(rootCmd, cmd))
|
||||
}
|
||||
return condition
|
||||
}
|
||||
|
|
|
@ -88,6 +88,7 @@ func TestFishCompletions(t *testing.T) {
|
|||
// check for preamble helper functions
|
||||
check(t, output, "__fish_root_no_subcommand")
|
||||
check(t, output, "__fish_root_has_flag")
|
||||
check(t, output, "__fish_root_seen_subcommand_path")
|
||||
|
||||
// check for subcommands
|
||||
check(t, output, "-a echo")
|
||||
|
@ -96,21 +97,21 @@ func TestFishCompletions(t *testing.T) {
|
|||
check(t, output, "-a cmd:colon")
|
||||
|
||||
// check for nested subcommands
|
||||
checkRegex(t, output, `-n '__fish_seen_subcommand_from echo(; and[^']*)?' -a times`)
|
||||
checkRegex(t, output, `-n '__fish_root_seen_subcommand_path echo(; and[^']*)?' -a times`)
|
||||
|
||||
// check for flags that will take arguments
|
||||
check(t, output, "-n '__fish_root_no_subcommand' -r -s i -l introot")
|
||||
check(t, output, "-n '__fish_root_no_subcommand' -r -l filename")
|
||||
check(t, output, "-n '__fish_root_no_subcommand' -r -l persistent-filename")
|
||||
check(t, output, "-n '__fish_root_no_subcommand' -r -l theme")
|
||||
check(t, output, "-n '__fish_seen_subcommand_from echo' -r -l config")
|
||||
check(t, output, "-n '__fish_seen_subcommand_from echo' -r -l filename")
|
||||
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 for persistent flags that will take arguments
|
||||
check(t, output, "-n '__fish_seen_subcommand_from cmd:colon' -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 print' -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 times' -r -l persistent-filename")
|
||||
check(t, output, "-n '__fish_root_seen_subcommand_path print' -r -l persistent-filename")
|
||||
|
||||
// check for positional arguments to a command
|
||||
checkRegex(t, output, `-n '__fish_root_no_subcommand(; and[^']*)?' -a pod`)
|
||||
|
|
Loading…
Add table
Reference in a new issue