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

标签 fish

我已经找到了答案和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/

相关文章:

fish - 如何仅在fishshell中列出文件/目录?

shell - 如何在 fish shell 脚本中获取程序名称?

fish - 如何使用oh-my-fish更改目录列表的颜色?

vim - neovim使用VIMINIT在ssh上移动配置(几乎可以正常工作)

user-input - 如何在 fish shell 中获得用户确认?

linux - 粘贴到终端/fish shell : [200~ and [201~ 时出现奇怪的字符

shell - 如何使用 fish shell 从我的 PATH 环境变量中删除特定变量

fish - 如何在 fish shell 中使用箭头键选择文本

shell - 在变量扩展后添加文本 - fish

node.js - 运行 npm 脚本时 npm 使用哪个 shell?