Pythonスクリプト引数に対して有効なタブ補完機能を持つようにbashにどのように指示しますか?

Pythonスクリプト引数に対して有効なタブ補完機能を持つようにbashにどのように指示しますか?

hello.pyプログラムがあり、可能な有効なパラメータの1つが次のとおりであるとします。

./hello.py autoawesomesauce

次のように入力できます。

./hello.py auto[tab]

この時点で、部分的に完了した引数はhelloに送信され、helloはそれを認識してシェルで完了します。

./hello.py autoawesomesauce

gitは似たようなことをしていることを知っていますが、Pythonスクリプト+ Bashを使用して実行できますか?

ベストアンサー1

Linuxシステムでは、通常、次の場所で複数のサンプルスクリプトを見つけることができます/etc/bash_completion.d。これらのスクリプトを取得すると、オートコンプリート動作が発生します。

そのディレクトリに例を含めました。解凍した完了スクリプトは次のとおりです。

_unrar()
{
    local cur

    COMPREPLY=()
    _get_comp_words_by_ref cur

    if [[ "$cur" == -* ]] ; then
        COMPREPLY=( $( compgen -W '-ad -ap -av- -c- -cfg- -cl -cu \
            -dh -ep -f -idp -ierr -inul -kb -o+ -o- -ow -p -p- -r -ta \
            -tb -tn -to -u -v -ver -vp -x -x@ -y' -- "$cur" ) )
    else
        if [ $COMP_CWORD -eq 1 ]; then
            COMPREPLY=( $( compgen -W 'e l lb lt p t v vb vt x' -- "$cur" ) )
        else
            _filedir '@(rar|RAR)'
        fi
    fi

} &&
complete -F _unrar -o filenames unrar

おすすめ記事