generator: Optional arguments, add-drive-opts (RHBZ#642934,CVE-2010-3851).
[libguestfs.git] / fish / guestfish-bash-completion.sh
1 # guestfish bash completion script
2 # Copyright (C) 2010 Red Hat Inc.
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 # To use this script, copy it into /etc/bash_completion.d/ if
19 # that directory exists.
20 #
21 # If your distro does not have that directory (or if you cannot or
22 # do not want to install this in /etc) then you can copy this to
23 # somewhere in your home directory such as
24 # ~/.guestfish-bash-completion.sh and add this to your .bashrc:
25 #   source ~/.guestfish-bash-completion.sh
26
27 # This was "inspired" by the git bash completion script written by
28 # Shawn O. Pearce.
29
30 _guestfish_virsh_list ()
31 {
32     local flag_ro=$1 flags
33
34     if [ "$flag_ro" -eq 1 ]; then
35         flags="--all"
36     else
37         flags="--inactive"
38     fi
39     virsh list $flags | head -n -1 | tail -n +3 | awk '{print $2}'
40 }
41
42 _guestfish ()
43 {
44     local flag_i=0 flag_ro=0 c=1 word cmds doms
45
46     # See if user has specified -i option before the current word.
47     while [ $c -lt $COMP_CWORD ]; do
48         word="${COMP_WORDS[c]}"
49         case "$word" in
50             -i|--inspector) flag_i=1 ;;
51             -r|--ro) flag_ro=1 ;;
52         esac
53         c=$((++c))
54     done
55
56     # Now try to complete the current word.
57     word="${COMP_WORDS[COMP_CWORD]}"
58     case "$word" in
59         --*)
60             COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '
61                           --cmd-help
62                           --add
63                           --no-dest-paths
64                           --file
65                           --inspector
66                           --listen
67                           --mount
68                           --no-sync
69                           --new
70                           --remote
71                           --ro
72                           --selinux
73                           --verbose
74                           --version
75                         ' -- "$word")) ;;
76         *)
77             if [ "$flag_i" -eq 1 ]; then
78                 doms=$(_guestfish_virsh_list "$flag_ro")
79                 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W "$doms" -- "$word"))
80             else
81                 cmds=$(guestfish -h| head -n -1 | tail -n +2 | awk '{print $1}')
82                 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W "$cmds" -- "$word"))
83             fi ;;
84     esac
85 }
86
87 complete -o bashdefault -o default -F _guestfish guestfish 2>/dev/null \
88   || complete -o default -F _guestfish guestfish
89
90 # EOF