| 1 |
_pipesz_module() |
| 2 |
{ |
| 3 |
local WORD OPTS OPTARG OPTEND SOPT LOPT TARG |
| 4 |
local SOPTS=(g s f n i o e c q v h V) |
| 5 |
local LOPTS=(get set file fd stdin stdout stderr check quiet verbose help version) |
| 6 |
local AOPTS=(0 1 1 1 0 0 0 0 0 0 0 0) # takes argument |
| 7 |
local TOPTS=(1 0 1 1 1 1 1 0 0 0 0 0) # specifies target |
| 8 |
local XOPTS=(0 0 0 0 0 0 0 0 0 0 1 1) # exits immediately |
| 9 |
local MOPTS=(0 0 1 1 0 0 0 0 0 0 0 0) # repeatable |
| 10 |
local NOPTS=(0 0 0 0 0 0 0 0 0 0 0 0) # number of repeats |
| 11 |
local IDXG=0 IDXS=1 # index of --get and --set |
| 12 |
|
| 13 |
for ((i=1; i<COMP_CWORD; i++)); do |
| 14 |
WORD=${COMP_WORDS[i]} |
| 15 |
|
| 16 |
if [[ ${NOPTS[$IDXG]} -eq 0 ]]; then |
| 17 |
case $WORD in |
| 18 |
--) |
| 19 |
_command_offset $((i+1)) |
| 20 |
return 0;; |
| 21 |
[^-]*) |
| 22 |
_command_offset $i |
| 23 |
return 0;; |
| 24 |
esac |
| 25 |
fi |
| 26 |
|
| 27 |
for ((j=0; j<${#NOPTS[@]}; j++)); do |
| 28 |
SOPT=${SOPTS[$j]} |
| 29 |
LOPT=${LOPTS[$j]} |
| 30 |
|
| 31 |
case $WORD in |
| 32 |
--$LOPT) OPTEND=l;; |
| 33 |
--*) continue;; |
| 34 |
-*$SOPT) OPTEND=s;; |
| 35 |
-*$SOPT*) OPTEND=n;; |
| 36 |
*) continue;; |
| 37 |
esac |
| 38 |
|
| 39 |
if [[ ${XOPTS[$j]} -ne 0 ]]; then |
| 40 |
COMPREPLY=() |
| 41 |
return 0 |
| 42 |
fi |
| 43 |
|
| 44 |
((NOPTS[j]++)) |
| 45 |
|
| 46 |
[[ ${TOPTS[$j]} -ne 0 ]] && TARG=y |
| 47 |
[[ $OPTEND != n ]] && ((i+=AOPTS[j])) |
| 48 |
[[ $OPTEND == l ]] && break |
| 49 |
done |
| 50 |
done |
| 51 |
|
| 52 |
case $3 in |
| 53 |
--fd) OPTARG=n;; |
| 54 |
--file) OPTARG=f;; |
| 55 |
--size) OPTARG=s;; |
| 56 |
--*) ;; |
| 57 |
-*n) OPTARG=n;; |
| 58 |
-*f) OPTARG=f;; |
| 59 |
-*s) OPTARG=s;; |
| 60 |
esac |
| 61 |
|
| 62 |
case $OPTARG in |
| 63 |
f) |
| 64 |
compopt -o filenames |
| 65 |
COMPREPLY=( $(compgen -f -- "$2") ) |
| 66 |
return 0;; |
| 67 |
n) |
| 68 |
COMPREPLY=( $(compgen -W "0 1 2" -- "$2") ) |
| 69 |
return 0;; |
| 70 |
s) |
| 71 |
WORD=$2 |
| 72 |
if [[ ! $WORD =~ ^[0-9]+[a-zA-Z]*$ ]]; then |
| 73 |
COMPREPLY=() |
| 74 |
return 0 |
| 75 |
fi |
| 76 |
|
| 77 |
while [[ $WORD =~ [a-zA-Z]$ ]]; do WORD=${WORD:0:-1}; done |
| 78 |
|
| 79 |
compopt -o nosort |
| 80 |
COMPREPLY=( $(compgen -W "$WORD $WORD{K,M,G}{B,iB}" -- "$2") ) |
| 81 |
return 0;; |
| 82 |
esac |
| 83 |
|
| 84 |
for ((j=0; j<${#NOPTS[@]}; j++)); do |
| 85 |
[[ $j -eq $IDXG && ${NOPTS[$IDXS]} -ne 0 ]] && continue |
| 86 |
[[ $j -eq $IDXS && ${NOPTS[$IDXG]} -ne 0 ]] && continue |
| 87 |
[[ $COMP_CWORD -ne 1 && ${XOPTS[$j]} -ne 0 ]] && continue |
| 88 |
[[ ${NOPTS[$j]} -gt 0 && ${MOPTS[$j]} -eq 0 ]] && continue |
| 89 |
|
| 90 |
[[ $2 != --* && $2 == -* ]] && OPTS+=" -${SOPTS[$j]}" |
| 91 |
OPTS+=" --${LOPTS[$j]}" |
| 92 |
done |
| 93 |
|
| 94 |
if [[ ! $TARG || ${NOPTS[$IDXG]} -ne 0 ]]; then |
| 95 |
COMPREPLY=( $(compgen -W "$OPTS" -- "$2") ) |
| 96 |
else |
| 97 |
compopt -o filenames |
| 98 |
COMPREPLY=( $(compgen -c -W "$OPTS --" -- "$2") ) |
| 99 |
fi |
| 100 |
} |
| 101 |
|
| 102 |
complete -F _pipesz_module pipesz |