a90820ab42c65d98fd1c1284c76d7474f70303b7
[lttng-tools.git] / extras / lttng-bash_completion
1 #
2 # Copyright (c) - 2012 Simon Marchi <simon.marchi@polymtl.ca>
3 #
4 # This program is free software; you can redistribute it and/or modify it under
5 # the terms of the GNU General Public License as published by as published by
6 # the Free Software Foundation; only version 2 of the License.
7 #
8 # This program is distributed in the hope that it will be useful, but WITHOUT
9 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 # more details.
12 #
13 # You should have received a copy of the GNU General Public License along with
14 # this program; if not, write to the Free Software Foundation, Inc., 51
15 # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 #
17
18 _lttng_complete_sessions() {
19 # TODO, maybe have a lttng list --simple or something like that
20 return
21 }
22
23 _lttng_cmd_addcontext() {
24 local add_context_opts
25 add_context_opts=$(lttng add-context --list-options)
26
27 case $prev in
28 --session|-s)
29 _lttng_complete_sessions
30 return
31 ;;
32 --channel|-c)
33 return
34 ;;
35 --type|-t)
36 return
37 ;;
38 esac
39
40 case $cur in
41 -*)
42 COMPREPLY=( $(compgen -W "${add_context_opts}" -- $cur) )
43 return
44 ;;
45 esac
46 }
47
48 _lttng_cmd_create() {
49 local create_opts
50 create_opts=$(lttng create --list-options)
51
52 case $prev in
53 --output|-o)
54 _filedir -d
55 return
56 ;;
57 esac
58
59 case $cur in
60 -*)
61 COMPREPLY=( $(compgen -W "${create_opts}" -- $cur) )
62 return
63 ;;
64 esac
65 }
66
67 _lttng_cmd_destroy() {
68 local destroy_opts
69 destroy_opts=$(lttng destroy --list-options)
70
71 case $cur in
72 -*)
73 COMPREPLY=( $(compgen -W "${destroy_opts}" -- $cur) )
74 ;;
75 *)
76 _lttng_complete_sessions
77 ;;
78 esac
79 }
80
81 _lttng_cmd_enablechannel() {
82 local enable_channel_opts
83 enable_channel_opts=$(lttng enable-channel --list-options)
84
85 case $prev in
86 --session|-s)
87 _lttng_complete_sessions
88 return
89 ;;
90 esac
91
92 case $cur in
93 -*)
94 COMPREPLY=( $(compgen -W "${enable_channel_opts}" -- $cur) )
95 return
96 ;;
97 esac
98 }
99
100 _lttng_cmd_enableevent() {
101 local enable_event_opts
102 enable_event_opts=$(lttng enable-event --list-options)
103
104 case $prev in
105 --session|-s)
106 _lttng_complete_sessions
107 return
108 ;;
109 --channel|-c)
110 return
111 ;;
112 --probe)
113 return
114 ;;
115 --function)
116 return
117 ;;
118 esac
119
120 case $cur in
121 -*)
122 COMPREPLY=( $(compgen -W "${enable_event_opts}" -- $cur) )
123 return
124 ;;
125 esac
126 }
127
128 _lttng_cmd_enableconsumer() {
129 local enable_consumer_opts
130 enable_consumer_opts=$(lttng enable-consumer --list-options)
131
132 case $prev in
133 --session|-s)
134 _lttng_complete_sessions
135 return
136 ;;
137 esac
138
139 case $cur in
140 -*)
141 COMPREPLY=( $(compgen -W "${enable_consumer_opts}" -- $cur) )
142 return
143 ;;
144 esac
145 }
146
147 _lttng_cmd_disableconsumer() {
148 local disable_consumer_opts
149 disable_consumer_opts=$(lttng disable-consumer --list-options)
150
151 case $prev in
152 --session|-s)
153 _lttng_complete_sessions
154 return
155 ;;
156 esac
157
158 case $cur in
159 -*)
160 COMPREPLY=( $(compgen -W "${disable_consumer_opts}" -- $cur) )
161 return
162 ;;
163 esac
164 }
165
166 _lttng_cmd_disablechannel() {
167 local disable_channel_opts
168 disable_channel_opts=$(lttng disable-channel --list-options)
169
170 case $prev in
171 --session|-s)
172 _lttng_complete_sessions
173 return
174 ;;
175 esac
176
177 case $cur in
178 -*)
179 COMPREPLY=( $(compgen -W "${disable_channel_opts}" -- $cur) )
180 return
181 ;;
182 esac
183 }
184
185 _lttng_cmd_disableevent() {
186 local disable_event_opts
187 disable_channel_opts=$(lttng disable-event --list-options)
188
189 case $prev in
190 --session|-s)
191 _lttng_complete_sessions
192 return
193 ;;
194 --channel|-c)
195 return
196 ;;
197 esac
198
199 case $cur in
200 -*)
201 COMPREPLY=( $(compgen -W "${disable_event_opts}" -- $cur) )
202 return
203 ;;
204 esac
205 }
206
207 _lttng_cmd_list() {
208 local list_opts
209 list_opts=$(lttng list --list-options)
210
211 case $prev in
212 --channel|-c)
213 return
214 ;;
215 esac
216
217 case $cur in
218 -*)
219 COMPREPLY=( $(compgen -W "${list_opts}" -- $cur) )
220 return
221 ;;
222 esac
223 }
224
225 _lttng_cmd_setsession() {
226 local set_session_opts
227 set_session_opts=$(lttng set-session --list-options)
228
229 case $cur in
230 -*)
231 COMPREPLY=( $(compgen -W "${set_session_opts}" -- $cur) )
232 return
233 ;;
234 esac
235 }
236
237 _lttng_cmd_start() {
238 local start_opts
239 start_opts=$(lttng start --list-options)
240
241 case $cur in
242 -*)
243 COMPREPLY=( $(compgen -W "${start_opts}" -- $cur) )
244 ;;
245 *)
246 _lttng_complete_sessions
247 ;;
248 esac
249 }
250
251 _lttng_cmd_stop() {
252 local stop_opts
253 stop_opts=$(lttng stop --list-options)
254
255 case $cur in
256 -*)
257 COMPREPLY=( $(compgen -W "${stop_opts}" -- $cur) )
258 ;;
259 *)
260 _lttng_complete_sessions
261 ;;
262 esac
263 }
264
265 _lttng_cmd_version() {
266 local version_opts
267 version_opts=$(lttng version --list-options)
268
269 case $cur in
270 -*)
271 COMPREPLY=( $(compgen -W "${version_opts}" -- $cur) )
272 ;;
273 esac
274 }
275
276 _lttng_cmd_calibrate() {
277 local calibrate_opts
278 calibrate_opts=$(lttng calibrate --list-options)
279
280 case $cur in
281 -*)
282 COMPREPLY=( $(compgen -W "${calibrate_opts}" -- $cur) )
283 ;;
284 esac
285 }
286
287 _lttng_cmd_view() {
288 local view_opts
289 view_opts=$(lttng view --list-options)
290
291 case $cur in
292 -*)
293 COMPREPLY=( $(compgen -W "${view_opts}" -- $cur) )
294 ;;
295 esac
296 }
297
298 _lttng_opts() {
299 local opts
300 opts=$(lttng --list-options)
301
302 COMPREPLY=( $(compgen -W "${opts}" -- $cur) )
303 }
304
305 _lttng_commands() {
306 COMPREPLY=( $(compgen -W "$commands" -- $cur) )
307 }
308
309 _lttng_before_command() {
310 # Check if the previous word should alter the behavior
311 case $prev in
312 --group|-g)
313 COMPREPLY=( $(compgen -g -- $cur) )
314 return
315 ;;
316 --sessiond-path)
317 _filedir
318 return
319 ;;
320 esac
321
322 case $cur in
323 -*)
324 # If the current word starts with a dash, complete with options
325 _lttng_opts
326 ;;
327 *)
328 # Otherwise complete with commands
329 _lttng_commands
330 ;;
331 esac
332 }
333
334 _lttng_after_command() {
335 local cmd_name
336
337 cmd_name=_lttng_cmd_${command//-/}
338
339 type -t $cmd_name | grep -q 'function' && $cmd_name
340 }
341
342 _lttng_is_command() {
343 for command in $commands; do
344 if [ "$1" == "$command" ]; then
345 return 0
346 fi
347 done
348
349 return 1
350 }
351
352 _lttng() {
353 local cur prev commands command_found command_found_index
354
355 # Get the current and previous word
356 _get_comp_words_by_ref cur prev
357
358 # Get the valid LTTng commands
359 commands=$(lttng --list-commands)
360
361 # The text of the found command
362 command_found=""
363
364 # The index of the found command in COMP_WORDS
365 command_found_index=-1
366
367 for (( i = 1 ; i < ${#COMP_WORDS[@]} ; i++ )); do
368 _lttng_is_command ${COMP_WORDS[$i]}
369 if [ $? -eq 0 ]; then
370 command_found=${COMP_WORDS[$i]}
371 command_found_index=$i
372 break
373 fi
374
375 done
376
377 # Check if the cursor is before or after the command keyword
378 if [ -n "$command_found" ] && [ "$COMP_CWORD" -gt "$command_found_index" ]; then
379 _lttng_after_command
380 else
381 _lttng_before_command
382 fi
383 }
384
385 complete -F _lttng lttng
This page took 0.056195 seconds and 4 git commands to generate.