#compdef bear

autoload -U is-at-least

_bear() {
    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+[Path of the config file]:FILE:_default' \
'--config=[Path of the config file]:FILE:_default' \
'-o+[Path of the result file]:FILE:_default' \
'--output=[Path of the result file]:FILE:_default' \
'-a[Append result to an existing output file]' \
'--append[Append result to an existing output file]' \
'-h[Print help]' \
'--help[Print help]' \
'-V[Print version]' \
'--version[Print version]' \
':BUILD_COMMAND -- Build command:_default' \
":: :_bear_commands" \
"*::: :->bear" \
&& ret=0
    case $state in
    (bear)
        words=($line[2] "${words[@]}")
        (( CURRENT += 1 ))
        curcontext="${curcontext%:*:*}:bear-command-$line[2]:"
        case $line[2] in
            (intercept)
_arguments "${_arguments_options[@]}" : \
'-o+[Path of the event file]:FILE:_default' \
'--output=[Path of the event file]:FILE:_default' \
'-h[Print help]' \
'--help[Print help]' \
'*--::BUILD_COMMAND -- Build command:_default' \
&& ret=0
;;
(semantic)
_arguments "${_arguments_options[@]}" : \
'-i+[Path of the event file]:FILE:_default' \
'--input=[Path of the event file]:FILE:_default' \
'-o+[Path of the result file]:FILE:_default' \
'--output=[Path of the result file]:FILE:_default' \
'-a[Append result to an existing output file]' \
'--append[Append result to an existing output file]' \
'-h[Print help]' \
'--help[Print help]' \
&& ret=0
;;
(help)
_arguments "${_arguments_options[@]}" : \
":: :_bear__subcmd__help_commands" \
"*::: :->help" \
&& ret=0

    case $state in
    (help)
        words=($line[1] "${words[@]}")
        (( CURRENT += 1 ))
        curcontext="${curcontext%:*:*}:bear-help-command-$line[1]:"
        case $line[1] in
            (intercept)
_arguments "${_arguments_options[@]}" : \
&& ret=0
;;
(semantic)
_arguments "${_arguments_options[@]}" : \
&& ret=0
;;
(help)
_arguments "${_arguments_options[@]}" : \
&& ret=0
;;
        esac
    ;;
esac
;;
        esac
    ;;
esac
}

(( $+functions[_bear_commands] )) ||
_bear_commands() {
    local commands; commands=(
'intercept:intercepts command execution' \
'semantic:detect semantics of command executions' \
'help:Print this message or the help of the given subcommand(s)' \
    )
    _describe -t commands 'bear commands' commands "$@"
}
(( $+functions[_bear__subcmd__help_commands] )) ||
_bear__subcmd__help_commands() {
    local commands; commands=(
'intercept:intercepts command execution' \
'semantic:detect semantics of command executions' \
'help:Print this message or the help of the given subcommand(s)' \
    )
    _describe -t commands 'bear help commands' commands "$@"
}
(( $+functions[_bear__subcmd__help__subcmd__help_commands] )) ||
_bear__subcmd__help__subcmd__help_commands() {
    local commands; commands=()
    _describe -t commands 'bear help help commands' commands "$@"
}
(( $+functions[_bear__subcmd__help__subcmd__intercept_commands] )) ||
_bear__subcmd__help__subcmd__intercept_commands() {
    local commands; commands=()
    _describe -t commands 'bear help intercept commands' commands "$@"
}
(( $+functions[_bear__subcmd__help__subcmd__semantic_commands] )) ||
_bear__subcmd__help__subcmd__semantic_commands() {
    local commands; commands=()
    _describe -t commands 'bear help semantic commands' commands "$@"
}
(( $+functions[_bear__subcmd__intercept_commands] )) ||
_bear__subcmd__intercept_commands() {
    local commands; commands=()
    _describe -t commands 'bear intercept commands' commands "$@"
}
(( $+functions[_bear__subcmd__semantic_commands] )) ||
_bear__subcmd__semantic_commands() {
    local commands; commands=()
    _describe -t commands 'bear semantic commands' commands "$@"
}

if [ "$funcstack[1]" = "_bear" ]; then
    _bear "$@"
else
    compdef _bear bear
fi
