use fish builtin for flag/argument checking.

This commit is contained in:
Tim Reddehase 2018-09-25 15:26:41 +02:00
parent 80632ad68c
commit 5ee6b2337b
2 changed files with 13 additions and 11 deletions

View file

@ -39,15 +39,7 @@ function __fish_%s_seen_subcommand_path --description 'Test whether the full pat
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
return 0
end
end
return 1
end
`, cmd.Name(), cmd.Name(), strings.Join(subCommandNames, " "), cmd.Name(), cmd.Name()))
`, cmd.Name(), cmd.Name(), strings.Join(subCommandNames, " "), cmd.Name()))
}
func writeFishCommandCompletion(rootCmd, cmd *Command, buf *bytes.Buffer) {
@ -130,7 +122,11 @@ func commandCompletionCondition(rootCmd, cmd *Command) string {
bareConditions = append(bareConditions, fmt.Sprintf("__fish_%s_no_subcommand", rootCmd.Name()))
}
localNonPersistentFlags.VisitAll(func(flag *pflag.Flag) {
bareConditions = append(bareConditions, fmt.Sprintf("not __fish_%s_has_flag %s", rootCmd.Name(), flag.Name))
flagSelector := fmt.Sprintf("-l %s", flag.Name)
if len(flag.Shorthand) > 0 {
flagSelector = fmt.Sprintf("-s %s %s", flag.Shorthand, flagSelector)
}
bareConditions = append(bareConditions, fmt.Sprintf("not __fish_seen_argument %s", flagSelector))
})
return fmt.Sprintf("-n '%s'", strings.Join(bareConditions, "; and "))
}

View file

@ -87,7 +87,6 @@ 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
@ -113,6 +112,13 @@ func TestFishCompletions(t *testing.T) {
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 local non-persistent flags
checkRegex(t, output, `; and not __fish_seen_argument -l custom[^']*' -a echo`)
checkRegex(t, output, `; and not __fish_seen_argument -l filename[^']*' -a echo`)
checkRegex(t, output, `; and not __fish_seen_argument -l filename-ext[^']*' -a echo`)
checkRegex(t, output, `; and not __fish_seen_argument -s i -l introot[^']*' -a echo`)
checkRegex(t, output, `; and not __fish_seen_argument -l theme[^']*' -a echo`)
// 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`)