Home > database >  Complete arguments for options
Complete arguments for options

Time:11-23

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.

CodePudding user response:

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 .

  •  Tags:  
  • bash
  • Related