cli-completion : rewrite completely the program

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@2321 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2015-09-25 09:35:48 +00:00
parent 937f071960
commit 242996154e

View File

@@ -1,41 +1,96 @@
_cli.php()
{
local cur prev opts base
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
local cur prev opts ante
COMPREPLY=()
if [ ! -x "$1" ]; then
return 0
fi
# List of options (operations) available
opts="-h -expert -list -listmethods -listmethodsdetails"
controllers=$($1 -listonly)
opts="$opts $controllers"
if [ "${prev}" == "-list" ]; then
return 0
elif [ "${prev}" == "-listmethods" ] ||
[ "${prev}" == "-listmethodsdetails" ]; then
# Display the available class available
local control=$($1 -listonly)
COMPREPLY=( $(compgen -W "${control}" -- ${cur}) )
return 0
elif [ `$1 -listonly | grep -qx "${prev}" ; echo $?` == 0 ]; then
# Previous was a class : display the methods available
local methods=$($1 -listmethodsonly ${prev})
COMPREPLY=( $(compgen -W "${methods}" -- ${cur}) )
return 0
elif [ `$1 -listonly | grep -qx "${COMP_WORDS[COMP_CWORD-2]}" ; echo $?` == 0 ] &&
[ `$1 -listonly | grep -qx ${prev}" ; echo $?` == 0 ]; then
# A Class and a method available : propose the datas
# XXX : To be done
return 0
opts="
-h
-expert
-list
-listmethods
-listmethodsdetails"
if [ "${COMP_WORDS[1]}" == "-expert" ]; then
EXPERT="-expert"
unset COMP_WORDS[1]
COMP_WORDS=("${COMP_WORDS[@]}")
COMP_CWORD=${COMP_CWORD}-1
else
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
EXPERT=""
fi
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
ante="${COMP_WORDS[COMP_CWORD-2]}"
LISTCLASSES=$($1 $EXPERT -listonly)
LISTCLASSESSLASH=${LISTCLASSES//\\/\\\\\\\\}
opts="$opts
$LISTCLASSESSLASH
"
read -r LISTCLASSESLINE <<< ${LISTCLASSES}
if [ "$3" == "-list" ]; then
# Do not take any option
COMPREPLY=()
return 0
fi
if [ "$3" == "-listmethods" ] ||
[ "$3" == "-listmethodsdetails" ]; then
# Display the available classes available
COMPREPLY=( $(compgen -W "${LISTCLASSESSLASH}" -- ${cur/\\/\\\\\\\\}) )
return 0
fi
if [ "${COMP_WORDS[1]}" == "" ] ||
[ "${COMP_WORDS[1]+1}" != "" ] &&
[ "${COMP_WORDS[2]+1}" == "" ]; then
# Nothing provided or just the first parameter : display the classes and
# options
COMPREPLY=( $(compgen -W "${opts}" -- ${cur//\\/\\\\}) )
return 0
fi
CLASS="${COMP_WORDS[1]}"
CLASS=${CLASS//\\\\/\\}
# Bash Syntax : If the COMP_WORDS[2] exists, return 1, else return ""
if [ "${COMP_WORDS[1]+1}" != "" ] &&
[ "${COMP_WORDS[2]+1}" != "" ] &&
[ "${COMP_WORDS[3]+1}" == "" ]; then
# Class filled, check for the methods
if [[ " ${LISTCLASSESLINE} " =~ " ${CLASS} " ]]; then
# Previous was a valid class : display the methods available
local LISTMETHODS=$($1 $EXPERT -listmethodsonly "${CLASS}")
COMPREPLY=( $(compgen -W "${LISTMETHODS}" -- ${cur}) )
else
# Not a valid class : don't display anything
COMPREPLY=()
fi
return 0
fi
if [ "${COMP_WORDS[1]+1}" != "" ] &&
[ "${COMP_WORDS[2]+1}" != "" ] &&
[ "${COMP_WORDS[3]+1}" != "" ]; then
# Class + method + first parameter available
local LISTMETHODS=$($1 $EXPERT -listmethodsonly "${CLASS}")
read -r LISTMETHODSLINE <<< ${LISTMETHODS}
METHOD=${COMP_WORDS[2]}
if [[ " ${LISTCLASSESLINE} " =~ " ${CLASS} " ]] &&
[[ " ${LISTMETHODSLINE} " =~ " ${METHOD} " ]]; then
# A Class and a method valids : propose the datas
# XXX : To be done
COMPREPLY=()
else
# Invalid class or method provided : don't display anything
COMPREPLY=()
fi
return 0
fi
return 0
}
complete -F _cli.php cli.php