-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathavalanche-cli-completion.bash
executable file
·65 lines (60 loc) · 2.76 KB
/
avalanche-cli-completion.bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#compdef avalanche-cli
# shellcheck disable=2207
###############################################################################
function _avalanche_cli {
local curr_0="${COMP_WORDS[$(( COMP_CWORD - 0 ))]}" ;
local curr_1="${COMP_WORDS[$(( COMP_CWORD - 1 ))]}" ;
local curr_2="${COMP_WORDS[$(( COMP_CWORD - 2 ))]}" ;
local word_1="${COMP_WORDS[1]}" ;
local word_2="${COMP_WORDS[2]}" ;
COMPREPLY=() ;
local sup_cmds ; sup_cmds="$(avalanche-cli --list-commands)" ;
local sub_cmds ;
local sup_opts ; sup_opts="$(avalanche-cli --list-options)" ;
local sub_opts ;
if [[ -n ${word_1} ]] && [[ " ${sup_cmds[*]} " == *" ${word_1} "* ]] ; then
sub_cmds="$(avalanche-cli "${word_1}" --list-commands)" ;
if [[ -n ${word_2} ]] && [[ " ${sub_cmds[*]} " == *" ${word_2} "* ]] ; then
sub_opts="$(avalanche-cli "${word_1}" "${word_2}" --list-options)" ;
if [ "${curr_1}" == "=" ] ; then
## avalanche-cli command sub-command --option=+ [TAB]
local sug_opts=( $(compgen -W "${sub_opts}" -- "${curr_2}=${curr_0}") ) ;
for opt in "${sug_opts[@]}" ; do
if [[ "$opt" =~ ([^=]+)=([^=]+) ]] ; then
COMPREPLY+=( "${BASH_REMATCH[2]}" ) ;
fi
done
elif [ "${curr_0}" == "=" ] ; then
## avalanche-cli command sub-command --option=* [TAB]
local sug_opts=( $(compgen -W "${sub_opts}" -- "${curr_1}=") ) ;
for opt in "${sug_opts[@]}" ; do
if [[ "$opt" =~ ([^=]+)=([^=]+) ]] ; then
COMPREPLY+=( "${BASH_REMATCH[2]}" ) ;
fi
done
else
## avalanche-cli command sub-command * [TAB]
COMPREPLY+=( $(compgen -W "${sub_opts}" -- "${curr_0}") ) ;
fi
return 0 ;
fi
## avalanche-cli command [TAB]
sub_opts="$(avalanche-cli "${word_1}" --list-options)" ;
COMPREPLY+=( $(compgen -W "${sub_opts}" -- "${curr_0}") ) ;
sub_cmds="$(avalanche-cli "${word_1}" --list-commands)" ;
COMPREPLY+=( $(compgen -W "${sub_cmds}" -- "${curr_0}") ) ;
return 0 ;
fi
## avalanche-cli [TAB]
COMPREPLY+=( $(compgen -W "${sup_opts}" -- "${curr_0}") ) ;
COMPREPLY+=( $(compgen -W "${sup_cmds}" -- "${curr_0}") ) ;
return 0 ;
}
###############################################################################
if [ -n "$ZSH_VERSION" ] ; then
autoload bashcompinit ;
bashcompinit ;
fi
complete -F _avalanche_cli avalanche-cli ;
###############################################################################
###############################################################################