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