build: support auto-completion for bash

This commit is contained in:
Daniel Liu 2025-04-06 18:47:37 +08:00 committed by Daniel Liu
parent 993d08cc3a
commit a6cd3c54eb
3 changed files with 76 additions and 0 deletions

View file

@ -0,0 +1,35 @@
#!/bin/bash
: ${PROG:=$(basename ${BASH_SOURCE})}
# Macs have bash3 for which the bash-completion package doesn't include
# _init_completion. This is a minimal version of that function.
_cli_init_completion() {
COMPREPLY=()
_get_comp_words_by_ref "$@" cur prev words cword
}
_cli_bash_autocomplete() {
if [[ "${COMP_WORDS[0]}" != "source" ]]; then
local cur opts base words
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
if declare -F _init_completion >/dev/null 2>&1; then
_init_completion -n "=:" || return
else
_cli_init_completion -n "=:" || return
fi
words=("${words[@]:0:$cword}")
if [[ "$cur" == "-"* ]]; then
requestComp="${words[*]} ${cur} --generate-bash-completion"
else
requestComp="${words[*]} --generate-bash-completion"
fi
opts=$(eval "${requestComp}" 2>/dev/null)
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
return 0
fi
}
complete -o bashdefault -o default -o nospace -F _cli_bash_autocomplete $PROG
unset PROG

View file

@ -0,0 +1,21 @@
#!/bin/bash
set -eo pipefail
PROFILE="${HOME}/.bashrc"
SRC_DIR="$(dirname "$0")"
DEST_DIR="${HOME}/.bash_completion.d"
XDC_PATH="\${HOME}/XDPoSChain/build/bin"
CMD_STR="PROG=XDC source \${HOME}/.bash_completion.d/xdc.sh"
mkdir -p ${DEST_DIR}
cp ${SRC_DIR}/bash_autocomplete "${DEST_DIR}/xdc.sh"
cp ${SRC_DIR}/zsh_autocomplete "${DEST_DIR}/xdc.zsh"
if ! grep PATH ${PROFILE} | grep ${XDC_PATH} &>/dev/null; then
echo "export PATH=\${PATH}:${XDC_PATH}" >>"${PROFILE}"
fi
if ! grep -q "${CMD_STR}" "${PROFILE}"; then
echo "${CMD_STR}" >>"${PROFILE}"
fi
echo "please login again or run: source \${HOME}/.bashrc"

View file

@ -0,0 +1,20 @@
#compdef $PROG
_cli_zsh_autocomplete() {
local -a opts
local cur
cur=${words[-1]}
if [[ "$cur" == "-"* ]]; then
opts=("${(@f)$(${words[@]:0:#words[@]-1} ${cur} --generate-bash-completion)}")
else
opts=("${(@f)$(${words[@]:0:#words[@]-1} --generate-bash-completion)}")
fi
if [[ "${opts[1]}" != "" ]]; then
_describe 'values' opts
else
_files
fi
}
compdef _cli_zsh_autocomplete $PROG