#compdef mml

autoload -U is-at-least

_mml() {
    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[@]}" \
'-h[Print help]' \
'--help[Print help]' \
'-V[Print version]' \
'--version[Print version]' \
":: :_mml_commands" \
"*::: :->mml" \
&& ret=0
    case $state in
    (mml)
        words=($line[1] "${words[@]}")
        (( CURRENT += 1 ))
        curcontext="${curcontext%:*:*}:mml-command-$line[1]:"
        case $line[1] in
            (completion)
_arguments "${_arguments_options[@]}" \
'-h[Print help]' \
'--help[Print help]' \
'-V[Print version]' \
'--version[Print version]' \
':shell:(bash elvish fish powershell zsh)' \
&& ret=0
;;
(man)
_arguments "${_arguments_options[@]}" \
'-h[Print help (see more with '\''--help'\'')]' \
'--help[Print help (see more with '\''--help'\'')]' \
'-V[Print version]' \
'--version[Print version]' \
':dir -- Directory in which to generate man files:_files' \
&& ret=0
;;
(compile)
_arguments "${_arguments_options[@]}" \
'-h[Print help]' \
'--help[Print help]' \
'-V[Print version]' \
'--version[Print version]' \
'::mml -- Read the mssage from the given file path:' \
&& ret=0
;;
(interpret)
_arguments "${_arguments_options[@]}" \
'(--exclude-header)*--include-header=[Include header to the interpreted message]:HEADER: ' \
'(--include-header)*--exclude-header=[Exclude header from the interpreted message]:HEADER: ' \
'(--exclude-part)*--include-part=[Include parts to intepret by their MIME type]:MIME: ' \
'(--include-part)*--exclude-part=[Exclude parts to interpret by their MIME type]:MIME: ' \
'--save-attachments-dir=[Define the directory attachments should point to]:DIR:_files' \
'--show-multiparts[Enable interpretation of multiparts]' \
'--save-attachments[Save automatically attachments found in the original MIME message to the \`save_attachments_dir\` directory]' \
'--hide-attachments[If true, disable interpretation of all attachments]' \
'--hide-inline-attachments[If true, disable interpretation of attachments with a content disposition set to inline]' \
'--hide-plain-texts-signature[If true, trim out signatures from text bodies]' \
'-h[Print help (see more with '\''--help'\'')]' \
'--help[Print help (see more with '\''--help'\'')]' \
'-V[Print version]' \
'--version[Print version]' \
'::mime -- Read the MIME message from the given file path:' \
&& ret=0
;;
(help)
_arguments "${_arguments_options[@]}" \
":: :_mml__help_commands" \
"*::: :->help" \
&& ret=0

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

(( $+functions[_mml_commands] )) ||
_mml_commands() {
    local commands; commands=(
'completion:Generate the completion script for the given shell' \
'man:Generate all man pages to the given directory' \
'compile:Compile the given MML message to a valid MIME message' \
'interpret:Interpret the given MIME message as a MML message' \
'help:Print this message or the help of the given subcommand(s)' \
    )
    _describe -t commands 'mml commands' commands "$@"
}
(( $+functions[_mml__compile_commands] )) ||
_mml__compile_commands() {
    local commands; commands=()
    _describe -t commands 'mml compile commands' commands "$@"
}
(( $+functions[_mml__help__compile_commands] )) ||
_mml__help__compile_commands() {
    local commands; commands=()
    _describe -t commands 'mml help compile commands' commands "$@"
}
(( $+functions[_mml__completion_commands] )) ||
_mml__completion_commands() {
    local commands; commands=()
    _describe -t commands 'mml completion commands' commands "$@"
}
(( $+functions[_mml__help__completion_commands] )) ||
_mml__help__completion_commands() {
    local commands; commands=()
    _describe -t commands 'mml help completion commands' commands "$@"
}
(( $+functions[_mml__help_commands] )) ||
_mml__help_commands() {
    local commands; commands=(
'completion:Generate the completion script for the given shell' \
'man:Generate all man pages to the given directory' \
'compile:Compile the given MML message to a valid MIME message' \
'interpret:Interpret the given MIME message as a MML message' \
'help:Print this message or the help of the given subcommand(s)' \
    )
    _describe -t commands 'mml help commands' commands "$@"
}
(( $+functions[_mml__help__help_commands] )) ||
_mml__help__help_commands() {
    local commands; commands=()
    _describe -t commands 'mml help help commands' commands "$@"
}
(( $+functions[_mml__help__interpret_commands] )) ||
_mml__help__interpret_commands() {
    local commands; commands=()
    _describe -t commands 'mml help interpret commands' commands "$@"
}
(( $+functions[_mml__interpret_commands] )) ||
_mml__interpret_commands() {
    local commands; commands=()
    _describe -t commands 'mml interpret commands' commands "$@"
}
(( $+functions[_mml__help__man_commands] )) ||
_mml__help__man_commands() {
    local commands; commands=()
    _describe -t commands 'mml help man commands' commands "$@"
}
(( $+functions[_mml__man_commands] )) ||
_mml__man_commands() {
    local commands; commands=()
    _describe -t commands 'mml man commands' commands "$@"
}

if [ "$funcstack[1]" = "_mml" ]; then
    _mml "$@"
else
    compdef _mml mml
fi
