From 281d573652be1eb2cf1f27d6117bfcccfe7692c0 Mon Sep 17 00:00:00 2001 From: Marc Khouzam Date: Thu, 31 Dec 2020 16:41:02 -0500 Subject: [PATCH] Support fish completion with env vars in the path Fixes https://github.com/spf13/cobra/issues/1214 Fixes https://github.com/spf13/cobra/issues/1306 Signed-off-by: Marc Khouzam --- fish_completions.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/fish_completions.go b/fish_completions.go index 99f91c99a..55a7a2640 100644 --- a/fish_completions.go +++ b/fish_completions.go @@ -43,16 +43,13 @@ function __%[1]s_perform_completion end __%[1]s_debug "emptyArg: $emptyArg" - if not type -q "$args[1]" - # This can happen when "complete --do-complete %[2]s" is called when running this script. - __%[1]s_debug "Cannot find $args[1]. No completions." - return - end - set requestComp "$args[1] %[3]s $args[2..-1] $emptyArg" __%[1]s_debug "Calling $requestComp" - set results (eval $requestComp 2> /dev/null) + # Call the command as a sub-shell so that we can redirect any errors + # For example, if $requestComp has an unmatched quote + # https://github.com/spf13/cobra/issues/1214 + set results (fish -c "$requestComp" 2> /dev/null) # Some programs may output extra empty lines after the directive. # Let's ignore them or else it will break completion. @@ -66,6 +63,7 @@ function __%[1]s_perform_completion break end end + set comps $results[1..-2] set directiveLine $results[-1]