_cli.php()
{

  local cur prev opts ante
  COMPREPLY=()
  if [ ! -x "$1" ]; then
    return 0
  fi

  # List of options (operations) available
  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
    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
"
  LISTCLASSESLINE=$(tr '\n' ' ' <<< ${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}")
    LISTMETHODSLINE=$(tr '\n' ' ' <<< ${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
}

complete -F _cli.php cli
complete -F _cli.php cli.php
complete -F _cli.php ./cli/cli.php
