Fall back to filename completion if no completer is defined.
authorAndre Noll <maan@tuebingen.mpg.de>
Mon, 13 Nov 2017 01:13:46 +0000 (02:13 +0100)
committerAndre Noll <maan@tuebingen.mpg.de>
Thu, 30 Nov 2017 13:00:22 +0000 (14:00 +0100)
If a gsu application does not implement subcommand completers, it's
best to  complete on file names, if only to make completion work in
the common case of redirections. For example we like to complete the
last word of a partial command line of the form

app subcommand > /dev/nu

This did not work so far. With the patch applied, it does.

The application can always override the behaviour by implementing a
suitable completer.

subcommand

index 956fb5c..f269cdb 100644 (file)
@@ -739,7 +739,12 @@ com_complete()
                local -a candidates;
 
                candidates=(\$($0 complete "\$COMP_CWORD" "\${COMP_WORDS[@]}"));
-               COMPREPLY=(\$(compgen -W "\${candidates[*]}" -- "\$cur"));
+               if ((\$? == 0)); then
+                       COMPREPLY=(\$(compgen -W "\${candidates[*]}" -- "\$cur"));
+               else
+                       compopt -o filenames;
+                       COMPREPLY=(\$(compgen -fd -- "\$cur"));
+               fi
 EOF
                ret=$GSU_SUCCESS
                return
@@ -757,8 +762,9 @@ EOF
        shift
        words=("$@")
        cmd="${words[1]}"
-       ret=$GSU_SUCCESS # It's not an error if no completer was defined
-       [[ "$(type -t "complete_$cmd")" != "function" ]] && return
+       # if no completer is defined for this subcommand we exit unsuccessfully
+       # to let the generic completer above fall back to file name completion.
+       [[ "$(type -t "complete_$cmd")" != "function" ]] && exit 1
        "complete_$cmd" "$cword" "${words[@]}"
        # ignore errors, they would only clutter the completion output
        ret=$GSU_SUCCESS