1 # nbdkit bash completion script -*- shell-script -*-
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
15 # * Neither the name of Red Hat nor the names of its contributors may be
16 # used to endorse or promote products derived from this software without
17 # specific prior written permission.
19 # THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
20 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
23 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 _nbdkit_list_plugins ()
36 nbdkit --dump-config | grep ^plugindir= | sed 's/^plugindir=//'
38 ls -1 "$plugindir" | sed 's/^nbdkit-//' | sed 's/-plugin.*//'
41 _nbdkit_list_filters ()
45 nbdkit --dump-config | grep ^filterdir= | sed 's/^filterdir=//'
47 ls -1 "$filterdir" | sed 's/^nbdkit-//' | sed 's/-filter.*//'
50 # This handler function is called when the user presses tab.
53 local cur prev words cword split
54 local shortopts longopts plugin plugins filters args i
56 _init_completion -s || return
58 # Did we get the plugin name yet?
59 # This is only a heuristic because it can be confused by
60 # long opt parameters with an arguments. XXX
62 for (( i=1; i < ${#words[@]}; ++i)) ; do
63 if [[ "${words[i]}" =~ ^[a-zA-Z0-9]+$ ]]; then
69 # Previous item on the current line is a completable flag or plugin name?
72 filters="$(_nbdkit_list_filters)"
73 COMPREPLY=( $(compgen -W "$filters" "$cur") )
76 COMPREPLY=( $(compgen -W "off on require" "$cur") )
78 # Could complete -u and -g options too. XXX
81 # Current item is an option we can expand?
84 longopts="$(nbdkit --long-options)"
85 COMPREPLY=( $(compgen -W "$longopts" -- "$cur") )
88 shortopts="$(nbdkit --short-options)"
89 longopts="$(nbdkit --long-options)"
90 COMPREPLY=( $(compgen -W "$shortopts $longopts" -- "$cur") )
93 if [ "$plugin" = "" ] || [ "$plugin" = "$cur" ]; then
94 # Complete plugin name.
95 plugins="$(_nbdkit_list_plugins)"
96 COMPREPLY=( $(compgen -W "$plugins" "$cur") )
99 # Complete plugin args.
101 nbdkit $plugin --help 2>/dev/null |
102 grep -E '^[-a-z0-9]+=' | sed 's/=.*/=/'
104 COMPREPLY=( $(compgen -W "$args" "$cur") )
111 # Install the handler function.
112 complete -o default -F _nbdkit nbdkit