LoginSignup
5
4

More than 5 years have passed since last update.

CakePHP3でもTabでコマンド補完をしたい!

Last updated at Posted at 2016-09-04

CakePHPのコンソール作業をしているときにすべてのコマンドやオプションを憶えておくのはとても難しいと思います。
自分は無理です。。。
あのコマンド何だったけ?
bakeのあのオプション何だったけ。。。等々

この問題を解決する為にCakePHP3には Completion シェル が実装されています。

bash-completionのインストール

まずCakePHP3のコマンド補完を行う為に、 bash-completion をインストールする必要があります。

# epelリポジトリを有効にする必要があります。
yum install bash-completion

bash-completionスクリプトの設置

以下のファイルを作成する(作成後は再ログイン等を行う必要があります)

/etc/bash_completion.d/cake

#
# Bash completion file for CakePHP console
#

_cake()
{
    local cur prev opts cake
    COMPREPLY=()
    cake="${COMP_WORDS[0]}"
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    if [[ "$cur" == -* ]] ; then
        if [[ ${COMP_CWORD} = 1 ]] ; then
            opts=$(${cake} Completion options)
        elif [[ ${COMP_CWORD} = 2 ]] ; then
            opts=$(${cake} Completion options "${COMP_WORDS[1]}")
        else
            opts=$(${cake} Completion options "${COMP_WORDS[1]}" "${COMP_WORDS[2]}")
        fi

        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        return 0
    fi

    if [[ ${COMP_CWORD} = 1 ]] ; then
        opts=$(${cake} Completion commands)
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        return 0
    fi

    if [[ ${COMP_CWORD} = 2 ]] ; then
        opts=$(${cake} Completion subcommands $prev)
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        if [[ $COMPREPLY = "" ]] ; then
            _filedir
            return 0
        fi
        return 0
    fi

    opts=$(${cake} Completion fuzzy "${COMP_WORDS[@]:1}")
    COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
    if [[ $COMPREPLY = "" ]] ; then
        _filedir
        return 0
    fi
    return 0;
}

complete -F _cake cake bin/cake

実行結果

これで通常コマンドと同様にTabを押すことでコマンド補完を行うことが可能です。
自作のコマンド も同様に補完できます。

○コマンド一覧

bin/cake [Tab]
bake        benchmark   cache       console     i18n        migrations  orm_cache   plugin      routes      server      whitespace

○サブコマンド一覧

bin/cake bake [Tab]
all                 cell                controller          form                loadTasks           migration           migration_snapshot  plugin              shell               startup             template            behavior            component           fixture             helper              mailer              migration_diff      model               seed                shell_helper        task                test

○オプション一覧

bin/cake bake -[Tab]
--connection  --everything  --force       --help        --plugin      --prefix      --quiet       --theme       --verbose     -c            -f            -h            -p            -q            -t            -v

参考

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4