假设有一个名为 foo.fish
的脚本,该脚本只会输出“foo”、“help”或错误信息
function foo
__parse_args $argv[1]
echo foo
end
function __parse_args --argument option
if test -z $option
return # No option to parse, return early
end
switch $option
case -h --help
echo "Shows this help and exits"
return 0 # How can we exit 0 instead of return?
case -\*
echo "Error: '$option' not a valid option"
return 1 # How can we exit 1 instead of return?
end
end
实际表现:
↪ foo -h
Shows this help and exits
foo
期望的行为:
↪ foo -h
Shows this help and exits
< p > return
命令会停止当前内部函数并设置该函数的退出状态。
当在嵌套函数调用中时,我们如何提前退出脚本,并使用适当的退出代码?
请注意,我们不能使用 exit
命令,因为它会退出 shell 而不仅仅是脚本。
source
或者.
来运行这个脚本,否则它会在自己独立的 shell 中运行。 - Mark Reed