extract prefixed function behavior
This commit is contained in:
parent
cbb0dd3fe0
commit
834465a89d
2 changed files with 39 additions and 16 deletions
38
.config/fish/functions/prefixed-function.fish
Normal file
38
.config/fish/functions/prefixed-function.fish
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
function run
|
||||||
|
eval "$argv[1]-$argv[2]"
|
||||||
|
end
|
||||||
|
|
||||||
|
function usage
|
||||||
|
set fn $argv[1]
|
||||||
|
set allowed $argv[2..]
|
||||||
|
echo "Usage: $fn <CMD>"
|
||||||
|
echo " $fn do <CMD>[, <CMD>]"
|
||||||
|
echo " <CMD>:" (string join ", " $allowed)
|
||||||
|
end
|
||||||
|
|
||||||
|
function available-cmds
|
||||||
|
string replace --filter --regex "^$argv[1]-" "" (functions --name)
|
||||||
|
end
|
||||||
|
|
||||||
|
function prefixed-function
|
||||||
|
set fn $argv[1]
|
||||||
|
set allowed (available-cmds $fn)
|
||||||
|
|
||||||
|
if test $argv[2] = "do"
|
||||||
|
set cmds (string replace -a ',' '' $argv[3..])
|
||||||
|
else
|
||||||
|
set cmds $argv[2]
|
||||||
|
end
|
||||||
|
|
||||||
|
for cmd in $cmds
|
||||||
|
if not contains $cmd $allowed
|
||||||
|
echo "Error: Unknown subcommand '$cmd'"
|
||||||
|
usage $fn $allowed
|
||||||
|
return 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
for cmd in $cmds
|
||||||
|
run $fn $cmd
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,18 +1,3 @@
|
||||||
function vitally
|
function vitally
|
||||||
set cmd $argv[1]
|
prefixed-function vitally $argv[1..]
|
||||||
|
|
||||||
if not contains $cmd start stop session-name init do
|
|
||||||
echo "usage: vitally <CMD>; <CMD>: start, stop, session-name, init"
|
|
||||||
return 1
|
|
||||||
end
|
|
||||||
|
|
||||||
if test $cmd = "do"
|
|
||||||
set cmds (string replace -a ',' '' $argv[2..])
|
|
||||||
else
|
|
||||||
set cmds $cmd
|
|
||||||
end
|
|
||||||
|
|
||||||
for c in $cmds
|
|
||||||
eval "vitally-$c"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue