Files
xo-cli/src/cli/autocomplete/scripts/bash.sh

254 lines
8.8 KiB
Bash

# ------------------------------------------------------------------------------
# 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 <category> <template> [field]
# Position is computed relative to the subcommand token:
# 1 => category, 2 => template, 3 => field (inspect only)
local pos=$((cword - subcmd_idx))
if [[ $pos -eq 1 ]]; then
COMPREPLY=($(compgen -W "action transaction output lockingscript variable" -- "${cur}"))
elif [[ $pos -eq 2 ]]; then
local templates
templates=$(__xo_complete templates "${cur}")
if [[ -n "${templates}" ]]; then
while IFS= read -r line; do
COMPREPLY+=("$line")
done <<< "${templates}"
fi
elif [[ $pos -eq 3 && "${subcmd}" == "inspect" ]]; then
# Field names depend on both selected category and template.
local category="${words[subcmd_idx + 1]}"
local template_arg="${words[subcmd_idx + 2]}"
local fields
fields=$(__xo_complete fields "${category}" "${template_arg}" "${cur}")
if [[ -n "${fields}" ]]; then
while IFS= read -r line; do
COMPREPLY+=("$line")
done <<< "${fields}"
fi
fi
elif [[ "${subcmd}" == "set-default" ]]; then
# template set-default <template> <output> <role>
# We only complete the first positional argument (template) here.
local pos=$((cword - subcmd_idx))
if [[ $pos -eq 1 ]]; then
local templates
templates=$(__xo_complete templates "${cur}")
if [[ -n "${templates}" ]]; then
while IFS= read -r line; do
COMPREPLY+=("$line")
done <<< "${templates}"
fi
fi
fi
;;
invitation)
if [[ -z "${subcmd}" ]]; then
COMPREPLY=($(compgen -W "{{INVITATION_SUBS}}" -- "${cur}"))
else
case "${subcmd}" in
create)
# invitation create <template> <action>
# The available actions depend on the selected template.
local pos=$((cword - subcmd_idx))
if [[ $pos -eq 1 ]]; then
local templates
templates=$(__xo_complete templates "${cur}")
if [[ -n "${templates}" ]]; then
while IFS= read -r line; do
COMPREPLY+=("$line")
done <<< "${templates}"
fi
elif [[ $pos -eq 2 ]]; then
local template_arg="${words[subcmd_idx + 1]}"
local actions
actions=$(__xo_complete actions "${template_arg}" "${cur}")
if [[ -n "${actions}" ]]; then
while IFS= read -r line; do
COMPREPLY+=("$line")
done <<< "${actions}"
fi
fi
;;
append|sign|broadcast|requirements|export|inspect)
# These subcommands expect an invitation identifier as first arg.
local pos=$((cword - subcmd_idx))
if [[ $pos -eq 1 ]]; then
local invitations
invitations=$(__xo_complete invitations "${cur}")
if [[ -n "${invitations}" ]]; then
while IFS= read -r line; do
COMPREPLY+=("$line")
done <<< "${invitations}"
fi
fi
;;
import)
# File import path: delegate to bash's built-in file completion.
COMPREPLY=($(compgen -f -- "${cur}"))
;;
esac
fi
;;
resource)
if [[ -z "${subcmd}" ]]; then
COMPREPLY=($(compgen -W "{{RESOURCE_SUBS}}" -- "${cur}"))
elif [[ "${subcmd}" == "unreserve" ]]; then
# resource unreserve <txhash:vout>
# Suggest known reserved outpoints from the helper.
local pos=$((cword - subcmd_idx))
if [[ $pos -eq 1 ]]; then
local resources
resources=$(__xo_complete resources "${cur}")
if [[ -n "${resources}" ]]; then
while IFS= read -r line; do
COMPREPLY+=("$line")
done <<< "${resources}"
fi
fi
fi
;;
settings)
if [[ -z "${subcmd}" ]]; then
COMPREPLY=($(compgen -W "show get set" -- "${cur}"))
elif [[ "${subcmd}" == "get" || "${subcmd}" == "set" ]]; then
local pos=$((cword - subcmd_idx))
if [[ $pos -eq 1 ]]; then
COMPREPLY=($(compgen -W "currency default-mnemonic" -- "${cur}"))
fi
fi
;;
receive)
# receive <template> [output]
# Template is the first positional argument after `receive`.
local pos=$((cword - cmd_idx))
if [[ $pos -eq 1 ]]; then
local templates
templates=$(__xo_complete templates "${cur}")
if [[ -n "${templates}" ]]; then
while IFS= read -r line; do
COMPREPLY+=("$line")
done <<< "${templates}"
fi
fi
;;
completions)
# Shell target for generating completion scripts.
if [[ -z "${subcmd}" ]]; then
COMPREPLY=($(compgen -W "bash zsh fish" -- "${cur}"))
fi
;;
esac
}
# Register the completion function for the CLI binary.
complete -F _{{FUNC_NAME}}_completions {{BIN_NAME}}