如何为命名函数参数设置默认值?
How do I set a default value for a named function argument?
我已经找到了这个问题的答案,但 what I've found 似乎没有用。如果我 运行 weather "NYC"
,回显行是 "weather = NYC",但是如果我 运行 weather
,回显行是 "weather = " .如何为 $location
设置默认值?
function weather --argument location --description "Display weather information for a given location"
set -q location; or set location "San Francisco, CA"
echo "weather = $location"
curl -H "Accept-Language: en" "wttr.in/$location?u"
end
更新:
我已经能够通过以下更改获得所需的行为,但我仍然很好奇 set -q location; or set location "San Francisco, CA"
是否应该工作。命令行上好像有,函数里没有。
if not test -n "$location"
set location "Washington, DC"
end
见https://github.com/fish-shell/fish-shell/issues/2645
你的变通办法的可疑解决方案是:
set -q location[1]
or set location "Washington, DC"
在实践中,您的解决方案和上面可疑的解决方案等同于简单地测试是否设置了 var。
我已经找到了这个问题的答案,但 what I've found 似乎没有用。如果我 运行 weather "NYC"
,回显行是 "weather = NYC",但是如果我 运行 weather
,回显行是 "weather = " .如何为 $location
设置默认值?
function weather --argument location --description "Display weather information for a given location"
set -q location; or set location "San Francisco, CA"
echo "weather = $location"
curl -H "Accept-Language: en" "wttr.in/$location?u"
end
更新:
我已经能够通过以下更改获得所需的行为,但我仍然很好奇 set -q location; or set location "San Francisco, CA"
是否应该工作。命令行上好像有,函数里没有。
if not test -n "$location"
set location "Washington, DC"
end
见https://github.com/fish-shell/fish-shell/issues/2645
你的变通办法的可疑解决方案是:
set -q location[1]
or set location "Washington, DC"
在实践中,您的解决方案和上面可疑的解决方案等同于简单地测试是否设置了 var。