Improve new {append,prepend,remove}-path commands

Refactor code to call from the module command line modulefile-specific
commands. These commands can only be called from the command line, not
when calling 'module' procedure from a modulefile, to avoid any
ambiguity.

When parsing command line arguments, check context to know if '-d' means
'--default' (avail command) or '--delim' (*-path commands).

Add the new commands to the existing shell completion scripts (bash,
tcsh, fish, zsh).

Document new commands in the module.1 man page.

Acknowledgment: this development has been made and funded within the
framework of the PRACE Fifth Implementation Phase (PRACE-5IP) project
(http://www.prace-ri.eu/). PRACE-5IP receives funding from the EU's
Horizon 2020 research and innovation programme (2014-2020) under grant
agreement no. 730913.
This commit is contained in:
Xavier Delaruelle
2017-11-25 14:51:57 +01:00
parent 27c4e38165
commit 62334ed0f7
6 changed files with 85 additions and 29 deletions

View File

@@ -432,6 +432,24 @@ Module Sub-Commands
Print path of available *modulefiles* matching argument.
**append-path** [-d C|--delim C|--delim=C] variable value
Append *value* to environment *variable*. The *variable* is a colon, or
*delimiter*, separated list. See **append-path** in the :ref:`modulefile(4)`
man page for further explanation.
**prepend-path** [-d C|--delim C|--delim=C] variable value
Prepend *value* to environment *variable*. The *variable* is a colon, or
*delimiter*, separated list. See **prepend-path** in the :ref:`modulefile(4)`
man page for further explanation.
**remove-path** [-d C|--delim C|--delim=C] variable value
Remove *value* from the colon, or *delimiter*, separated list in environment
*variable*. See **remove-path** in the :ref:`modulefile(4)` man page for
further explanation.
Modulefiles
^^^^^^^^^^^

View File

@@ -91,14 +91,15 @@ _module() {
COMPREPLY=()
cmds="add apropos aliases avail display help\
initadd initclear initlist initprepend initrm\
keyword list load path paths purge refresh reload restore\
rm save savelist saveshow saverm search show source swap\
switch test unload unuse use whatis"
cmds="add apropos aliases avail append-path display help\
initadd initclear initlist initprepend initrm keyword\
list load path paths purge prepend-path refresh reload\
restore rm remove-path save savelist saveshow saverm search\
show source swap switch test unload unuse use whatis"
opts="-D -h -V --debug --help --version"
list_opts="-l -t --long --terse"
path_opts="-d --delim"
avail_opts="-d -L -l -t --default --latest --long --terse"
case "$prev" in
@@ -115,6 +116,8 @@ _module() {
COMPREPLY=( $(compgen -W "$(_module_avail)" -- "$cur") );;
-h|--help|-V|--version|aliases|apropos|keyword|purge|refresh|reload|search|source)
;;
append-path|prepend-path|remove-path)
COMPREPLY=( $(compgen -W "$path_opts" -- "$cur") );;
initadd|initclear|initlist|initprepend|initrm)
;;
*) if test $COMP_CWORD -gt 2

View File

@@ -2,7 +2,7 @@
function __fish_module_no_subcommand --description 'Test if modulecmd has yet to be given the subcommand'
for i in (commandline -opc)
if contains -- $i add load rm unload swap switch show display list avail aliases use unuse refresh reload purge source whatis apropos keyword search test save restore saverm saveshow savelist initadd initprepend initrm initswitch initlist initclear path paths
if contains -- $i add load rm unload swap switch show display list avail aliases use unuse refresh reload purge source whatis apropos keyword search test save restore saverm saveshow savelist initadd initprepend initrm initswitch initlist initclear path paths append-path prepend-path remove-path
return 1
end
end
@@ -92,6 +92,9 @@ complete -f -n '__fish_module_no_subcommand' -c module -a 'initlist' --descripti
complete -f -n '__fish_module_no_subcommand' -c module -a 'initclear' --description 'Clear all modulefiles from init file'
complete -f -n '__fish_module_no_subcommand' -c module -a 'path' --description 'Print modulefile path'
complete -f -n '__fish_module_no_subcommand' -c module -a 'paths' --description 'Print path of matching available modules'
complete -f -n '__fish_module_no_subcommand' -c module -a 'append-path' --description 'Append value to environment variable'
complete -f -n '__fish_module_no_subcommand' -c module -a 'prepend-path' --description 'Prepend value to environment variable'
complete -f -n '__fish_module_no_subcommand' -c module -a 'remove-path' --description 'Remove value from environment variable'
complete -c module -s V -l version --description 'Module version'
complete -c module -s D -l debug --description 'Enable debug messages'

View File

