From 69b6030e2db131dbbe6c7d85d1538ee2acf3c1b1 Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Tue, 1 Aug 2023 16:01:19 +0900 Subject: [PATCH] fix(python): complete filenames for script arguments --- completions/python | 45 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/completions/python b/completions/python index d06f8f28677..8bec5b875c5 100644 --- a/completions/python +++ b/completions/python @@ -44,6 +44,38 @@ _comp_cmd_python() esac local noargopts='!(-*|*[cmQWX]*)' + + # if command, module, or script is already given by [-c command | -m module + # | script], complete all kind of files. + local i has_command="" + for ((i = 1; i < cword; i++)); do + # shellcheck disable=SC2254 + case ${words[i]} in + -${noargopts}[QWX]) + ((i++)) + ;; + -${noargopts}[cm]?*) + has_command=set + break + ;; + -- | -${noargopts}[cm]) + if ((i + 1 < cword)); then + has_command=set + break + fi + ;; + -*) ;; + *) + has_command=set + break + ;; + esac + done + if [[ $has_command ]]; then + _comp_compgen_filedir + return + fi + # shellcheck disable=SC2254 case $prev in --help | --version | -${noargopts}[?hVc]) @@ -74,19 +106,10 @@ _comp_cmd_python() _comp_compgen -- -W "help off" return ;; - !(?(*/)?(micro)python*([0-9.])|?(*/)py@(py|ston)*([0-9.])|-?)) - if [[ $cword -lt 2 || ${words[cword - 2]} != -[QWX] ]]; then - _comp_compgen_filedir - return - fi - ;; esac - # if -c or -m is already given, complete all kind of files. - if [[ ${words[*]::cword} == *\ -[cm]\ * ]]; then - _comp_compgen -a filedir - elif [[ $cur != -* ]]; then - _comp_compgen -a filedir '@(py?([cowz])|zip)' + if [[ $prev == -- || $cur != -* ]]; then + _comp_compgen_filedir '@(py?([cowz])|zip)' else _comp_compgen_help - <<<"$("$1" -h | awk '{ sub("\\(-bb:","\n-bb "); print }')"