| [9fd196] | 1 | # bash_completion for molecuilder | 
|---|
|  | 2 | # | 
|---|
|  | 3 | # gives a list of all available actions, gives list of options for the last action | 
|---|
|  | 4 |  | 
|---|
|  | 5 | function _molecuilder_option() | 
|---|
|  | 6 | { | 
|---|
|  | 7 | local cmd="${1##*/}" | 
|---|
|  | 8 | local word=${COMP_WORDS[COMP_CWORD]} | 
|---|
|  | 9 | local line=${COMP_LINE} | 
|---|
|  | 10 | option=`echo $line | awk -F"--" {'print $NF'} | tr -d \ ` | 
|---|
|  | 11 | previousoption=`echo $line | awk -F"--" {'print $(NF-1)'} | tr -d \ ` | 
|---|
|  | 12 | #echo "current option is '$word', line is '$line', and cmd is '$cmd', last option is '$option', and last but one is '$previousoption'." | 
|---|
|  | 13 | COMPREPLY=() | 
|---|
|  | 14 | # if current option is molecuilder, give all available actions | 
|---|
|  | 15 | if test x"$option" = x"molecuilder"; then | 
|---|
|  | 16 | # "molecuilder " | 
|---|
|  | 17 | COMPREPLY=($($cmd --help | grep "^'.*'" | awk -F":" {'print "--"$1'} | tr -d \')) | 
|---|
|  | 18 | else | 
|---|
|  | 19 | # if current argument is void, look at previous (if its not molecuilder) | 
|---|
|  | 20 | if test x"$option" = x""; then | 
|---|
|  | 21 | if test x"$previousoption" = x"molecuilder"; then | 
|---|
|  | 22 | # "molecuilder --" | 
|---|
|  | 23 | COMPREPLY=($($cmd --help | grep "^'.*'" | awk -F":" {'print "--"$1'} | tr -d \')) | 
|---|
|  | 24 | else | 
|---|
|  | 25 | # "molecuilder --some-action --" | 
|---|
|  | 26 | COMPREPLY=($($cmd --help | grep "^'$previousoption.*'" | awk -F":" {'print "--"$1'} | tr -d \')) | 
|---|
|  | 27 | if  test ${#COMPREPLY[*]} -eq 0; then | 
|---|
|  | 28 | # "molecuilder --some-option --" | 
|---|
|  | 29 | COMPREPLY=($($cmd --help | grep "^'.*'" | awk -F":" {'print "--"$1'} | tr -d \')) | 
|---|
|  | 30 | else | 
|---|
|  | 31 | COMPREPLY=($($cmd --help --actionname "$previousoption" | grep Option\  | awk -F"'" {'print "--"$2'} | tr -d \')) | 
|---|
|  | 32 | # if action has no options, give list of actions | 
|---|
|  | 33 | if  test ${#COMPREPLY[*]} -eq 0; then | 
|---|
|  | 34 | COMPREPLY=($($cmd --help | grep "^'.*'" | awk -F":" {'print "--"$1'} | tr -d \')) | 
|---|
|  | 35 | fi | 
|---|
|  | 36 | fi | 
|---|
|  | 37 | fi | 
|---|
|  | 38 | elif test -z `echo "$word" 2>/dev/null | grep "\-\-"`; then | 
|---|
|  | 39 | # "molecuilder somefile/value" | 
|---|
|  | 40 | COMPREPLY=( $( compgen -W "$( ls --color=n -1 ${word}*.in ${word}*.pdb ${word}*.pcp ${word}*.psi ${word}*.data ${word}*.xyz 2>/dev/null| sed -e 's/ /\\ /g' )" -- $cur )) | 
|---|
|  | 41 | else | 
|---|
|  | 42 | # "molecuilder --something" | 
|---|
|  | 43 | COMPREPLY=($($cmd --help | grep "^'$option.*'" | awk -F":" {'print "--"$1'} | tr -d \')) | 
|---|
|  | 44 | # check whether its an action | 
|---|
|  | 45 | if test ${#COMPREPLY[*]} -eq 1; then | 
|---|
|  | 46 | # check whether it's same as option | 
|---|
|  | 47 | if test x"${COMPREPLY[0]}" = x"--$option"; then | 
|---|
|  | 48 | # "molecuilder --an-action" | 
|---|
|  | 49 | #echo "Giving help for option $option" | 
|---|
|  | 50 | COMPREPLY=($($cmd --help --actionname "$option" | grep Option\  | awk -F"'" {'print "--"$2'} | tr -d \')) | 
|---|
|  | 51 | # if action has no options, give list of actions | 
|---|
|  | 52 | if  test ${#COMPREPLY[*]} -eq 0; then | 
|---|
|  | 53 | COMPREPLY=($($cmd --help | grep "^'.*'" | awk -F":" {'print "--"$1'} | tr -d \')) | 
|---|
|  | 54 | fi | 
|---|
|  | 55 | fi | 
|---|
|  | 56 | fi | 
|---|
|  | 57 | # if the "action" is unknown, it must be actually an option (segment), give all available options from previous action | 
|---|
|  | 58 | if test ${#COMPREPLY[*]} -eq 0; then | 
|---|
|  | 59 | # "molecuilder --an-action --opt.." | 
|---|
|  | 60 | COMPREPLY=($($cmd --help --actionname "$previousoption" | grep Option\ | grep $option | awk -F"'" {'print "--"$2'} | tr -d \')) | 
|---|
|  | 61 | # if action has no options, give list of actions | 
|---|
|  | 62 | if  test ${#COMPREPLY[*]} -eq 0; then | 
|---|
|  | 63 | COMPREPLY=($($cmd --help | grep "^'.*'" | awk -F":" {'print "--"$1'} | tr -d \')) | 
|---|
|  | 64 | else | 
|---|
|  | 65 | #if we have a complete option, give again all available actions | 
|---|
|  | 66 | if test x"${COMPREPLY[0]}" = x"--$option"; then | 
|---|
|  | 67 | # "molecuilder --an-action --one-of-its-options" | 
|---|
|  | 68 | COMPREPLY=($($cmd --help | grep "^'.*'" | awk -F":" {'print "--"$1'} | tr -d \')) | 
|---|
|  | 69 | fi | 
|---|
|  | 70 | fi | 
|---|
|  | 71 | fi | 
|---|
|  | 72 | fi | 
|---|
|  | 73 | fi | 
|---|
|  | 74 | #echo "returning ${COMPREPLY[*]}." | 
|---|
|  | 75 | return 0 | 
|---|
|  | 76 | } | 
|---|
|  | 77 |  | 
|---|
|  | 78 | complete -F _molecuilder_option molecuilder | 
|---|