#compdef apicli

_apicli() {
    local program=apicli
    typeset -A opt_args
    local curcontext="$curcontext" state line context


        # ---- Command: 
        _arguments -s  \
            '1: :->cmd1' \
            '*: :->args' \
            && ret=0


        case $state in
        cmd1)
            _alternative 'args:cmd2:((appspec\:"Generate App::Spec file" help\:"Show command help"))'
        ;;

        args)
            case $line[1] in
            _meta)

                # ---- Command: _meta
                _arguments -s -C \
                    '1: :->cmd1' \
                    '2: :->cmd2' \
                    '*: :->args' \
                    && ret=0


                case $state in
                cmd2)
                    _alternative 'args:cmd3:((completion\:"Shell completion functions" pod\:"Pod documentation"))'
                ;;

                args)
                    case $line[2] in
                    completion)

                        # ---- Command: _meta completion
                        _arguments -s -C \
                            '1: :->cmd1' \
                            '2: :->cmd2' \
                            '3: :->cmd3' \
                            '*: :->args' \
                            && ret=0


                        case $state in
                        cmd3)
                            _alternative 'args:cmd4:((generate\:"Generate self completion"))'
                        ;;

                        args)
                            case $line[3] in
                            generate)

                                # ---- Command: _meta completion generate
                                _arguments -s -C \
                                    '1: :->cmd1' \
                                    '2: :->cmd2' \
                                    '3: :->cmd3' \
                                    '--help[Show command help]' \
                                    '-h[Show command help]' \
                                    '--name[name of the program (optional, override name in spec)]:name' \
                                    '--zsh[for zsh]' \
                                    '--bash[for bash]' \
                                    && ret=0


                            ;;
                            esac

                        ;;

                        esac
                    ;;
                    pod)

                        # ---- Command: _meta pod
                        _arguments -s -C \
                            '1: :->cmd1' \
                            '2: :->cmd2' \
                            '3: :->cmd3' \
                            '*: :->args' \
                            && ret=0


                        case $state in
                        cmd3)
                            _alternative 'args:cmd4:((generate\:"Generate self pod"))'
                        ;;

                        args)
                            case $line[3] in
                            generate)

                                # ---- Command: _meta pod generate
                                _arguments -s -C \
                                    '1: :->cmd1' \
                                    '2: :->cmd2' \
                                    '3: :->cmd3' \
                                    '--help[Show command help]' \
                                    '-h[Show command help]' \
                                    && ret=0


                            ;;
                            esac

                        ;;

                        esac
                    ;;
                    esac

                ;;

                esac
            ;;
            appspec)

                # ---- Command: appspec
                _arguments -s -C \
                    '1: :->cmd1' \
                    '2: :->file' \
                    '--help[Show command help]' \
                    '-h[Show command help]' \
                    '--from[Currently only '"'"'openapi'"'"' is supported]:from' \
                    '--out[Output file (default stdout)]:out' \
                    '--name[appname]:name' \
                    '--class[Class name (default API::CLI)]:class' \
                    && ret=0

                case $state in
                file)
_files
                ;;
                esac

            ;;
            help)

                # ---- Command: help
                _arguments -s -C \
                    '1: :->cmd1' \
                    '2: :->cmd2' \
                    '*: :->args' \
                    && ret=0


                case $state in
                cmd2)
                    _alternative 'args:cmd3:((appspec))'
                ;;

                args)
                    case $line[2] in
                    _meta)

                        # ---- Command: help _meta
                        _arguments -s -C \
                            '1: :->cmd1' \
                            '2: :->cmd2' \
                            '3: :->cmd3' \
                            '*: :->args' \
                            && ret=0


                        case $state in
                        cmd3)
                            _alternative 'args:cmd4:((completion pod))'
                        ;;

                        args)
                            case $line[3] in
                            completion)

                                # ---- Command: help _meta completion
                                _arguments -s -C \
                                    '1: :->cmd1' \
                                    '2: :->cmd2' \
                                    '3: :->cmd3' \
                                    '4: :->cmd4' \
                                    '*: :->args' \
                                    && ret=0


                                case $state in
                                cmd4)
                                    _alternative 'args:cmd5:((generate))'
                                ;;

                                args)
                                    case $line[4] in
                                    generate)

                                        # ---- Command: help _meta completion generate
                                        _arguments -s -C \
                                            '1: :->cmd1' \
                                            '2: :->cmd2' \
                                            '3: :->cmd3' \
                                            '4: :->cmd4' \
                                            '--help[Show command help]' \
                                            '-h[Show command help]' \
                                            '--all[]' \
                                            && ret=0


                                    ;;
                                    esac

                                ;;

                                esac
                            ;;
                            pod)

                                # ---- Command: help _meta pod
                                _arguments -s -C \
                                    '1: :->cmd1' \
                                    '2: :->cmd2' \
                                    '3: :->cmd3' \
                                    '4: :->cmd4' \
                                    '*: :->args' \
                                    && ret=0


                                case $state in
                                cmd4)
                                    _alternative 'args:cmd5:((generate))'
                                ;;

                                args)
                                    case $line[4] in
                                    generate)

                                        # ---- Command: help _meta pod generate
                                        _arguments -s -C \
                                            '1: :->cmd1' \
                                            '2: :->cmd2' \
                                            '3: :->cmd3' \
                                            '4: :->cmd4' \
                                            '--help[Show command help]' \
                                            '-h[Show command help]' \
                                            '--all[]' \
                                            && ret=0


                                    ;;
                                    esac

                                ;;

                                esac
                            ;;
                            esac

                        ;;

                        esac
                    ;;
                    appspec)

                        # ---- Command: help appspec
                        _arguments -s -C \
                            '1: :->cmd1' \
                            '2: :->cmd2' \
                            '--help[Show command help]' \
                            '-h[Show command help]' \
                            '--all[]' \
                            && ret=0


                    ;;
                    esac

                ;;

                esac
            ;;
            esac

        ;;

        esac

}


__apicli_dynamic_comp() {
    local argname="$1"
    local arg="$2"
    local comp="arg:$argname:(("
    local line
    while read -r line; do
        local name="$line"
        local desc="$line"
        name="${name%$'\t'*}"
        desc="${desc/*$'\t'}"
        comp="$comp$name"
        if [[ -n "$desc" && "$name" != "$desc" ]]; then
            comp="$comp\\:"'"'"$desc"'"'
        fi
        comp="$comp "
    done <<< "$arg"

    comp="$comp))"
    _alternative "$comp"
}

