#!/bin/bash
_apps()
{
echo $(cat ${APPS} | awk '{print $1}' | grep -v -e '^#\|^$')
}
_servers()
{
echo $(cat ${SERVERS} | awk '{print $2}' | sort -u | cut -f2 -d@)
}
_options()
{
echo "--help --verbose --validate --quiet --server"
}
_commands()
{
echo "status start stop restart kill version config"
}
_contains()
{
local e
for e in ${@:2}; do
if [[ "$e" == "$1" ]]; then
echo 1
return 0
fi
done
echo 0
return 1
}
_complete()
{
local prev_cmd="${COMP_WORDS[COMP_CWORD-1]}"
local curr_cmd="${COMP_WORDS[COMP_CWORD]}"
if [[ ${prev_cmd} == "--server" ]]; then
COMPREPLY=( $(compgen -W "$(_servers)" -- ${curr_cmd}) )
return 0
fi
if [[ ${curr_cmd} == -* ]]; then
COMPREPLY=( $(compgen -W "$(_options)" -- ${curr_cmd}) )
return 0
fi
# previous command was an app name, so show commands
if [[ $(_contains "${prev_cmd}" "$(_apps)") -eq 1 ]]; then
COMPREPLY=( $(compgen -W "$(_commands)" -- ${curr_cmd}) )
return 0
fi
# otherwise try match an app name
COMPREPLY=( $(compgen -W "$(_apps)" -- ${curr_cmd}) )
}
_main()
{
complete -F _complete cmd
}
_main
No comments:
Post a Comment