#compdef stdbuf

autoload -U is-at-least

_stdbuf() {
    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[@]}" : \
'-i+[stdbuf-help-input]:MODE:_default' \
'--input=[stdbuf-help-input]:MODE:_default' \
'-o+[stdbuf-help-output]:MODE:_default' \
'--output=[stdbuf-help-output]:MODE:_default' \
'-e+[stdbuf-help-error]:MODE:_default' \
'--error=[stdbuf-help-error]:MODE:_default' \
'-h[Print help]' \
'--help[Print help]' \
'-V[Print version]' \
'--version[Print version]' \
':command:_command_names -e' \
&& ret=0
}

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

if [ "$funcstack[1]" = "_stdbuf" ]; then
    _stdbuf "$@"
else
    compdef _stdbuf stdbuf
fi
