File: lmod_bash_completions

package info (click to toggle)
lmod 6.6-0.2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 16,716 kB
  • ctags: 1,336
  • sloc: sh: 3,714; python: 967; makefile: 718; tcl: 695; perl: 495; csh: 85; ansic: 35
file content (297 lines) | stat: -rw-r--r-- 8,527 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297

_module_avail() {
  local redirect="${LMOD_REDIRECT:-false}"
  if ${redirect,,}; then
    #
    # Lmod output already redirected to STDOUT: do not redirect again; just parse.
    #
    @PKG@/libexec/lmod bash -t avail 2>/dev/null | bash | sed ' /:$/d; s#/*$##g;'
  else
    #
    # Lmod output goes to STDERR by default, but for bash redirection we need it on STOUT.
    #
    @PKG@/libexec/lmod bash -t avail 2>&1 >/dev/null | sed ' /:$/d; s#/*$##g;'
  fi
}

_module_dir() {
  local cur="${1}" pattern i
  if [[ "${cur:0:1}" == '$' ]]; then
    pattern='^\$[[:alnum:]_]+\/$'
    if [[ ${cur} =~ ${pattern} ]]; then
      #
      # Resolve variable into value.
      #
      eval COMPREPLY[0]="${cur}"
    else
      #
      # Complete exported environment variables
      # and remove any matches that don't contain a directory.
      #
      COMPREPLY=( $( compgen -v -P '$' -- "${cur:1}" ) )
      local -a FILTEREDCOMPREPLY
      for ((i=0; i < ${#COMPREPLY[@]}; i++)); do
        pattern='^\$[[:alnum:]_]+$'
        if [[ ${COMPREPLY[$i]} =~ ${pattern} ]]; then
          eval local env_val="${COMPREPLY[$i]}"
          if [ -d "${env_val}" ]; then
            FILTEREDCOMPREPLY+=(${COMPREPLY[$i]})
          fi
        fi
      done
      COMPREPLY=( ${FILTEREDCOMPREPLY[@]} )
    fi
  elif [[ "${cur:0:1}" == '~' ]]; then
    if [[ "${cur}" != "${cur//\/}" ]]; then
      #
      # ${cur} contains a slash:
      # 1: Remove * including and after first slash (/), i.e. "~a/b"
      #    becomes "~a".  Double quotes allow eval.
      # 2: Remove * before the first slash (/), i.e. "~a/b"
      #    becomes 'b'.  Single quotes prevent eval.
      #                 +-----1-----+ +-----2----+
      eval COMPREPLY[0]="${cur%%\/*}"/'${cur#*\/}'
    else
      #
      # ${cur} doesn't contain slash.
      #
      eval COMPREPLY[0]="~"
    fi
  else
    #
    # Complete directories.
    #
    COMPREPLY=( $(compgen -d -- "${cur}") )
  fi
  if [[ ${#COMPREPLY[@]} -eq 1 ]]; then
    pattern='\/$'
    if [[ -d "${COMPREPLY[0]}" && ! "${COMPREPLY[0]}" =~ ${pattern} ]]; then
      COMPREPLY[0]="${COMPREPLY[0]}/"
    fi
    compopt -o nospace
  fi
}

_module_spider() {
  local redirect="${LMOD_REDIRECT:-false}"
  if ${redirect,,}; then
    @PKG@/libexec/lmod bash -t spider 2>/dev/null | bash
  else
    @PKG@/libexec/lmod bash -t spider 2>&1 >/dev/null
  fi
}

_module_loaded_modules() {
  local redirect="${LMOD_REDIRECT:-false}"
  if ${redirect,,}; then
    @PKG@/libexec/lmod bash -t list 2>/dev/null | bash | sed ' /^ *$/d; /:$/d; s#/*$##g;'
  else
    @PKG@/libexec/lmod bash -t list 2>&1 >/dev/null | sed ' /^ *$/d; /:$/d; s#/*$##g;'
  fi
}

_module_savelist() {
  local redirect="${LMOD_REDIRECT:-false}"
  if ${redirect,,}; then
    @PKG@/libexec/lmod bash -t savelist 2>/dev/null | bash
  else
    @PKG@/libexec/lmod bash -t savelist 2>&1 >/dev/null
  fi
}

_module_loaded_modules_negated() {
  local redirect="${LMOD_REDIRECT:-false}"
  if ${redirect,,}; then
    @PKG@/libexec/lmod bash -t list 2>/dev/null | bash | sed ' /^ *$/d; /:$/d; s#/*$##g; s|^|-|g;'
  else
    @PKG@/libexec/lmod bash -t list 2>&1 >/dev/null | sed ' /^ *$/d; /:$/d; s#/*$##g; s|^|-|g;'
  fi
}

_module_mcc() {
  local redirect="${LMOD_REDIRECT:-false}"
  if ${redirect,,}; then
    @PKG@/libexec/lmod bash -t collection 2>/dev/null | bash
  else
    @PKG@/libexec/lmod bash -t collection 2>&1 >/dev/null
  fi
}

_module_not_yet_loaded() {
  comm -23  <(_module_avail|sort)  <(_module_loaded_modules|sort)
}

_module_long_arg_list() {
  local cur="${1}" i
  if [[ ${COMP_WORDS[COMP_CWORD-2]} == sw* ]]; then
    COMPREPLY=( $(compgen -W "$(_module_not_yet_loaded)" -- "${cur}") )
    return
  fi
  for ((i = COMP_CWORD - 1; i > 0; i--)); do
    case ${COMP_WORDS[${i}]} in
      add|load)
        COMPREPLY=( $(compgen -W "$(_module_not_yet_loaded)" -- "${cur}") )
        break
        ;;
      rm|remove|unload|switch|swap)
        COMPREPLY=( $(compgen -W "$(_module_loaded_modules)" -- "${cur}") )
        break
        ;;
    esac
  done
}

_module() {
  local cur="${2}" prev="${3}" cmds opts
  
  COMPREPLY=()
  
  cmds="add avail delete help keyword list load purge rm restore \
        save show spider swap unload unuse update use whatis"
  
  opts="-d -D -h -q -t -v -w -s --style --expert --quiet --help \
        --quiet --terse --version --default --width -r --regexp --mt"
  
  case "${prev}" in
    add|load|try-load)
      COMPREPLY=( $(compgen -W "$(_module_not_yet_loaded)" -- "${cur}") )
      ;;
    rm|remove|unload|switch|swap)
      COMPREPLY=( $(compgen -W "$(_module_loaded_modules)" -- "${cur}") )
      ;;
    restore)
      COMPREPLY=( $(compgen -W "$(_module_savelist)" -- "${cur}") )
      ;;
    spider)
      COMPREPLY=( $(compgen -W "$(_module_spider)" -- "${cur}") )
      ;;
    unuse)
      COMPREPLY=( $(IFS=: compgen -W "${MODULEPATH}" -- "${cur}") )
      ;;
    use|*-a*)
      _module_dir "${cur}"
      ;;
    help|show|whatis)
      COMPREPLY=( $(compgen -W "$(_module_avail)" -- "${cur}") )
      ;;
    collection|cc|mcc)
      COMPREPLY=( $(compgen -W "$(_module_mcc)" -- "${cur}") )
      ;;
    *)
      if [ ${COMP_CWORD} -gt 2 ]; then
        _module_long_arg_list "${cur}"
      else
        case "${cur}" in
          # The mappings below are optional abbreviations for convenience.
          ls)
            COMPREPLY='list'
            ;;
          sw*)
            COMPREPLY='swap'
            ;;
          -*)
            COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
            ;;
          *)
            COMPREPLY=( $(compgen -W "${cmds}" -- "${cur}") )
            ;;
        esac
      fi
      ;;
  esac
}
complete -F _module module

