function _docopt_wordlist {
if [ -z "$DOCOPT_WORDLIST_BIN" ]; then
DOCOPT_WORDLIST_BIN=/usr/local/bin/docopt-wordlist
fi
cword=$(_get_cword)
cmd="${COMP_WORDS[0]}"
wordlist=$("$cmd" --help 2>&1 | "$DOCOPT_WORDLIST_BIN")
gen "$cword" "$wordlist"
}
function _docopt_wordlist_commands {
if [ -z "$DOCOPT_WORDLIST_BIN" ]; then
DOCOPT_WORDLIST_BIN=/usr/local/bin/docopt-wordlist
fi
cword=$(_get_cword)
if [ "$COMP_CWORD" = 1 ]; then
cmd="${COMP_WORDS[0]}"
wordlist=$("$cmd" --help 2>&1 | "$DOCOPT_WORDLIST_BIN")
wordlist+=" $("$cmd" --list | egrep '^ +\w' | awk '{print $1}')"
gen "$cword" "$wordlist"
else
for ((i="$COMP_CWORD"; i >= 1; i++)); do
cmd="${COMP_WORDS[@]::$i}"
wordlist=$($cmd --help 2>&1 | "$DOCOPT_WORDLIST_BIN")
if [ $? = 0 ]; then
gen "$cword" "$wordlist"
break
fi
done
fi
}
function gen {
cword="$1"
wordlist="$2"
if [[ "$cword" = .* || "$cword" = /* ]]; then
COMPREPLY=($(compgen -A file -- "$cword"))
else
COMPREPLY=($(compgen -W "$wordlist" -- "$cword"))
if [ -z "$COMPREPLY" ]; then
COMPREPLY=($(compgen -A file -- "$cword"))
fi
fi
}