#compdef aur

zstyle ':completion::complete:aur:argument-rest:' sort false

_aur() {
    local context state state_descr line
    typeset -A opt_args

    typeset -A default_cmds

    default_cmds[build]='--arg-file: --bind-rw: --bind: --buildscript: --checkpkg --chroot --clean --database: --directory: --force --ignore-arch --log --makepkg-args: --makepkg-conf: --namcap --new --no-check --no-confirm --no-sync --pacman-conf: --pkgver --prevent-downgrade --remove --repo: --results: --rmdeps --root: --sign --syncdeps --temp --user: --verify -A -C -D: -L -N -R -S -T -U: -a: -c -d: -f -n -r -s -v'
    default_cmds[chroot]='--bind-rw: --bind: --build --buildscript: --create --directory: --makepkg-conf: --packagelist --pacman-conf: --suffix: --update -B -C: -D: -M: -U -x:'
    default_cmds[depends]='--no-checkdepends --no-depends --no-makedepends --pkgbase --pkgname --pkgname-all --table -a -b -n -t'
    default_cmds[fetch]='--existing --recurse --results: --sync: -S -r'
    default_cmds[pkglist]='--fixed-strings --info --perl-regexp --pkgbase --plain --search --time: -F -I -P -S -b -t:'
    default_cmds[repo]='--all --config: --database: --list --path --path-list --quiet --repo-list --repo: --root: --status-file: --sync --table --upgrades -S -a -c: -d: -l -q -r: -t -u'
    default_cmds[repo-filter]='--all --database: --repo: --sync --sysroot: -a -d:'
    default_cmds[query]='--by: --type: -b: -t:'
    default_cmds[search]='--any --checkdepends --depends --desc --info --key: --maintainer --makedepends --name --optdepends --raw --search --short --table --verbose -a -d -i -k: -m -n -q -r -s -v'
    default_cmds[srcver]='--jobs: --no-prepare -j:'
    default_cmds[sync]='--bind-rw: --bind: --chroot --continue --database: --directory: --force --format: --ignore-arch --ignore-file: --ignore: --log --makepkg-args: --makepkg-conf: --new --no-build --no-check --no-confirm --no-graph --no-provides --no-ver --no-ver-argv --no-view --pacman-conf: --pkgver --prevent-downgrade --provides-from: --rebuild --rebuild-all --rebuild-tree --remove --repo: --results: --rm-deps --root: --sign --temp --upgrades --verify -A -D: -L -R -S -T -c -d: -f -n -o -r -u -v'
    default_cmds[vercmp]='--all --current --path: --quiet --upair: -a -c -p: -q -u:'
    default_cmds[graph]=''

    _arguments -C \
        '1: :(${(k)default_cmds})' \
        '2: :(${(@s/ /)default_cmds[${words[2]}]})' \
        '*: :->options'

    case $state in
        options)
            if [[ ${default_cmds[${words[2]}]} != *${words[-2]}:* ]]; then
                _arguments '*: :(${=default_cmds[${words[2]}]//:/})'
            else
               _files
            fi ;;
    esac
}
