[Bash-Completion] Fix bash completon for files paths

Changes:
  * fix for command `rally task start --task [TAB]' - now
    it shows filepaths choices
  * introduces function _rally_filedir as replacement for
    _filedir - this fixes issue `._filedir: command not found'
    in case if /etc/bash_completion routines are not loaded
  * small refactoring in rally bash completion

Change-Id: Ib5e15a20c52ba3ccec32424d9404d2a929d82958
Closes-Bug: #1470006
This commit is contained in:
Alexander Maretskiy
2015-06-30 19:44:11 +03:00
parent b766e3c9ce
commit ca34e1ea2d
2 changed files with 43 additions and 27 deletions

View File

@@ -1,6 +1,18 @@
#!/bin/bash
# Standalone _filedir() alternative.
# This exempts from dependence of bash completion routines
function _rally_filedir()
{
test "${1}" \
&& COMPREPLY=( \
$(compgen -f -- "${cur}" | grep -E "${1}") \
$(compgen -o plusdirs -- "${cur}") ) \
|| COMPREPLY=( \
$(compgen -o plusdirs -f -- "${cur}") \
$(compgen -d -- "${cur}") )
}
_rally()
{
declare -A SUBCOMMANDS
@@ -44,7 +56,6 @@ _rally()
OPTS["verify_start"]="--deployment --set --regex --tempest-config --no-use"
OPTS["verify_use"]="--verification"
for OPT in ${!OPTS[*]} ; do
CMD=${OPT%%_*}
CMDSUB=${OPT#*_}
@@ -57,22 +68,19 @@ _rally()
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD-1]}"
if [[ $cur =~ (\.|\~|\/).* ]] ; then
_filedir
if [[ $cur =~ ^(\.|\~|\/) ]] || [[ $prev =~ ^--out(|put-file)$ ]] ; then
_rally_filedir
elif [[ $prev =~ ^--(task|filename)$ ]] ; then
_rally_filedir "\.json|\.yaml|\.yml"
elif [ $COMP_CWORD == "1" ] ; then
COMPREPLY=($(compgen -W "$COMMANDS" -- ${cur}))
elif [ $COMP_CWORD == "2" ] ; then
COMPREPLY=($(compgen -W "${SUBCOMMANDS[${prev}]}" -- ${cur}))
else
if [ $prev == "--filename" ] ; then
_filedir "@(json|ya?ml)"
elif [ $prev == "--output-file" ] || [ $prev == "--out" ]; then
_filedir
else
COMMAND="${COMP_WORDS[1]}_${COMP_WORDS[2]}"
COMPREPLY=($(compgen -W "${OPTS[$COMMAND]}" -- ${cur}))
fi
COMMAND="${COMP_WORDS[1]}_${COMP_WORDS[2]}"
COMPREPLY=($(compgen -W "${OPTS[$COMMAND]}" -- ${cur}))
fi
return 0
}
complete -F _rally rally
complete -o filenames -F _rally rally

View File

@@ -545,8 +545,20 @@ def run(argv, categories):
def _generate_bash_completion_script():
from rally.cli import main
bash_data = """
#!/bin/bash
bash_data = """#!/bin/bash
# Standalone _filedir() alternative.
# This exempts from dependence of bash completion routines
function _rally_filedir()
{
test "${1}" \\
&& COMPREPLY=( \\
$(compgen -f -- "${cur}" | grep -E "${1}") \\
$(compgen -o plusdirs -- "${cur}") ) \\
|| COMPREPLY=( \\
$(compgen -o plusdirs -f -- "${cur}") \\
$(compgen -d -- "${cur}") )
}
_rally()
{
@@ -554,7 +566,6 @@ _rally()
declare -A OPTS
%(data)s
for OPT in ${!OPTS[*]} ; do
CMD=${OPT%%%%_*}
CMDSUB=${OPT#*_}
@@ -567,25 +578,22 @@ _rally()
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD-1]}"
if [[ $cur =~ (\.|\~|\/).* ]] ; then
_filedir
if [[ $cur =~ ^(\.|\~|\/) ]] || [[ $prev =~ ^--out(|put-file)$ ]] ; then
_rally_filedir
elif [[ $prev =~ ^--(task|filename)$ ]] ; then
_rally_filedir "\\.json|\\.yaml|\\.yml"
elif [ $COMP_CWORD == "1" ] ; then
COMPREPLY=($(compgen -W "$COMMANDS" -- ${cur}))
elif [ $COMP_CWORD == "2" ] ; then
COMPREPLY=($(compgen -W "${SUBCOMMANDS[${prev}]}" -- ${cur}))
else
if [ $prev == "--filename" ] ; then
_filedir "@(json|ya?ml)"
elif [ $prev == "--output-file" ] || [ $prev == "--out" ]; then
_filedir
else
COMMAND="${COMP_WORDS[1]}_${COMP_WORDS[2]}"
COMPREPLY=($(compgen -W "${OPTS[$COMMAND]}" -- ${cur}))
fi
COMMAND="${COMP_WORDS[1]}_${COMP_WORDS[2]}"
COMPREPLY=($(compgen -W "${OPTS[$COMMAND]}" -- ${cur}))
fi
return 0
}
complete -F _rally rally
complete -o filenames -F _rally rally
"""
completion = []
for category, cmds in main.categories.items():