[buildd-tools-devel] Bug#519745: [Buildd-tools-devel] Bug#519745: schroot: Add bash completion script

Tim Abbott tabbott at MIT.EDU
Sat Mar 28 18:32:13 UTC 2009


On Sat, 21 Mar 2009, Roger Leigh wrote:

> > By the way, it currently is the case that when new options are added to 
> > schroot, they need to be added to the list of options in the completion 
> > script for them to complete; I just wanted to make sure you were aware of 
> > this.
> 
> OK.  It's exactly the same for zsh completions as well.
> 
> BTW, you can always get a complete list of options from
>   schroot --help | grep '[[:space:]]*-'
> which might make it more flexible.  The --help output is dynamically
> generated, so is always comprehensive.  A little sed/echo trickery will
> get you a space separated list of options.

Yeah, okay, one can do this by piping schroot --help into

sed 's/\(^\|[[:space:]]\)[^[:space:]-][^[:space:]]*//g

(basically, remove every token that doesn't start with a dash).

> Also, if you wanted to be *really* clever, and if bash allows this,
> you could also complete the usernames and session names in addition
> to the chroot names.  

I'm not aware of a nice bash mechanism for completing usernames.

> Does this work for just -c or --chroot as well?

Hmm, it only did -c.  I've fixed this (and made it parse --help) in the 
attached version.

	-Tim Abbott
-------------- next part --------------
# Debian GNU/Linux schroot(1) completion.
# Copyright ? 2009 Tim Abbott <tabbott at mit.edu>
#
# schroot is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# schroot is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see
# <http://www.gnu.org/licenses/>.

have schroot &&
_schroot()
{
    local cur prev options

    COMPREPLY=()
    cur=${COMP_WORDS[COMP_CWORD]}
    prev=${COMP_WORDS[COMP_CWORD-1]}

    # Select precisely the tokens from schroot --help that begin with a dash
    options=$(schroot --help | sed 's/\(^\|[[:space:]]\)[^[:space:]-][^[:space:]]*//g')

    if [ "$prev" = "-c" ] || [ "$prev" = "--chroot" ]; then
	COMPREPLY=( $(compgen -W "$(schroot -a -l)" -- $cur) )
    else
	COMPREPLY=( $(compgen -W "$options" -- $cur) )
    fi
    return 0
}
complete -F _schroot schroot


More information about the Buildd-tools-devel mailing list