Python Fabricのタスクタブ補完を有効にする

最近またPython Fabricを使い始めたんだけど、Python Fabricのバージョン1で使っていたタスクのタブ補完入力のシェルスクリプトがバージョン2では動作しなくなってて、なんとかしたいなとウェブ検索で探したりしてたんですが、、、なんてことはない、Fabricのヘルプにあった!

$ fab -h
Print the tab-completion script for your
preferred shell (bash|zsh|fish).


$ fab --print-completion-script=bash


# Invoke tab-completion script to be sourced with Bash shell.
# Known to work on Bash 3.x, untested on 4.x.

_complete_fab() {
    local candidates

    # COMP_WORDS contains the entire command string up til now (including
    # program name).
    # We hand it to Invoke so it can figure out the current context: spit back
    # core options, task names, the current task's options, or some combo.
    candidates=`fab --complete -- ${COMP_WORDS[*]}`

    # `compgen -W` takes list of valid options & a partial word & spits back
    # possible matches. Necessary for any partial word completions (vs
    # completions performed when no partial words are present).
    # $2 is the current word or token being tabbed on, either empty string or a
    # partial word, and thus wants to be compgen'd to arrive at some subset of
    # our candidate list which actually matches.
    # COMPREPLY is the list of valid completions handed back to `complete`.
    COMPREPLY=( $(compgen -W "${candidates}" -- $2) )

# Tell shell builtin to use the above for completing our invocations.
# * -F: use given function name to generate completions.
# * -o default: when function generates no results, use filenames.
# * positional args: program names to complete for.
complete -F _complete_fab -o default fab

# vim: set ft=sh :

と出力されのでそのまま .bash_profile にでも貼り付けて、再読み込みします。

$ source .bash_profile


Comments are closed.