forked from robotology/yarp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyarp_completion
executable file
·406 lines (353 loc) · 11.8 KB
/
yarp_completion
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#!/bin/bash
# Copyright: (C) 2010 Daniel Krieg
# Author: Daniel Krieg <[email protected]>
# CopyPolicy: Released under the terms of the LGPLv2.1 or later, see LGPL.TXT
### call "yarp name list" and extract all available ports
_get_yarp_ports() {
echo $(yarp name list 2>/dev/null | grep registration | awk '{print $3 }'| grep ^\/ )
}
### call "yarp run --on 'server' --ps" and extract all available tags
_get_yarprun_tags() {
echo $(yarp run --on $1 --ps 2>&1 | grep "^(pid" | sed 's/(//g;s/)//g' | awk '{print $4 }' )
}
_get_yarp_help(){
echo $(yarp help 2>/dev/null| awk 'NR>=4 {print $1}' )
}
_get_yarp_carriers(){
echo $(yarp connect --list-carriers 2>/dev/null)
}
_get_yarp_plugins(){
echo $(yarp plugin --list 2>/dev/null)
}
_yarp()
{
COMPREPLY=()
# check if bash completion version is 1.2 or higher
if type _get_comp_words_by_ref > /dev/null 2>&1 ;
then bc1_2=true;
else bc1_2=false;
fi;
if $bc1_2 ;
then
_get_comp_words_by_ref -n : cur prev words cword
COMP_WORDS=$words
COMP_CWORD=$cword ;
else
local cur=$(_get_cword) ;
fi;
local command="${COMP_WORDS[1]}"
local cmds=$(_get_yarp_help)
local connectopts=(--persist --persist-to --persist-from --list-carriers --help)
local pluginopts=(--help --list --all --search-path)
completion=""
if [ $COMP_CWORD -eq 1 ]; then
### first option => possible commands
completion="${cmds}"
else
case "${command}" in
connect)
local secondArg="${COMP_WORDS[2]}"
if [ $COMP_CWORD -eq 2 ]; then
### complete source port
completion="${connectopts[@]} $(_get_yarp_ports)"
elif [ $COMP_CWORD -eq 3 ]; then
### complete destination port
completion=$(_get_yarp_ports)
elif [ $COMP_CWORD -eq 4 ]; then
if [[ ${connectopts[*]} =~ "${secondArg}" ]]; then
completion=$(_get_yarp_ports)
else
completion=$(_get_yarp_carriers)
fi
elif [[ $COMP_CWORD -eq 5 && ${connectopts[*]} =~ "${secondArg}" ]]; then
completion=$(_get_yarp_carriers)
fi
;;
disconnect)
if [ $COMP_CWORD -le 3 ]; then
### complete source and destination ports
completion=$(_get_yarp_ports)
fi
;;
read)
if [ $COMP_CWORD -eq 3 ]; then
### complete destination port
completion=$(_get_yarp_ports)
fi
;;
write)
if [ $COMP_CWORD -eq 3 ]; then
### complete destination port
completion=$(_get_yarp_ports)
fi
;;
run)
### handle with yarprun completion
### offset of 1 for additional command
_yarprun 1
;;
context)
_yarpcontext
;;
robot)
_yarprobot
;;
exists|ping|rpc|terminate|wait)
if [ $COMP_CWORD -eq 2 ]; then
completion=$(_get_yarp_ports)
fi
;;
detect)
if [ $COMP_CWORD -eq 2 ]; then completion="--write"; fi ;;
name)
### TODO: add more nameserver commands if needed
if [ $COMP_CWORD -eq 2 ]; then completion="list"; fi ;;
conf)
if [ $COMP_CWORD -eq 2 ]; then completion="--clean"; fi ;;
plugin)
if [ $COMP_CWORD -eq 2 ]; then
completion="${pluginopts[@]} $(_get_yarp_plugins)"
fi
;;
*)
;;
esac
fi
COMPREPLY=( $(compgen -W "${completion}" -- ${cur}) )
if $bc1_2 ;
then __ltrim_colon_completions "$cur"
fi;
return 0
}
_yarprun() {
COMPREPLY=()
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD-1]}"
if [ $COMP_CWORD -ge 2 ]; then
local pprev="${COMP_WORDS[COMP_CWORD-2]}"
fi
local command="${COMP_WORDS[1]}"
local run_cmds1="--cmd --as --geometry --hold --stdio --workdir"
local run_cmds2="--kill --sigterm --sigtermall --ps --isrunning --exit"
completion=""
### check offset (if called from "yarp run" instead of "yarprun")
### default 0
offset=${1:-0}
if [ $COMP_CWORD -eq $((1+$offset)) ]; then
### first options (either start new server or send command to exisiting one)
completion="--on --server"
else
if [ $COMP_CWORD -ge $((2+$offset)) -a "${COMP_WORDS[$((1+$offset))]}" == "--on" ]; then
server=${COMP_WORDS[$((2+$offset))]}
case "$prev" in
"--on"|"--stdio")
### complete server or stdio port
completion=$(_get_yarp_ports)
;;
"--kill"|"--sigterm"|"--isrunning")
### complete tags running on server
completion=$(_get_yarprun_tags $server)
;;
"--hold")
completion="$run_cmds1"
;;
*)
### options with user input
case "$pprev" in
### complete after user input has been entered
"--on")
completion="$run_cmds1 $run_cmds2"
;;
"--cmd"|"--geometry"|"--stdio"|"--as"|"--workdir")
completion="$run_cmds1"
;;
esac
;;
esac
fi
fi
COMPREPLY=( $(compgen -W "${completion}" -- ${cur}) )
return 0
}
_yarpserver(){
COMPREPLY=()
local cur="${COMP_WORDS[COMP_CWORD]}"
completion=""
### check offset (if called from "yarp server" instead of "yarpserver")
### default 0
offset=${1:-0}
if [ $COMP_CWORD -eq $((1+$offset)) ]; then
completion=$(echo $(yarpserver --help | grep -o "^ --\S*" | sed 's/ //g' | sort ) )
fi
COMPREPLY=( $(compgen -W "${completion}" -- ${cur}) )
return 0
}
_get_all_contexts() {
for x in `yarp-config context --list | grep "^[^\*]" | sort | uniq`;
do echo ${x} ;
done
}
_get_user_contexts() {
for x in `yarp-config context --list --user| grep "^[^\*]" | sort | uniq`;
do echo ${x} ;
done
}
_get_installed_contexts() {
for x in `yarp-config context --list --installed| grep "^[^\*]" | sort | uniq`;
do echo ${x} ;
done
}
_yarpcontext() {
COMPREPLY=()
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD-1]}"
local command="${COMP_WORDS[2]}"
local opts="--where --import --remove --diff --diff-list --merge --list --help --import-all"
completion=""
if [ $COMP_CWORD -eq 2 ]; then
### first options
completion=$opts
else
case "$prev" in
"--where")
completion=$(_get_all_contexts)
;;
"--import")
completion=$(_get_installed_contexts)
;;
"--remove"|"--diff"|"--merge")
completion=$(_get_user_contexts)
;;
"--list")
completion="--user --sysadm --installed"
;;
*)
case "$command" in
"--import" )
local contextName="${COMP_WORDS[3]}"
local directories
for x in `yarp-config context --where $contextName --installed| grep "^[^\*]" | sort | uniq`;
do directories="${directories[@]} $x"
done
for d in ${directories}
do completion+="$(find "${d}" -mindepth 1 -printf '%P\n')"
done
;;
"--remove"|"--merge")
local contextName="${COMP_WORDS[3]}"
local directories
for x in `yarp-config context --where $contextName --user| grep "^[^\*]" | sort | uniq`;
do directories="${directories[@]} $x"
done
for d in ${directories}
do completion+="$(find "${d}" -mindepth 1 -printf '%P\n')"
done
;;
*)
;;
esac
;;
esac
fi
COMPREPLY=( $(compgen -W "${completion}" -- ${cur}) )
return 0
}
_get_all_robots() {
for x in `yarp-config robot --list | grep "^[^\*]" | sort | uniq`;
do echo ${x} ;
done
}
_get_user_robots() {
for x in `yarp-config robot --list --user| grep "^[^\*]" | sort | uniq`;
do echo ${x} ;
done
}
_get_installed_robots() {
for x in `yarp-config robot --list --installed| grep "^[^\*]" | sort | uniq`;
do echo ${x} ;
done
}
_yarprobot() {
COMPREPLY=()
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD-1]}"
local command="${COMP_WORDS[2]}"
local opts="--where --current --import --remove --diff --diff-list --merge --list --help --import-all"
completion=""
if [ $COMP_CWORD -eq 2 ]; then
### first options
completion=$opts
else
case "$prev" in
"--where")
completion=$(_get_all_robots)
;;
"--import")
completion=$(_get_installed_robots)
;;
"--remove"|"--diff"|"--merge")
completion=$(_get_user_robots)
;;
"--list")
completion="--user --sysadm --installed"
;;
*)
case "$command" in
"--import" )
local robotName="${COMP_WORDS[3]}"
local directories
for x in `yarp-config robot --where $robotName --installed| grep "^[^\*]" | sort | uniq`;
do directories="${directories[@]} $x"
done
for d in ${directories}
do completion+="$(find "${d}" -mindepth 1 -printf '%P\n')"
done
;;
"--remove"|"--merge")
local robotName="${COMP_WORDS[3]}"
local directories
for x in `yarp-config robot --where $robotName --user| grep "^[^\*]" | sort | uniq`;
do directories="${directories[@]} $x"
done
for d in ${directories}
do completion+="$(find "${d}" -mindepth 1 -printf '%P\n')"
done
;;
*)
;;
esac
;;
esac
fi
COMPREPLY=( $(compgen -W "${completion}" -- ${cur}) )
return 0
}
_yarpconfig() {
COMPREPLY=()
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD-1]}"
local command="${COMP_WORDS[1]}"
local opts="context robot --help --namespace --nameserver --version"
completion=""
if [ $COMP_CWORD -eq 1 ]; then
### first options
completion=$opts
else
case "$command" in
"context")
_yarpcontext
;;
"robot")
_yarprobot
;;
*)
;;
esac
fi
COMPREPLY=( $(compgen -W "${completion}" -- ${cur}) )
return 0
}
complete -F _yarpconfig yarp-config
complete -F _yarp yarp
complete -F _yarprun yarprun
complete -F _yarpserver yarpserver