aboutsummaryrefslogtreecommitdiffstats
path: root/examples/complete/complete.gnu-longopt
blob: c55b43651247102a204b66262ce991297908fc8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#
# Originally from:
#
#Message-ID: <3B13EC65.179451AE@wanadoo.fr>
#Date: Tue, 29 May 2001 20:37:25 +0200
#From: Manu Rouat <emmanuel.rouat@wanadoo.fr>
#Subject: [bash] Universal command options completion?
#
#
#In the recent versions of bash (after 2.04) programmable 
#completion is available. A useful completion function
#is , for a particular command, to enumerate all flags
#that can be used in the command. Now, most GNU unix 
#commands have so-called 'long options' for example:
#
#ls --color=always --no-group --size
#
#and these are all listed when you issue a '--help' flag.
#So the idea is to use that, then parse the output of the
#'--help' and reinject this to compgen. The basis of the 
#following 'universal' completion funtion was the _configure_func'
#written by Ian McDonnald (or is it Chet Ramey ?)
#A dedicated function will always be better, but this is quite
#convenient. I chose to use 'long options' because they are
#easy to parse and explicit too (it's the point I guess...)
#Lots of room for improvement !

_longopt_func ()
{
    case "$2" in
	-*)	;;
	*)	return ;;
    esac

    case "$1" in
	\~*)	eval cmd=$1 ;;
	*)	cmd="$1" ;;
    esac
    COMPREPLY=( $("$cmd" --help | sed  -e '/--/!d' -e 's/.*--\([^ ]*\).*/--\1/'| \
grep ^"$2" |sort -u) )
}

complete  -o default -F _longopt_func ldd wget bash id info # some examples that work