[英](Git)Bash programmable completion with short '-...' and long '--...' options
I created a completion function inspired by How to Create a Custom Bash Completion Script (which has some obvious errors but I corrected these) and in a newer version on GitHub .我创建了一个完成函数,其灵感来自如何创建自定义 Bash 完成脚本(它有一些明显的错误,但我更正了这些错误)和GitHub 上的更新版本。
bash_completion_test
#!/bin/bash
#
# bash_completion_test
#
# from https://maskedbyte.com/how-to-create-a-custom-bash-completion-script/
# and https://github.com/SumuduLansakara/custom-bash-completion-script/blob/master/completer.sh
#
echo 'Begin bash_completion_test...'
# -------------------------------
# Generic hierarchical completion
# test definitions
_args=(add list remove)
list_args=(student teacher)
list_student_args=(age grade name)
list_student_grade_args=(-a -o --another --option)
list_teacher_args=(name)
function generic_hierarchical_completion_test() {
local arguments word_list_ptr word DEBUG
log='bash_completion_test.log'
echo -n '␃🖕' > $log # clear log file
DEBUG= #true # set variable for debugging info in log file
[[ $DEBUG ]] && echo -n > $log
# Array of individual words in the current command line
# see https://www.gnu.org/software/bash/manual/bash.html#index-COMP_005fWORDS
[[ $DEBUG ]] && echo "COMP_WORDS=${COMP_WORDS[@]}" >> $log
# Second word to last word in the current command line
arguments=("${COMP_WORDS[@]:1}")
[[ $DEBUG ]] && echo "arguments=${#arguments[@]}:${arguments[@]}" >> $log
# Remove last empty word from arguments
[[ "${#COMP_WORDS[@]}" -gt 1 ]] && unset 'arguments[${#arguments[@]}-1]'
[[ $DEBUG ]] && echo "arguments=${#arguments[@]}:${arguments[@]}" >> $log
# Create word list pointer variable from arguments while replacing spaces with '_'
IFS=_
word_list_ptr="${arguments[*]}_args[*]"
unset IFS
[[ $DEBUG ]] && echo "word_list_ptr=$word_list_ptr" >> $log
[[ $DEBUG ]] && echo "word_list=${!word_list_ptr}" >> $log
# -W <word list from variable indirected by word_list_ptr>
# COMP_CWORD: An index into ${COMP_WORDS} of the word containing the current cursor position.
# see https://www.gnu.org/software/bash/manual/bash.html#index-COMP_005fCWORD
word=${COMP_WORDS[${COMP_CWORD}]}
[[ $DEBUG ]] && echo "word=$word" >> $log
COMPREPLY=( $( compgen -W "${!word_list_ptr}" "$word" ) )
}
complete -F generic_hierarchical_completion_test ct
echo 'End bash_completion_test.'
ct
#!/bin/bash
echo ">> $# arguments: $*"
This works well until $ ct list student grade
.这很有效,直到
$ ct list student grade
。
If I $ ct list student grade
TAB the result is $ ct list student grade -
.如果我
$ ct list student grade
TAB结果是$ ct list student grade -
。 So far so good.到现在为止还挺好。
If I $ ct list student grade -
TAB TAB the expected result is:如果我
$ ct list student grade -
TAB TAB预期结果是:
$ ct list student grade -
--another --option -a -o
If I $ ct list student grade --
TAB the result is $ ct list student grade -
, ie the trailing hyphen is removed.如果 I
$ ct list student grade --
TAB结果是$ ct list student grade -
,即删除尾随的连字符。
If I $ ct list student grade -a
TAB TAB it shows all the options correctly but all my aliases in addition (one of them is alias a=alias
):如果我
$ ct list student grade -a
TAB TAB它会正确显示所有选项,但除此之外我的所有别名(其中之一是alias a=alias
):
$ ct list student grade -a
--another --option -a -o .. a b bp c df kc ll ls x
If I $ ct list student grade --a
TAB I get an error:如果我
$ ct list student grade --a
TAB我得到一个错误:
$ ct list student grade --abash: compgen: --: invalid option
compgen: usage: compgen [-abcdefgjksuv] [-o option] [-A action] [-G globpat] [-W wordlist] [-F function] [-C command] [-X filterpat] [-P prefix] [-S suffix] [word]
How can I handle short ' -...
' and long ' --...
' options correctly?我该如何处理短“
-...
”长“ --...
”选项是否正确?
How can I handle short '-...' and long '--...' options correctly?
如何正确处理短“-...”和长“--...”选项?
You have to separate compgen options and word.您必须将 compgen 选项和 word 分开。
compgen -W "<words>" -- "$word"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.