#!/bin/bash _appsh_contains() { local e for e in "${@:2}"; do [[ "$e" == "$1" ]] && echo $1 && return; done } _appsh_methods=( "conf" "install" "list" "list-versions" "set-current" "start" "stop" ) _appsh_parse_opts() { local prev="" local has_method for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do local curr=${COMP_WORDS[$i]} if [ "$prev" == -n ] then echo "local has_n=$curr" elif [ "$prev" == -i ] then echo "local has_i=$curr" fi has_method="$has_method$(_appsh_contains "$curr" ${_appsh_methods[@]})" prev="${COMP_WORDS[$i]}" done echo "local has_method='$has_method'" } _complete_appsh() { COMPREPLY=() local cur="${COMP_WORDS[COMP_CWORD]}" local prev="${COMP_WORDS[COMP_CWORD-1]}" local opts eval `_appsh_parse_opts` if [ $COMP_CWORD == 1 ] then opts="${_appsh_methods[@]}" opts="-n $opts" COMPREPLY=($(compgen -W "${opts}" -- ${cur})) return 0 fi case $prev in -n) values=$(./app list -P name) COMPREPLY=($(compgen -W "$values" -- ${cur})) return 0 ;; -i) if [ -n "$has_n" ] then values=$(./app list -n $has_n -P instance) COMPREPLY=($(compgen -W "$values" -- ${cur})) return 0 fi ;; esac opts="" if [ -z "$has_n" ] then opts="$opts -n" else if [ -z "$has_i" ] then opts="$opts -i" fi fi if [ -z "$has_method" ] then opts="$opts ${_appsh_methods[@]}" fi COMPREPLY=($(compgen -W "${opts}" -- ${cur})) set +x } complete -F _complete_appsh app