alias myapp="python ./myapp.py"

_myapp_complete()
{
    local cur prev BASE_LEVEL

    COMPREPLY=()
    cur=${COMP_WORDS[COMP_CWORD]}
    prev=${COMP_WORDS[COMP_CWORD-1]}

    # SETUP THE BASE LEVEL (everything after "myapp")
    if [ $COMP_CWORD -eq 1 ]; then
        COMPREPLY=( $(compgen \
                      -W "base-cmd1 base-cmd2 embedded-cmd3 second" \
                      -- $cur) )


    # SETUP THE SECOND LEVEL (EVERYTHING AFTER "myapp second")
    elif [ $COMP_CWORD -eq 2 ]; then
        case "$prev" in

            # HANDLE EVERYTHING AFTER THE SECOND LEVEL NAMESPACE
            "second")
                COMPREPLY=( $(compgen \
                              -W "second-cmd4 second-cmd5 third" \
                              -- $cur) )
                ;;

            # IF YOU HAD ANOTHER CONTROLLER, YOU'D HANDLE THAT HERE
            "some-other-controller")
                COMPREPLY=( $(compgen \
                              -W "some-other-sub-command" \
                              -- $cur) )
                ;;

            # EVERYTHING ELSE
            *)
                ;;
        esac

    # SETUP THE THIRD LEVEL (EVERYTHING AFTER "myapp second third")
    elif [ $COMP_CWORD -eq 3 ]; then
        case "$prev" in
            # HANDLE EVERYTHING AFTER THE THIRD LEVEL NAMESPACE
            "third")
                COMPREPLY=( $(compgen \
                              -W "third-cmd6 third-cmd7" \
                              -- $cur) )
                ;;

            # IF YOU HAD ANOTHER CONTROLLER, YOU'D HANDLE THAT HERE
            "some-other-controller")
                COMPREPLY=( $(compgen \
                              -W "some-other-sub-command" \
                              -- $cur) )
                ;;

            *)
                ;;
        esac
    fi

    return 0

} &&
complete -F _myapp_complete myapp
