0

I've wrote the following sample completion for : command (to understand why completion fails):

__sample_complete()
{
  local OPTIONS=('--first' '--first=')
  local FIRST_ARGUMENTS=('arg1' 'arg2')

  local current=$2
  local previous=$3

  case $current in
    --first=*)
      current=${current##--first*,}
      readarray -t COMPREPLY < <(compgen -o nospace -W "${FIRST_ARGUMENTS[*]}" -- "$current")
      ;;
    *)
      readarray -t COMPREPLY < <(compgen -W "${OPTIONS[*]}" -- "$current")
      ;;
  esac
}

complete -F __sample_complete :

I wanna see arg1 arg2 suggestions when I type : --first= but now Bash automatically on tab press completes it with --first: --first=--first.

  • [How to bash complete a GNU long option with given set of arguments?](https://stackoverflow.com/questions/58156681/how-to-bash-complete-a-gnu-long-option-with-given-set-of-arguments) – ceving Nov 22 '21 at 08:54

1 Answers1

0

The word --first= is split on COMP_WORDBREAKS, so --first is considered another word. It's really simple to debug - add args=("$@"); declare -p args to your script and inspect the arguments - current word is empty, previous word is --first.

case "$3" in
--first) compgen -o nospace -W "${FIRST_ARGUMENTS[*]}" -- "$2"

I always use COMP* variables, no idea what's better. I liked https://devmanual.gentoo.org/tasks-reference/completion/index.html .

KamilCuk
  • 120,984
  • 8
  • 59
  • 111