diff --git a/.travis.yml b/.travis.yml index a493f6ce..2e0c6372 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,4 +15,4 @@ install: script: - QIIMETEST= nosetests - flake8 q2cli setup.py - - eval "$(register-qiime-completion 2> /dev/null)" + - source tab-qiime diff --git a/README.md b/README.md index 6e6c7982..da1e5cad 100644 --- a/README.md +++ b/README.md @@ -14,5 +14,5 @@ To get help with QIIME 2, visit http://2.qiime.org. To enable tab completion in Bash, run the following command or add it to your `.bashrc`/`.bash_profile`: ```bash -eval "$(register-qiime-completion 2> /dev/null)" +source tab-qiime ``` diff --git a/bin/register-qiime-completion b/bin/register-qiime-completion deleted file mode 100644 index 36266f13..00000000 --- a/bin/register-qiime-completion +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/env bash - -# Bash completion script that defers to a cached completion script representing -# the state of the current QIIME 2 deployment. -# -# This script is intended to be evaluated on the command-line or in -# .bashrc/.bash_profile: -# -# eval "$(register-qiime-completion 2> /dev/null)" -# -cat << 'EOF' -_qiime_completion() -{ - # Attempt to find the cached completion script. If q2cli isn't installed, - # or is an incompatible version, don't attempt completion. - local path="$(python -c "import q2cli.util; print(q2cli.util.get_completion_path())" 2> /dev/null)" - - if [[ $? != 0 ]]; then - unset COMPREPLY - return 0 - fi - - # If the completion script exists, attempt completion by invoking the - # script in a subshell, supplying COMP_WORDS and COMP_CWORD. Capture the - # output as the completion reply. If the completion script failed, don't - # attempt completion. - if [[ -f "$path" ]] ; then - COMPREPLY=( $(COMP_WORDS="${COMP_WORDS[*]}" COMP_CWORD="${COMP_CWORD}" "$path" 2> /dev/null) ) - - if [[ $? != 0 ]]; then - unset COMPREPLY - return 0 - fi - else - unset COMPREPLY - return 0 - fi - - return 0 -} - -# Enable default readline and bash completion behavior when `_qiime_complete` -# doesn't have a reply. -complete -F _qiime_completion -o default -o bashdefault qiime -EOF diff --git a/bin/tab-qiime b/bin/tab-qiime new file mode 100644 index 00000000..6e995d35 --- /dev/null +++ b/bin/tab-qiime @@ -0,0 +1,54 @@ +#!/usr/bin/env bash + +# Bash completion script that defers to a cached completion script representing +# the state of the current QIIME 2 deployment. +# +# This script is intended to be executed on the command-line or in +# .bashrc/.bash_profile: +# +# source tab-qiime +# + +_qiime_completion() +{ + # Attempt to find the cached completion script. If q2cli isn't installed, or + # is an incompatible version, don't attempt completion. + local path="$(python -c "import q2cli.util; print(q2cli.util.get_completion_path())" 2> /dev/null)" + + if [[ $? != 0 ]]; then + unset COMPREPLY + return 0 + fi + + # If the completion script exists, attempt completion by invoking the script + # in a subshell, supplying COMP_WORDS and COMP_CWORD. Capture the output as + # the completion reply. If the completion script failed, don't attempt + # completion. + if [[ -f "$path" ]] ; then + COMPREPLY=( $(COMP_WORDS="${COMP_WORDS[*]}" COMP_CWORD="${COMP_CWORD}" "$path" 2> /dev/null) ) + + if [[ $? != 0 ]]; then + unset COMPREPLY + return 0 + fi + else + unset COMPREPLY + return 0 + fi + + return 0 +} + +# Enable default readline and bash completion behavior when `_qiime_completion` +# doesn't have a reply. +complete -F _qiime_completion -o default -o bashdefault qiime + +# Execute a `qiime` command (any command will do) so that tab-completion will +# work out-of-the-box (e.g. with a fresh installation of q2cli). Running a +# command will create or refresh the cache if necessary, which contains the +# actual completion script. +# +# Ignore stdout to avoid displaying help text to users enabling tab-completion. +# stderr displays the note about cache refreshing, as that can take a few +# moments to complete. +qiime > /dev/null diff --git a/q2cli/__main__.py b/q2cli/__main__.py index ad533290..9e20bc8f 100644 --- a/q2cli/__main__.py +++ b/q2cli/__main__.py @@ -21,7 +21,7 @@ To enable tab completion in Bash, run the following command or add it to your \ .bashrc/.bash_profile: - eval "$(register-qiime-completion 2> /dev/null)" + source tab-qiime """ diff --git a/setup.py b/setup.py index dba69fe3..bd68e1a5 100644 --- a/setup.py +++ b/setup.py @@ -15,7 +15,7 @@ packages=find_packages(), include_package_data=True, install_requires=['click', 'qiime >= 2.0.2', 'pip'], - scripts=['bin/register-qiime-completion'], + scripts=['bin/tab-qiime'], entry_points=''' [console_scripts] qiime=q2cli.__main__:qiime