#compdef sudo

autoload -U is-at-least

_sudo() {
    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+[diverging from sudo, this sets NOFILE limit, achieving similar behavior as sudo explicitly watching and killing file descriptors]:FILE_DESCRIPTOR_LIMIT:_default' \
'--close-from=[diverging from sudo, this sets NOFILE limit, achieving similar behavior as sudo explicitly watching and killing file descriptors]:FILE_DESCRIPTOR_LIMIT:_default' \
'-D+[change the working directory before running command]:WORKING_DIRECTORY:_default' \
'--chdir=[change the working directory before running command]:WORKING_DIRECTORY:_default' \
'*-E+[preserve user environment when running command. If no explicit list of environment variables is supplied, preserves all variables except a narrow blocklist. This is considered insecure and a warning will be emitted]::PRESERVE_ENV:_default' \
'*--preserve-env=[preserve user environment when running command. If no explicit list of environment variables is supplied, preserves all variables except a narrow blocklist. This is considered insecure and a warning will be emitted]::PRESERVE_ENV:_default' \
'-g+[run command as the specified group name or ID]:GROUP:_default' \
'--group=[run command as the specified group name or ID]:GROUP:_default' \
'--host=[\[UNSUPPORTED\] run command on host (if supported by plugin)]:HOST:_default' \
'-p+[\[IGNORED\] use the specified password prompt]:PROMPT:_default' \
'--prompt=[\[IGNORED\] use the specified password prompt]:PROMPT:_default' \
'-R+[\[UNSUPPORTED\] change the root directory before running command]:CHROOT:_default' \
'--chroot=[\[UNSUPPORTED\] change the root directory before running command]:CHROOT:_default' \
'-T+[terminate command after the specified time limit]:COMMAND_TIMEOUT:_default' \
'--command-timeout=[terminate command after the specified time limit]:COMMAND_TIMEOUT:_default' \
'-U+[\[UNSUPPORTED\] in list mode, display privileges for user]:OTHER_USER:_users' \
'--other-user=[\[UNSUPPORTED\] in list mode, display privileges for user]:OTHER_USER:_users' \
'-u+[run command (or edit file) as specified user name or ID]:USER:_users' \
'--user=[run command (or edit file) as specified user name or ID]:USER:_users' \
'*--run0-extra-arg=[an extra argument to pass to run0 (can be specified multiple times)]:RUN0_EXTRA_ARGS:_default' \
'-A[\[IGNORED\] use a helper program for password prompting]' \
'--askpass[\[IGNORED\] use a helper program for password prompting]' \
'-b[\[UNSUPPORTED\] run command in the background]' \
'--background[\[UNSUPPORTED\] run command in the background]' \
'-B[ring bell when prompting]' \
'--bell[ring bell when prompting]' \
'-e[\[UNSUPPORTED\] edit files instead of running a command]' \
'--edit[\[UNSUPPORTED\] edit files instead of running a command]' \
'-H[set HOME variable to target user'\''s home dir]' \
'--set-home[set HOME variable to target user'\''s home dir]' \
'-i[run login shell as the target user; a command may also be specified]' \
'--login[run login shell as the target user; a command may also be specified]' \
'-K[\[UNSUPPORTED\] remove timestamp file completely]' \
'--remove-timestamp[\[UNSUPPORTED\] remove timestamp file completely]' \
'-k[\[UNSUPPORTED\] invalidate timestamp file]' \
'--reset-timestamp[\[UNSUPPORTED\] invalidate timestamp file]' \
'*-l[\[UNSUPPORTED\] list user'\''s privileges or check a specific command; use twice for longer format]' \
'*--list[\[UNSUPPORTED\] list user'\''s privileges or check a specific command; use twice for longer format]' \
'-n[non-interactive mode, no prompts are used]' \
'--non-interactive[non-interactive mode, no prompts are used]' \
'-P[\[UNSUPPORTED\] preserve group vector instead of setting to target'\''s]' \
'--preserve-groups[\[UNSUPPORTED\] preserve group vector instead of setting to target'\''s]' \
'-S[read password from standard input]' \
'--stdin[read password from standard input]' \
'-s[run shell as the target user; a command may also be specified]' \
'--shell[run shell as the target user; a command may also be specified]' \
'-v[validate a root login]' \
'--validate[validate a root login]' \
'-h[Print help]' \
'--help[Print help]' \
'-V[Print version]' \
'--version[Print version]' \
'*::command -- command to be executed:_cmdambivalent' \
&& ret=0
}

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

if [ "$funcstack[1]" = "_sudo" ]; then
    _sudo "$@"
else
    compdef _sudo sudo
fi
