[Bash-completion-devel] [Proposal] A simple Bash library to simplify completions
Marc Coiffier
marc.coiffier at ujf-grenoble.fr
Thu Apr 2 11:30:47 UTC 2015
Hello all,
I’ve recently written a few Bash functions to help me define completions
for some personal scripts and commands, and I would appreciate your
opinion and suggestions for improvements. As no explanation beats a good
example, here is a small example of a completion description with these
functions (they all start with ‘C.’ to facilitate reading and to avoid
name clashes) :
|source /usr/share/bash/bashcomps.shl # I had some qualms about putting a shell script in /usr/lib
function C.example() {
C.alt C.describing "a color" C.wordOf 5 red green blue black white \
C.describing "a file" C.fileIn -f . \
C.normal "$@"
C.alt C.describing "invert colors" C.flagOf 2 -I --invert \
C.normal "$@"
}
C.defcomp 'C.repeat C.example' example
|
These few lines define a completion function that completes a color then
a file, or a single flag, repeated ad infinitum. This is of course a
contrived example, but the approach can trivially be extended to more
useful completions.
The library predefines a small number of combinators, that can then be
used to construct all sorts of complex structures within the arguments.
For example, you can complete a URL by simply combining a protocol, a
|://|, a hostname, a |/| and a path. In other words :
|function C.url() {
local prevSuf="$COMP_SUFFIX"
C.suffixed '://' C.wordOf 3 http https ftp \
C.suffixed '/' C.hostname \
C.suffixed "$prevSuf" C.any "$@"
}
|
After that, completing a URL as part of an option becomes a piece of cake :
|function C.urlOpt() {
C.optOf 2 --url -U C.url "$@"
}
|
Of course, it is still possible to write your own generation functions
and plug them into a parsing chain using a function called |C.argument|
(in fact, all other helpers are defined in terms of |C.argument|) but I
won’t bore you with the details here, that’s what the man page is for ;-)
If you want to test it for yourself, you can download it here
<https://pool.coiffier.net/packages/bashcomps.tgz> (for a
distribution-independent .tar.gz version), or directly as a .deb
<https://pool.coiffier.net/packages/bashcomps.deb> or .tar.xz
<https://pool.coiffier.net/packages/bashcomps.tar.xz> if your distro
supports it.
Anyway, thank you for reading this far, and for any feedback or opinion
you might have :-)
Yours indubitably,
Marc Coiffier
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.alioth.debian.org/pipermail/bash-completion-devel/attachments/20150402/3eae79f0/attachment.html>
More information about the Bash-completion-devel
mailing list