I have some programs for which I've created some bash completion scripts, and those seem to work OK. I'd like to add some tests, and though this approach looked promising, but I get unexpected failures. I instrument the completion script as
_cpthsv()
{
local cur prev words cword split
_init_completion -s || return
# debugging
echo
echo "cur: '$cur'"
echo "prev: '$prev'"
echo "words: ${COMP_WORDS[*]}"
:
and on the command-line
$ cpthsv --hel<tab>
cur: '--hel'
prev: 'cpthsv'
words: cpthsv --hel
which looks right (and completes to --help
). But
$ COMP_WORDS=(cpthsv --hel) ; COMP_CWORD=1 ; _cpthsv ; echo ${COMPREPLY[*]}
cur: ''
prev: 'cpthsv'
words: cpthsv --hel
i.e., $cur
variable is empty (and the completion fails because of this). I've looked at the _init_completion
function which is supposed to set $cur
but can't see anything obvious.
Would anyone know why $cur
is set in the first scenario, but not the second; better, is there a workaround in the second case?
_cpthsv
is invoked by the Readline functioncomplete
. That might be accomplished by some conditional logic inside_init_completion
._init_completion
is deprecated anyway; it mentions using_comp_initialize
instead.