---
source: lib/src/complete/bash.rs
expression: "complete_bash_init(\"usage\")"
---
# @generated by usage-cli — auto-completion for usage shebang scripts
# Source this file from your bashrc to enable <Tab> completion for any command
# on $PATH whose first line is a `usage` shebang.
#
# Sourcing order: this script registers a `complete -D` default handler. Bash
# only allows one such handler — whichever script registers last wins. Source
# this AFTER bash-completion (e.g. `/etc/bash_completion` or
# `/usr/share/bash-completion/bash_completion`) so that bash-completion's own
# default handler is captured below and chained to for non-usage commands.

# Capture any pre-existing `complete -D` handler so we can chain to it for
# commands that aren't usage shebang scripts (e.g. bash-completion's loader).
# `complete -p -D` exits non-zero if no -D handler is set; tolerate that under
# `set -e`.
_usage_chained_default_complete=""
{
    _usage_existing_d="$(complete -p -D 2>/dev/null || true)"
    if [[ "$_usage_existing_d" =~ -F[[:space:]]+([^[:space:]]+) ]]; then
        _usage_chained_default_complete="${BASH_REMATCH[1]}"
    fi
    unset _usage_existing_d
}

_usage_default_complete() {
    local cmd="${COMP_WORDS[0]}"
    local cmdpath
    if [[ "$cmd" == */* ]]; then
        cmdpath="$cmd"
    else
        cmdpath="$(type -P "$cmd" 2>/dev/null)"
    fi

    if [[ -n "$cmdpath" && -f "$cmdpath" ]]; then
        local first
        if IFS= read -r first < "$cmdpath" 2>/dev/null && [[ "$first" == "#!"*"usage"* ]]; then
            if type -P usage &> /dev/null; then
                local IFS=$'\n'
                # shellcheck disable=SC2207
                COMPREPLY=( $(command usage complete-word --shell bash -f "$cmdpath" --cword="$COMP_CWORD" -- "${COMP_WORDS[@]}") )
                return 0
            fi
        fi
    fi

    # Chain to whatever default handler was registered before us, if any.
    if [[ -n "$_usage_chained_default_complete" ]] \
        && [[ "$_usage_chained_default_complete" != "_usage_default_complete" ]] \
        && declare -F "$_usage_chained_default_complete" >/dev/null 2>&1; then
        "$_usage_chained_default_complete" "$@"
        return $?
    fi
    if declare -F _completion_loader >/dev/null 2>&1; then
        _completion_loader "$@"
        return $?
    fi
    return 1
}

complete -D -F _usage_default_complete -o bashdefault -o default
# vim: noet ci pi sts=0 sw=4 ts=4 ft=sh
