| 1 | #!/bin/bash
 | 
| 2 | #
 | 
| 3 | # Demo of bash completion fallback.  Hm.  -D is not chained, and neither is -F
 | 
| 4 | # -F.
 | 
| 5 | #
 | 
| 6 | # Usage:
 | 
| 7 | #   $ bash --norc --noprofile
 | 
| 8 | #   $ . completion-demo.bash 
 | 
| 9 | 
 | 
| 10 | # NOOP
 | 
| 11 | fnone() {
 | 
| 12 |   echo -n ''
 | 
| 13 | }
 | 
| 14 | 
 | 
| 15 | f12() {
 | 
| 16 |   COMPREPLY=(f1 f2)
 | 
| 17 | }
 | 
| 18 | 
 | 
| 19 | f34() {
 | 
| 20 |   COMPREPLY=(f3 f4)
 | 
| 21 | }
 | 
| 22 | 
 | 
| 23 | complete-file() {
 | 
| 24 |   local cur="${COMP_WORDS[COMP_CWORD]}"
 | 
| 25 |   # Hm no trailing slash here.
 | 
| 26 |   COMPREPLY=( $(compgen -A file -- "${cur}") )
 | 
| 27 | }
 | 
| 28 | 
 | 
| 29 | # Use -X to filter
 | 
| 30 | complete-sh() {
 | 
| 31 |   local cur="${COMP_WORDS[COMP_CWORD]}"
 | 
| 32 |   # Hm no trailing slash here.
 | 
| 33 |   COMPREPLY=( $(compgen -A file -X '!*.sh' -o plusdirs -- "${cur}") )
 | 
| 34 | }
 | 
| 35 | 
 | 
| 36 | # default completion
 | 
| 37 | complete -F f12 -F f34 -D
 | 
| 38 | 
 | 
| 39 | # empty completion
 | 
| 40 | # Oops, does NOT fall back on f34
 | 
| 41 | complete -F f12 -F f34 -F fnone -E
 | 
| 42 | 
 | 
| 43 | # Directory names will be completed with trailing slash; this is default readline behavior.
 | 
| 44 | complete -A file foo
 | 
| 45 | 
 | 
| 46 | # Hm no trailing slash here.  Lame.
 | 
| 47 | complete -F complete-sh bar
 | 
| 48 | 
 | 
| 49 | # Aha!  This adds trailing slash.  The problem is that if you are completing
 | 
| 50 | # with -D, you may or may not be completing with a file!  Need to use comopt?
 | 
| 51 | complete -F complete-sh -o filenames -o bashdefault barf
 | 
| 52 | 
 | 
| 53 | echo 'Installed completions'
 | 
| 54 | 
 |