-
Notifications
You must be signed in to change notification settings - Fork 372
/
Copy pathopam_completion.sh
155 lines (146 loc) · 4.33 KB
/
opam_completion.sh
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
_opam_add()
{
_opam_reply="$_opam_reply $1"
}
_opam_add_f()
{
local cmd
cmd=$1; shift
_opam_add "$($cmd "$@" 2>/dev/null)"
}
_opam_flags()
{
opam "$@" --help=groff 2>/dev/null | \
sed -n 's#\\-#-#g; s#^\\fB\(-[^,= ]*\)\\fR.*#\1#p'
}
_opam_commands()
{
opam "$@" --help=groff 2>/dev/null | \
sed -n '/^\.SH COMMANDS$/,/^\.SH/ { s#\\-#-#g; s#^\\fB\([^,= ]*\)\\fR.*#\1#p }'
echo '--help'
}
_opam_vars()
{
opam config list --safe 2>/dev/null | \
sed -n '/^PKG:/d; s/^\([^# ][^ ]*\).*/\1/p'
}
_opam()
{
local cmd subcmd cur prev compgen_opt
COMPREPLY=()
cmd=${COMP_WORDS[1]}
subcmd=${COMP_WORDS[2]}
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
compgen_opt=""
_opam_reply=""
if [ $COMP_CWORD -eq 1 ]; then
_opam_add_f opam help topics
COMPREPLY=( $(compgen -W "$_opam_reply" -- $cur) )
unset _opam_reply
return 0
fi
case "$cmd" in
install|show|info)
_opam_add_f opam list --safe -a -s
if [ $COMP_CWORD -gt 2 ]; then
_opam_add_f _opam_flags "$cmd"
fi;;
reinstall|remove|uninstall)
_opam_add_f opam list --safe -i -s
if [ $COMP_CWORD -gt 2 ]; then
_opam_add_f _opam_flags "$cmd"
fi;;
upgrade)
_opam_add_f opam list --safe -i -s
_opam_add_f _opam_flags "$cmd"
;;
switch)
case $COMP_CWORD in
2)
_opam_add_f _opam_commands "$cmd"
_opam_add_f opam switch list --safe -s -i;;
3)
case "$subcmd" in
install|set)
_opam_add_f opam switch list --safe -s -a;;
remove|reinstall)
_opam_add_f opam switch list --safe -s -i;;
import|export)
compgen_opt="-o filenames -f";;
*)
_opam_add_f _opam_flags "$cmd"
esac;;
*)
_opam_add_f _opam_flags "$cmd"
esac;;
config)
if [ $COMP_CWORD -eq 2 ]; then
_opam_add_f _opam_commands "$cmd"
else
if [ $COMP_CWORD -eq 3 ] && [ "$subcmd" = "var" ]; then
_opam_add_f _opam_vars
else
_opam_add_f _opam_flags "$cmd"
fi
fi;;
repository|remote)
case $COMP_CWORD in
2)
_opam_add_f _opam_commands "$cmd";;
3)
case "$subcmd" in
add)
if [ $COMP_CWORD -gt 3 ]; then
compgen_opt="-o filenames -f"
fi;;
remove|priority|set-url)
_opam_add_f opam repository list --safe -s;;
*)
_opam_add_f _opam_flags "$cmd"
esac;;
*)
_opam_add_f _opam_flags "$cmd"
case "$subcmd" in
set-url|add) compgen_opt="-o filenames -f";;
esac;;
esac;;
update)
_opam_add_f opam repository list --safe -s
_opam_add_f opam pin list --safe -s
;;
source)
_opam_add_f opam list --safe -A -s
_opam_add_f _opam_flags "$cmd"
;;
pin)
case $COMP_CWORD in
2)
_opam_add_f _opam_commands "$cmd";;
3)
case "$subcmd" in
add)
_opam_add_f opam list --safe -A -s;;
remove|edit)
_opam_add_f opam pin list --safe -s;;
*)
_opam_add_f _opam_flags "$cmd"
esac;;
*)
_opam_add_f _opam_flags "$cmd"
esac;;
unpin)
if [ $COMP_CWORD -eq 2 ]; then
_opam_add_f opam pin list --safe -s
else
_opam_add_f _opam_flags "$cmd"
fi;;
*)
_opam_add_f _opam_commands "$cmd"
_opam_add_f _opam_flags "$cmd"
esac
COMPREPLY=( $(compgen -W "$_opam_reply" $compgen_opt -- "$cur") )
unset _opam_reply
return 0
}
complete -F _opam opam