home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / doc / bash / completion-contrib / modules < prev    next >
Encoding:
Text File  |  2006-08-28  |  2.2 KB  |  88 lines

  1. # module completion by Ted Stern <stern@cray.com>
  2. #
  3. # $Id: modules,v 1.2 2005/07/11 17:49:14 ianmacd Exp $
  4. #
  5. # Completion for Environment Modules `module' alias.
  6. #
  7. # See http://sourceforge.net/projects/modules/
  8. #     http://modules.sourceforge.net/
  9. #
  10. # There are several versions of modules that are commonly used.  Older
  11. # Cray UNICOS systems and many other sites use 2.2.2b.  The latest GPL'd
  12. # version is 3.1.6.  But the module alias is somewhat self-documenting
  13. # via the `module help' command, so use that to print the options.
  14. #
  15. # Programmable completion might be more difficult under tcsh since the
  16. # module command is an alias, and the `module avail' command returns
  17. # its output as stderr.
  18. #
  19. type module &>/dev/null && {
  20.  
  21. _module_list ()
  22. {
  23.    local modules="$( echo $LOADEDMODULES | sed 's/:/ /g' | sort )"
  24.    compgen -W "$modules" -- $1
  25. }
  26.  
  27. _module_path ()
  28. {
  29.    local modules="$( echo $MODULEPATH | sed 's/:/ /g' | sort )"
  30.    compgen -W "$modules" -- $1
  31. }
  32.  
  33. _module_avail ()
  34. {
  35.    local modules="$( \
  36.       module avail 2>&1 | \
  37.       egrep -v '^(-|$)' | \
  38.       xargs printf '%s\n' | sort )"
  39.  
  40.    compgen -W "$modules" -- $1
  41. }
  42.  
  43. # A completion function for the module alias
  44. _module () {
  45.    local cur prev options
  46.  
  47.    COMPREPLY=()
  48.    cur=${COMP_WORDS[COMP_CWORD]}
  49.    prev=${COMP_WORDS[COMP_CWORD-1]}
  50.  
  51.    if [ $COMP_CWORD -eq 1 ] ; then
  52.       # First parameter on line -- we expect it to be a mode selection
  53.  
  54.       options="$( module help 2>&1 | egrep '^[[:space:]]*\+' | \
  55.           awk '{print $2}' | sed -e 's/|/ /g' | sort )"
  56.  
  57.       COMPREPLY=( $(compgen -W "$options" -- $cur) )
  58.  
  59.    elif [ $COMP_CWORD -eq 2 ] ; then
  60.  
  61.       case "$prev" in
  62.       @(add|display|help|load|show|whatis))
  63.           COMPREPLY=( $(_module_avail $cur) )
  64.           ;;
  65.  
  66.       @(rm|switch|swap|unload|update))
  67.           COMPREPLY=( $(_module_list $cur) )
  68.           ;;
  69.       unuse)
  70.           COMPREPLY=( $(_module_path $cur) )
  71.          ;;
  72.       esac
  73.    elif [ $COMP_CWORD -eq 3 ] ; then
  74.       case ${COMP_WORDS[1]} in
  75.          @(sw?(ap|itch)))
  76.             COMPREPLY=( $(_module_avail $cur) )
  77.             ;;
  78.       esac
  79.    fi
  80.    return 0
  81. }
  82. complete -o default -F _module module
  83. }
  84. # Local Variables:
  85. # mode:shell-script
  86. # sh-shell:bash
  87. # End:
  88.