_herdr_complete() {
    local IFS=$'\013'
    local cur=$2
    local line=${COMP_LINE:0:COMP_POINT}
    local -a words=()
    local glue=0 i word rest
    for ((i = 0; i <= COMP_CWORD; i++)); do
        rest=${line#"${line%%[![:space:]]*}"}
        if ((i == COMP_CWORD)); then
            word=$rest
        else
            word=${COMP_WORDS[i]}
        fi
        if ((i > 0 && ${#rest} == ${#line})) && { ((glue)) || [[ $word =~ ^[:=]+$ ]]; }; then
            words[${#words[@]} - 1]+=$word
        else
            words+=("$word")
        fi
        line=${rest#"$word"}
        [[ $word =~ ^[:=]+$ ]] && glue=1 || glue=0
    done
    local cword=$((${#words[@]} - 1))
    local space=true
    if compopt +o nospace 2> /dev/null; then
        space=false
    fi
    COMPREPLY=( $( \
        _CLAP_IFS="$IFS" \
        _CLAP_COMPLETE_INDEX="$cword" \
        _CLAP_COMPLETE_COMP_TYPE="$COMP_TYPE" \
        _CLAP_COMPLETE_SPACE="$space" \
        HERDR_COMPLETE="bash" \
        'herdr' -- "${words[@]}" 2> /dev/null \
    ) )
    if [[ $? != 0 ]]; then
        unset COMPREPLY
        return
    fi
    local prefix=${words[cword]%"$cur"}
    if [[ -n $prefix ]]; then
        for i in "${!COMPREPLY[@]}"; do
            COMPREPLY[i]=${COMPREPLY[i]#"$prefix"}
        done
    fi
    if [[ $space == false && ${COMPREPLY-} =~ [=/:]$ ]]; then
        compopt -o nospace
    fi
}
if [[ ${BASH_VERSINFO[0]} -eq 4 && ${BASH_VERSINFO[1]} -ge 4 || ${BASH_VERSINFO[0]} -gt 4 ]]; then
    complete -o nospace -o nosort -F _herdr_complete herdr
else
    complete -o nospace -F _herdr_complete herdr
fi
