#compdef env

autoload -U is-at-least

_env() {
    typeset -A opt_args
    typeset -a _arguments_options
    local ret=1

    if is-at-least 5.2; then
        _arguments_options=(-s -S -C)
    else
        _arguments_options=(-s -C)
    fi

    local context curcontext="$curcontext" state line
    _arguments "${_arguments_options[@]}" : \
'-C+[change working directory to DIR]:DIR:_files -/' \
'--chdir=[change working directory to DIR]:DIR:_files -/' \
'*-f+[read and set variables from a ".env"-style configuration file (prior to any unset and/or set)]:PATH:_files' \
'*--file=[read and set variables from a ".env"-style configuration file (prior to any unset and/or set)]:PATH:_files' \
'*-u+[remove variable from the environment]:NAME:_default' \
'*--unset=[remove variable from the environment]:NAME:_default' \
'-S+[process and split S into separate arguments; used to pass multiple arguments on shebang lines]:S:_default' \
'--split-string=[process and split S into separate arguments; used to pass multiple arguments on shebang lines]:S:_default' \
'-a+[Override the zeroth argument passed to the command being executed. Without this option a default value of \`command\` is used.]:a:_default' \
'--argv0=[Override the zeroth argument passed to the command being executed. Without this option a default value of \`command\` is used.]:a:_default' \
'*--ignore-signal=[set handling of SIG signal(s) to do nothing]:SIG:_default' \
'-i[start with an empty environment]' \
'--ignore-environment[start with an empty environment]' \
'-0[end each output line with a 0 byte rather than a newline (only valid when printing the environment)]' \
'--null[end each output line with a 0 byte rather than a newline (only valid when printing the environment)]' \
'*-v[print verbose information for each processing step]' \
'*--debug[print verbose information for each processing step]' \
'-h[Print help]' \
'--help[Print help]' \
'-V[Print version]' \
'--version[Print version]' \
'::vars:_default' \
&& ret=0
}

(( $+functions[_env_commands] )) ||
_env_commands() {
    local commands; commands=()
    _describe -t commands 'env commands' commands "$@"
}

if [ "$funcstack[1]" = "_env" ]; then
    _env "$@"
else
    compdef _env env
fi
