1 # Copyright (C) 2007-2011 Andre Noll <maan@systemlinux.org>
3 # Licensed under the GPL v2. For licencing details see COPYING.
7 local prg="$1" # the program to execute
8 local cur=${COMP_WORDS[$COMP_CWORD]}
9 local line="$COMP_LINE" OLD_IFS="$IFS"
12 if [[ "$COMP_WORDBREAKS" != ' ' ]]; then
14 return 124 # try again with proper value
16 # This extracts short and long options from the help output
18 if ($1 ~ "-[a-zA-Z]," && $2 ~ "--[a-zA-Z]") {
19 print substr($1, 0, 2);
22 } else if ($1 ~ "--[a-zA-Z]") {
28 if [[ "$cur" == -* ]]; then # option
29 # Depending on whether '--' is one of the previous words we
30 # complete either on local options, i.e. those of the program
31 # to execute, or call the program to print possible completions
34 for ((i=0; i < $COMP_CWORD; i++)); do
35 [[ "${COMP_WORDS[$i]}" != '--' ]] && continue
39 if [[ "$local_opts" == "true" ]]; then
40 result="-- $($prg --help | awk "$script")"
41 COMPREPLY=($(compgen -W "$result" -- $cur))
45 # We need to call the program with --complete to get the possible
46 # completions. Before that, all local options must be discarded.
50 ((n > 0)) && ! [[ "$word" == -* ]] && break
51 line="${line##*( )}" # remove leading whitespace
52 line="${line##+([^ ])}"
55 [[ "$word" == '--' ]] && break
58 s=$((${#COMP_LINE} - ${#line})) # how many characters have been cut
59 if (($COMP_POINT > $s)); then
60 COMP_POINT=$(($COMP_POINT - $s))
65 #echo "line: $COMP_LINE, point: $COMP_POINT"
66 export COMP_LINE COMP_POINT
67 result=($($prg --complete))
69 # the last line of the output contains the options for compopt,
70 # prefixed with '-o='.
72 (($n == 0)) && return # oops, $prg did not write any output
82 filenames) compopt -o filenames;;
83 nospace) compopt -o nospace;;
87 COMPREPLY=(${result[@]})
92 _para_complete ~maan/para/para_audioc
94 complete -F _para_audioc para_audioc
98 _para_complete ~maan/para/para_client
100 complete -o default -o nospace -F _para_client para_client
101 complete -o default -o nospace -F _para_client para