| 1 |
#!/bin/bash completion for {scriptname} |
| 2 |
|
| 3 |
_{scriptname}(){ |
| 4 |
local cur prev |
| 5 |
local -A ARGS MAP FORCE OPTS OPTS_SUB MULTI |
| 6 |
|
| 7 |
COMPREPLY=() |
| 8 |
cur="${COMP_WORDS[COMP_CWORD]}" |
| 9 |
prev="${COMP_WORDS[COMP_CWORD-1]}" |
| 10 |
|
| 11 |
OPTS=({opts}) |
| 12 |
ARGS=({args}) |
| 13 |
MAP=({map}) |
| 14 |
|
| 15 |
if [ ! -z "$prev" ]; then |
| 16 |
# if is an argument complete with list of choice if define |
| 17 |
prev_key=${MAP[$prev]} |
| 18 |
if [ ! -z "$prev_key" ] && [ -v "${ARGS[$prev_key]}" ]; then |
| 19 |
COMPREPLY=($(compgen -W "${ARGS[$prev_key]}" -- "${cur}")) |
| 20 |
return 0 |
| 21 |
fi |
| 22 |
fi |
| 23 |
for in_use in ${COMP_WORDS[@]:1}; do |
| 24 |
key=${MAP[$in_use]} |
| 25 |
# Unset option that is already use |
| 26 |
unset OPTS[$key] |
| 27 |
unset ARGS[$key] |
| 28 |
done |
| 29 |
compl="${OPTS[@]}" |
| 30 |
COMPREPLY=($(compgen -W "${compl}" -- "${cur}")) |
| 31 |
return 0 |
| 32 |
} |
| 33 |
|
| 34 |
complete -F _{scriptname} {scriptname} |