_cargo-sonar() {
    local i cur prev opts cmd
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    cmd=""
    opts=""

    for i in ${COMP_WORDS[@]}
    do
        case "${cmd},${i}" in
            ",$1")
                cmd="cargo__sonar"
                ;;
            *)
                ;;
        esac
    done

    case "${cmd}" in
        cargo__sonar)
            opts="-h -V --audit --audit-path --clippy --clippy-path --deny --deny-path --outdated --outdated-path --udeps --udeps-path --sonar-path --codeclimate-path --completion --help --version"
            if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
                COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
                return 0
            fi
            case "${prev}" in
                --audit-path)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --clippy-path)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --deny-path)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --outdated-path)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --udeps-path)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --sonar-path)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --codeclimate-path)
                    COMPREPLY=($(compgen -f "${cur}"))
                    return 0
                    ;;
                --completion)
                    COMPREPLY=($(compgen -W "bash elvish fish nushell power-shell zsh" -- "${cur}"))
                    return 0
                    ;;
                *)
                    COMPREPLY=()
                    ;;
            esac
            COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
            return 0
            ;;
    esac
}

if [[ "${BASH_VERSINFO[0]}" -eq 4 && "${BASH_VERSINFO[1]}" -ge 4 || "${BASH_VERSINFO[0]}" -gt 4 ]]; then
    complete -F _cargo-sonar -o nosort -o bashdefault -o default cargo-sonar
else
    complete -F _cargo-sonar -o bashdefault -o default cargo-sonar
fi
