From e811891d69ab74c3defb2b40f2ec29fd2a420b67 Mon Sep 17 00:00:00 2001 From: Kai Chen Date: Wed, 8 Jul 2020 11:02:46 -0700 Subject: [PATCH] Support files in autocomplete --- src/click/shell_completion.py | 5 +++++ src/click/types.py | 3 +++ 2 files changed, 8 insertions(+) diff --git a/src/click/shell_completion.py b/src/click/shell_completion.py index 158e253ba..0db803ae3 100644 --- a/src/click/shell_completion.py +++ b/src/click/shell_completion.py @@ -26,6 +26,11 @@ IFS=',' read type value <<< "$completion" if [[ $type == 'path' ]]; then COMPREPLY=($(compgen -d -S / -- $value )) + elif [[ $type == 'file' ]]; then + IFS=$'\n' + FILES=( $(compgen -W "$(ls -p | grep -v /)" -- $value)) + IFS=' ' + COMPREPLY=( "${FILES[@]// /\\ }" ) elif [[ $type == 'none' ]]; then COMPREPLY+=("$value ") fi diff --git a/src/click/types.py b/src/click/types.py index 10fb46e6b..c51127500 100644 --- a/src/click/types.py +++ b/src/click/types.py @@ -504,6 +504,9 @@ def convert(self, value, param, ctx): ctx, ) + def shell_complete(self, ctx, all_args, incomplete): + return [["file", incomplete, None]] + class Path(ParamType): """The path type is similar to the :class:`File` type but it performs