_ml() {
  local cur="${2}" prev="${3}" cmds opts found
  
  COMPREPLY=()
  
  cmds="add avail delete help keyword list load purge rm restore \
        save sl show spider swap unload unuse update use whatis"
  
  opts="-d -D -h -q -t -v -w -s --style --expert --quiet --help \
        --quiet --terse --version --default --Verbose --width -r --regexp --mt"
  
  case "${prev}" in
    rm|remove|unload|switch|swap)
      COMPREPLY=( $(compgen -W "$(_module_loaded_modules)" -- "${cur}") )
      ;;
    restore)
      COMPREPLY=( $(compgen -W "$(_module_savelist)" -- "${cur}") )
      ;;
    spider)
      COMPREPLY=( $(compgen -W "$(_module_spider)" -- "${cur}") )
      ;;
    unuse)
      COMPREPLY=( $(IFS=: compgen -W "${MODULEPATH}" -- "${cur}") )
      ;;
    use|*-a*)
      _module_dir "${cur}"
      ;;
    help|show|whatis)
      COMPREPLY=( $(compgen -W "$(_module_avail)" -- "${cur}") )
      ;;
    *)
      case "${cur}" in
        -*)
          if [ ${COMP_CWORD} -eq 1 ]; then
            COMPREPLY=( $(compgen -W "${opts} $(_module_loaded_modules_negated)" -- "${cur}") )
          else
            COMPREPLY=( $(compgen -W "        $(_module_loaded_modules_negated)" -- "${cur}") )
          fi
          ;;
        *)
          if [ ${COMP_CWORD} -eq 1 ]; then
            case "${cur}" in
              ls)
                COMPREPLY='list'
                ;;
              sw*)
                COMPREPLY='swap'
                ;;
              *)
                COMPREPLY=( $(compgen -W "${cmds} $(_module_avail)" -- "${cur}") )
                ;;
            esac
          else
            if [[ ${COMP_WORDS[COMP_CWORD-2]} == sw* ]]; then
              COMPREPLY=( $(compgen -W "$(_module_not_yet_loaded)" -- "${cur}") )
            else
              for ((i = COMP_CWORD - 1; i > 0; i--)); do
                case ${COMP_WORDS[$i]} in
                  show|whatis)
                    COMPREPLY=( $(compgen -W "$(_module_avail)" -- "${cur}") )
                    found=1
                    break
                    ;;
                  rm|remove|unload)
                    COMPREPLY=( $(compgen -W "$(_module_loaded_modules)" -- "${cur}") )
                    found=1
                    break
                    ;;
                  spider)
                    COMPREPLY=( $(compgen -W "$(_module_spider)" -- "${cur}") )
                    found=1
                    break
                    ;;
                esac
              done
              if [ -z "${found}" ]; then
                COMPREPLY=( $(compgen -W "$(_module_avail)" -- "${cur}") )
              fi
            fi
          fi
          ;;
      esac
  esac
}
complete -F _ml ml

# Local Variables:
# mode: shell-script
# indent-tabs-mode: nil
# sh-basic-offset: 2
# sh-indent-comment: t
# End:
# ex: ts=2 sw=2 et filetype=sh