@@ -38,11 +38,12 @@ alias _module_not_yet_loaded '\\
alias _module_modulepath 'echo ${MODULEPATH} | sed '"'"'s/:/\n/g;'"'"' '
set module_cmds = "add apropos aliases avail display help initadd initclear initlist initprepend initrm keyword list load path paths purge refresh reload restore rm save savelist saveshow saverm search show source swap switch test unload unuse use whatis"
set module_cmds = "add apropos aliases avail append-path display help initadd initclear initlist initprepend initrm keyword list load path paths purge prepend-path refresh reload restore rm remove-path save savelist saveshow saverm search show source swap switch test unload unuse use whatis"
set module_opts = "-D -h -V --debug --help --version"
set module_list_opts = "-l -t --long --terse"
set module_avail_opts = "-d -L -l -t --default --latest --long --terse"
set module_path_opts = "-d --delim"
complete module 'n/help/`_module_avail`/' \
'n/add/`_module_not_yet_loaded`/' \
@@ -81,6 +82,9 @@ complete module 'n/help/`_module_avail`/' \
'n/initrm/n/' \
'n/initlist/n/' \
'n/initclear/n/' \
"n/append-path/(${module_path_opts})/" \
"n/prepend-path/(${module_path_opts})/" \
"n/remove-path/(${module_path_opts})/" \
'n/-h/n/' \
'n/--help/n/' \
'n/-V/n/' \

View File

@@ -114,6 +114,9 @@ _module() {
'path:Print modulefile path'
'paths:Print path of matching available modules'
'source:Execute scriptfile(s)'
'append-path:Append value to environment variable'
'prepend-path:Prepend value to environment variable'
'remove-path:Remove value from environment variable'
)
# show commands only with compatible options
if (( !$+opt_args[-h] && !$+opt_args[--help] \
@@ -172,6 +175,11 @@ _module() {
_alternative 'avail-mods:modulefiles:{_module_avail_mods}' \
&& ret=0
;;
(append-path|prepend-path|remove-path)
_arguments \
'(-d --delim)'{-d,--delim}'[Path element separator]' \
&& ret=0
;;
esac
;;
esac

View File

@@ -1472,6 +1472,19 @@ proc module {command args} {
popCommandName
}
}
{^(prepend|append|remove)-path$} {
if {$topcall} {
if {[llength $args] < 2 || [llength $args] > 4} {
set errormsg "Unexpected number of args for '$command' command"
} else {
eval cmdModuleResurface $command $args
}
} else {
# no call other than from top level to to conflict with modulefile
# specific Tcl commands
set errormsg "${msgprefix}Command '$command' not supported"
}
}
. {
set errormsg "${msgprefix}Invalid command '$command'"
}
@@ -5451,12 +5464,15 @@ proc cmdModuleInit {args} {
}
}
proc cmdModuleImm {args} {
# provide access to modulefile specific commands from the command-line, making
# them standing as a module sub-command (see module procedure)
proc cmdModuleResurface {cmd args} {
reportDebug "cmdModuleResurface: cmd='$cmd', args='$args'"
pushMode "load"
#puts stderr $args
eval $args
pushCommandName $cmd
eval $cmd $args
popCommandName
popMode
}
@@ -5542,11 +5558,11 @@ Shell's initialization files handling commands:
initswitch mod1 mod2 Switch mod1 with mod2 from init file
initclear Clear all modulefiles from init file
Module commands available from the command line:
prepend-path VAR PATH [separator]
append-path VAR PATH [separator]
remove-path VAR PATH [separator]
Path-like variable manipulation
Environment direct handling commands:
prepend-path [-d c] var value Prepend value to environment variable
append-path [-d c] var value Append value to environment variable
remove-path [-d c] var value Remove value from environment variable
Other commands:
help [modulefile ...] Print this or modulefile(s) help info
display | show modulefile [...] Display information about modulefile(s)
@@ -5610,6 +5626,7 @@ if {[catch {
# extract options and command switches from other args
set otherargv {}
set ddelimarg 0
foreach arg [lrange $argv 1 end] {
if {[info exists ignore_next_arg]} {
unset ignore_next_arg
@@ -5639,15 +5656,26 @@ if {[catch {
set show_oneperline 0
}
{-d} - {--default} {
set show_filter "onlydefaults"
# in case of *-path command, -d means --delim
if {$arg eq "-d" && $ddelimarg} {
lappend otherargv $arg
} else {
set show_filter "onlydefaults"
}
}
{-L} - {--latest} {
set show_filter "onlylatest"
}
{-a} - {--append} - {-append} - {-p} - {--prepend} - {-prepend} {
{-a} - {--append} - {-append} - {-p} - {--prepend} - {-prepend} \
- {--delim} - {-delim} - {--delim=*} - {-delim=*} {
# command-specific switches interpreted later on
lappend otherargv $arg
}
{append-path} - {prepend-path} - {remove-path} {
# detect *-path commands to say -d means --delim, not --default
set ddelimarg 1
lappend otherargv $arg
}
{-f} - {--force} - {--human} - {-v} - {--verbose} - {-s} -\
{--silent} - {-c} - {--create} - {-i} - {--icase} -\
{--userlvl=*} {
@@ -5732,16 +5760,8 @@ if {[catch {
# defined in env(MODULESPATH)
runModulerc
switch -regexp -- $command {
{^(prepend|append|remove)-path$} {
eval cmdModuleImm $command $otherargv
renderSettings
}
. {
# eval needed to pass otherargv as list to module proc
eval module $command $otherargv
}
}
# eval needed to pass otherargv as list to module proc
eval module $command $otherargv
} errMsg ]} {
# no use of reportError here to get independent from any
# previous error report inhibition