0

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?

4
  • 1
    I suspect it is somehow only set when _cpthsv is invoked by the Readline function complete. That might be accomplished by some conditional logic inside _init_completion.
    – chepner
    Commented Sep 1, 2024 at 21:40
  • It looks like _init_completion is deprecated anyway; it mentions using _comp_initialize instead.
    – chepner
    Commented Sep 1, 2024 at 21:52
  • Interesting, I'm on mint stable and the completion package 2.11 does not have that yet ...
    – jjg
    Commented Sep 1, 2024 at 21:57
  • It's deprecated as of 2.12.
    – chepner
    Commented Sep 1, 2024 at 22:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.