Add bash completion definitions
authorJeremy Kerr <jk@ozlabs.org>
Sun, 19 Nov 2006 12:28:07 +0000 (23:28 +1100)
committerJeremy Kerr <jk@ozlabs.org>
Thu, 12 Nov 2009 23:58:42 +0000 (10:58 +1100)
Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
bitfield-completions.sh [new file with mode: 0644]

diff --git a/bitfield-completions.sh b/bitfield-completions.sh
new file mode 100644 (file)
index 0000000..d602b9e
--- /dev/null
@@ -0,0 +1,37 @@
+# completion for bitfield program
+_bitfield_list() {
+       bitfield --list | awk '{print $1}'
+}
+
+_bitfield() {
+       local cur prev
+
+        COMPREPLY=()
+        cur=${COMP_WORDS[COMP_CWORD]}
+        prev=${COMP_WORDS[COMP_CWORD-1]}
+
+       # help, search, list: no completions
+       if [[ "$prev" == '-h' || "$prev" == '-s' || "$prev" == '-l' ]]
+       then
+               return 0
+       fi
+
+       opts="--help --search --list --non-zero"
+
+       # complete -* with long options.
+       if [[ "$cur" == -* ]]
+       then
+               COMPREPLY=($(compgen -W "$opts" -- $cur))
+               return 0
+       fi
+
+       # first parameter on line - complete with options + list of bitfields
+       # FIXME: presence of -n will disable this...
+       if [ $COMP_CWORD -eq 1 ]
+       then
+               COMPREPLY=($(compgen -W "$opts $(_bitfield_list)" -- $cur))
+               return 0
+       fi
+}
+
+complete -F _bitfield filenames bitfield