paver_autocomplete.sh 3.33 KB
Newer Older
Minh Tue Vo committed
1 2
# Courtesy of Gregory Nicholas

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
_subcommand_opts()
{
    local awkfile command cur usage
    command=$1
    cur=${COMP_WORDS[COMP_CWORD]}
    awkfile=/tmp/paver-option-awkscript-$$.awk
    echo '
BEGIN {
    opts = "";
}

{
    for (i = 1; i <= NF; i = i + 1) {
        # Match short options (-a, -S, -3) 
        # or long options (--long-option, --another_option)
        # in output from paver help [subcommand] 
        if ($i ~ /^(-[A-Za-z0-9]|--[A-Za-z][A-Za-z0-9_-]*)/) {
            opt = $i;
            # remove trailing , and = characters.
            match(opt, "[,=]");  
            if (RSTART > 0) {
                opt = substr(opt, 0, RSTART);
            }
            opts = opts " " opt;
        }
    }
}

END {
    print opts
}' > $awkfile

    usage=`paver help $command`
    options=`echo "$usage"|awk -f $awkfile`

    COMPREPLY=( $(compgen -W "$options" -- "$cur") )
}


Minh Tue Vo committed
42 43
_paver()
{
44
    local cur prev
Minh Tue Vo committed
45 46 47
    COMPREPLY=()
    # Variable to hold the current word
    cur="${COMP_WORDS[COMP_CWORD]}"
48
    prev="${COMP_WORDS[COMP_CWORD - 1]}"
Minh Tue Vo committed
49 50 51 52

    # Build a list of the available tasks from: `paver --help --quiet`
    local cmds=$(paver -hq | awk '/^  ([a-zA-Z][a-zA-Z0-9_]+)/ {print $1}')

53
    subcmd="${COMP_WORDS[1]}"
Minh Tue Vo committed
54 55
    # Generate possible matches and store them in the
    # array variable COMPREPLY
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    
    if [[ -n $subcmd ]]
    then
        case $subcmd in
            test_system)
               
                _test_system_args
                if [[ -n $COMPREPLY ]]
                then
                    return 0
                fi
                ;;
            test_bokchoy)
                _test_bokchoy_args
                if [[ -n $COMPREPLY ]]
                then
                    return 0
                fi
                ;;
            *)
                ;;
        esac

        if [[ ${#COMP_WORDS[*]} == 3 ]]
        then 
                _subcommand_opts $subcmd
                return 0
        else
            if [[ "$cur" == -* ]]
            then
                _subcommand_opts $subcmd
                return 0
            else
                COMPREPLY=( $(compgen -o nospace -- "$cur") )
            fi
        fi
    fi
 
    if [[ ${#COMP_WORDS[*]} == 2 ]]
    then
        COMPREPLY=( $(compgen -W "${cmds}" -- "$cur") )
    fi
}

_test_system_args()
{
        local cur prev
        cur="${COMP_WORDS[COMP_CWORD]}"
        prev="${COMP_WORDS[COMP_CWORD - 1]}"

        case "$prev" in 
            -s|--system)
                COMPREPLY=( $(compgen -W "lms cms" -- "$cur") )
                return 0
                ;;
            *)
                ;;
        esac
Minh Tue Vo committed
114 115
}

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
_test_bokchoy_args()
{
    local bokchoy_tests cur prev
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD - 1]}"

    case "$prev" in 
        -d|--test_dir)
            bokchoy_tests=`find common/test/acceptance -name \*.py| sed 's:common/test/acceptance/::'`
            COMPREPLY=( $(compgen -o filenames -W "$bokchoy_tests" -- $cur) )
            return 0
            ;;
        -t|--test_spec)
            bokchoy_tests=`find common/test/acceptance/tests -name \*.py| sed 's:common/test/acceptance/::'`
            COMPREPLY=( $(compgen -o filenames -W "$bokchoy_tests" -- $cur) )
            return 0
            ;;
        *)
            ;;
    esac
}
Minh Tue Vo committed
137 138
# Assign the auto-completion function for our command.

139
complete -F _paver -o default paver