# ------------------------------------------------------------------------------ # Bash completion template for {{BIN_NAME}} # ------------------------------------------------------------------------------ # Installation: # eval "$({{BIN_NAME}} completions bash)" # # This file is generated from a template. Placeholders (for example `{{OPTIONS}}`) # are replaced at build/runtime with concrete command data from the CLI. # ------------------------------------------------------------------------------ # Prefer a globally-installed helper, but fall back to a helper co-located with # the CLI binary. This lets completions work in both "installed via PATH" and # "single extracted directory" workflows. __xo_complete_bin="" if command -v xo-complete &>/dev/null; then __xo_complete_bin="xo-complete" elif command -v {{BIN_NAME}} &>/dev/null; then __xo_complete_bin="$(dirname "$(command -v {{BIN_NAME}})")/xo-complete" fi # @description # Calls the dynamic completion helper and suppresses helper stderr so the shell # completion menu stays clean even when the helper is unavailable or errors. # @param "$@" Arguments forwarded to xo-complete. __xo_complete() { [[ -n "${__xo_complete_bin}" ]] && "${__xo_complete_bin}" "$@" 2>/dev/null } # @description # Lists mnemonic aliases directly from the config directory without starting # the dynamic Node helper. __xo_complete_mnemonics() { local config_dir="${XO_CONFIG_DIR:-${HOME}/.config/xo-cli}" local file mnemonic for file in "${config_dir}"/mnemonics/mnemonic-*; do [[ -f "${file}" ]] || continue mnemonic="${file##*/}" [[ "${mnemonic}" == "$1"* ]] && printf '%s\n' "${mnemonic}" done } # @description # Main completion dispatcher invoked by bash's `complete -F`. # It determines context (command/subcommand/argument position) and then mixes: # - static completions (known command words) # - dynamic completions (resolved by xo-complete) # - filesystem completions (when a subcommand expects file paths) _{{FUNC_NAME}}_completions() { local cur prev words cword # Populates `cur`, `prev`, `words`, and `cword`. # `_init_completion` is provided by bash-completion. _init_completion || return # If the previous token is `-m/--mnemonic-file`, this argument expects a # mnemonic file alias/path. List mnemonic aliases directly from disk. if [[ "${prev}" == "-m" || "${prev}" == "--mnemonic-file" ]]; then local mnemonics mnemonics=$(__xo_complete_mnemonics "${cur}") if [[ -n "${mnemonics}" ]]; then while IFS= read -r line; do COMPREPLY+=("$line") done <<< "${mnemonics}" return 0 fi fi # Option context: show global options when the current token starts with `-`. if [[ "${cur}" == -* ]]; then COMPREPLY=($(compgen -W "{{OPTIONS}}" -- "${cur}")) return 0 fi # Parse command/subcommand from non-option tokens before the current cursor. # We track indices so argument-position logic can be computed later. local cmd="" subcmd="" cmd_idx=0 subcmd_idx=0 for ((i=1; i < cword; i++)); do if [[ "${words[i]}" != -* ]]; then if [[ -z "${cmd}" ]]; then cmd="${words[i]}" cmd_idx=$i else subcmd="${words[i]}" subcmd_idx=$i break fi fi done # No command selected yet: complete top-level commands. if [[ -z "${cmd}" ]]; then COMPREPLY=($(compgen -W "{{COMMANDS}}" -- "${cur}")) return 0 fi # Command-specific completion rules. case "${cmd}" in mnemonic) if [[ -z "${subcmd}" ]]; then COMPREPLY=($(compgen -W "{{MNEMONIC_SUBS}}" -- "${cur}")) fi ;; template) if [[ -z "${subcmd}" ]]; then COMPREPLY=($(compgen -W "{{TEMPLATE_SUBS}}" -- "${cur}")) elif [[ "${subcmd}" == "list" || "${subcmd}" == "inspect" ]]; then # template list/inspect