| Bri Hatch | Personal | Work |
|---|---|---|
| bri@ifokr.org |
ExtraHop Networks bri@extrahop.com |
$ pa<tab> pagesize parldyn patch pagestuff pass pathchk par.pl passwd pax parl paste $ pas<tab> pass passwd paste $ passw<tab> $ passwd █ $ pag<tab> pagesize pagestuff $ pages█
$ grep --l<tab> --label= --line-buffered --line-number --line-regexp $ grep --li<tab> $ grep --line-<tab> --line-buffered --line-number --line-regexp $ grep --line-b<tab> $ grep --line-buffered █
$ ls foofi<tab> $ ls foofile.txt █ $ ls foofile.txt bar<tab> $ ls foofile.txt barfile.txt █
$ scp foofi<tab> $ scp foofile.txt █ $ scp foofile.txt bar<tab> $ scp foofile.txt barfile.txt █ $ scp foofile.txt barfile.txt loca<tab>
$ scp foofi<tab> $ scp foofile.txt █ $ scp foofile.txt bar<tab> $ scp foofile.txt barfile.txt █ $ scp foofile.txt barfile.txt loca<tab> $ scp foofile.txt barfile.txt localhost:█ $ scp foofile.txt barfile.txt localhost:<tab>
$ scp foofi<tab> $ scp foofile.txt █ $ scp foofile.txt bar<tab> $ scp foofile.txt barfile.txt █ $ scp foofile.txt barfile.txt loca<tab> $ scp foofile.txt barfile.txt localhost:█ $ scp foofile.txt barfile.txt localhost:<tab> $ scp foofile.txt barfile.txt localhost:/home/wbagg/█
(There's always a relevant xkcd...)
$ source frobnicate_completion.sh $ frobnicate <tab> --bar --baz --foo $ frobnicate --b<tab> --bar --baz $ frobnicate --ba<tab> --bar --baz $ frobnicate --baz<tab> $ frobnicate --baz █ $ frobnicate --baz <tab> --bar --baz --foo $ frobnicate --baz --f<tab> $ frobnicate --baz --foo █
complete.bashrc, ~/.config/bash_completion,
/usr/share/bash-completion/completions/)
COMP_WORDSCOMP_CWORDCOMPREPLY
COMP_LINECOMP_POINTcompgencompgen -W "-a --bunch --of --possible --words" -- "--what-they-said"
completecomplete -F functioname programname
$ source frobnicate_completion.sh
$ cat frobnicate_completion.sh
_frobnicate () {
COMPREPLY=(
$(compgen -W "--foo --bar --baz" -- "${COMP_WORDS[$COMP_CWORD]}")
)
}
complete -F _frobnicate frobnicate
$ frobnicate --b<tab>
--bar --baz
$ frobnicate --f<tab>
$ frobnicate --foo █
...
$ frobnicate -h usage: frobnicate [options] options: -h show this help message and exit -v be verbose -d show debugging output --assignee ASSIGNEE person who should be assigned the issue --reporter REPORTER person who reported the issue --description "..." The description of the issue
_frobnicate () {
local cur prev
local allargs possibilities
cur="${COMP_WORDS[$COMP_CWORD]}"
prev="${COMP_WORDS[$(( $COMP_CWORD - 1 ))]}"
allargs=" --reporter --assignee -h -v -d --description "
# lots of logic
# that sets the 'possibilities'
# variable here
if [ -n "$possibilities" ] ; then
COMPREPLY=(
$(compgen -W "$possibilities" -- "$cur" )
)
fi
}
complete -F _frobnicate frobnicate
allargs=" --reporter --assignee -h -v -d --description "
if [ "$COMP_CWORD" = 1 ] ; then
possibilities="$allargs"
# Our previous argument is explicitly one of the
# options we support!
elif [[ "$allargs" == *" $prev "* ]] ; then
# Lots of stuff to handle each of the different options
# as appropriate and set the "possibliities" variable
else
possibilities="$allargs"
fi
# Our previous option expects an arbitrary string
# No autocomplete is possible
if [ "$prev" = "--description" ] ; then
return
# they want help? try to get them to hit 'enter' now
elif [ "$prev" = "-h" ] ; then
return
# Our previous option was one of the options
# that does not take any value, so all options
# are on the table
elif [ "$prev" = "-v" -o "$prev" = "-d" ] ; then
possibilities="$allargs"
# Our previous option expects a username
elif [ "$prev" = "--reporter" -o "$prev" = "--assignee" ] ; then
possibilities=$(getent passwd | awk -F: '$3 > 1000 {print $1}')
fi
| Personal | Work |
|---|---|
| Bri Hatch bri@ifokr.org |
Bri Hatch |
Copyright 2022, Bri Hatch, Creative Commons BY-NC-SA License
compopt -o [complete option]complete defaults
complete [-abcdefgjksuv] [-o comp-option] [-A action]
[-G globpat] [-W wordlist] [-P prefix] [-S suffix]
[-X filterpat] [-F function] [-C command] name [name ...]
complete -o bashdefaultcomplete -o defaultreadline's default completioncomplete -o dirnamescomplete -o filenamescomplete -o nospacecomplete -o plusdirseval "$(kubectl completion bash)"
shtab for python generates from your parser object
$ shtab --shell=bash myprogram.main.parser # Or in the code import shtab print(shtab.complete(parser, shell="bash"))