_altflow_cli()
{
    local cur prev command word
    COMPREPLY=()
    cur=${COMP_WORDS[COMP_CWORD]}
    prev=${COMP_WORDS[COMP_CWORD-1]}

    case "$prev" in
        --log-file|--log_file)
            COMPREPLY=( $(compgen -f -- "$cur") )
            return
            ;;
        -s|--stage)
            COMPREPLY=( $(compgen -W 'BUILD TEST SIGN PUSH CLEANUP' -- "$cur") )
            return
            ;;
    esac

    for word in "${COMP_WORDS[@]:1:COMP_CWORD}"; do
        case "$word" in
            auth|token|req|tasks|log) command=$word; break ;;
        esac
    done

    if [[ -z $command ]]; then
        COMPREPLY=( $(compgen -W 'auth token req tasks log -i --insecure -h --help' -- "$cur") )
        return
    fi

    case "$command" in
        req)
            if [[ $cur == -* ]]; then
                COMPREPLY=( $(compgen -W '-i --insecure -n --names --exclude -s --silent --log-file --log_file -h --help' -- "$cur") )
            else
                COMPREPLY=( $(compgen -f -- "$cur") )
            fi
            ;;
        tasks)
            COMPREPLY=( $(compgen -W '-i --insecure -f --follow --log-file --log_file -h --help' -- "$cur") )
            ;;
        log)
            COMPREPLY=( $(compgen -W '-i --insecure -t --task -f --follow -s --stage -l --limit --since --log-file --log_file -h --help' -- "$cur") )
            ;;
        auth|token)
            COMPREPLY=( $(compgen -W '-i --insecure -h --help' -- "$cur") )
            ;;
    esac
}

complete -F _altflow_cli altflow-cli
