From 5ee6b2337b8e4ca659bb979c761efad8b2645b97 Mon Sep 17 00:00:00 2001 From: Tim Reddehase Date: Tue, 25 Sep 2018 15:26:41 +0200 Subject: [PATCH] use fish builtin for flag/argument checking. --- fish_completions.go | 16 ++++++---------- fish_completions_test.go | 8 +++++++- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/fish_completions.go b/fish_completions.go index 2a5ec19a..19afae87 100644 --- a/fish_completions.go +++ b/fish_completions.go @@ -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 ")) } diff --git a/fish_completions_test.go b/fish_completions_test.go index fc65dfd9..2cae7fc8 100644 --- a/fish_completions_test.go +++ b/fish_completions_test.go @@ -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`)