【发布时间】:2017-06-11 13:44:33
【问题描述】:
给定一个仅打印“foo”、帮助或错误的 fish 脚本 foo.fish
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
return 手册说它会停止当前的内部函数并设置函数的退出状态。
在嵌套函数调用中,我们如何使用适当的退出代码提前退出脚本?
请注意,我们不能使用 exit,因为它会退出 shell 而不仅仅是脚本。
【问题讨论】:
-
“退出 shell 而不仅仅是脚本”是什么意思?除非您使用
source或.获取脚本,否则脚本将在其自己的 shell 中运行。