gpt4 book ai didi

fish - 如何为命名函数参数设置默认值?

转载 作者:行者123 更新时间:2023-12-03 22:22:31 27 4
gpt4 key购买 nike

我已经找到了答案和what I've found似乎不起作用。如果我运行 weather "NYC" ,回显的行是“天气 = NYC”,但是如果我运行 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。

关于fish - 如何为命名函数参数设置默认值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45726573/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com