Search code examples
fish

How do I set a default value for a named function argument?


I have dug for an answer to this and what I've found doesn't seem to work. If I run weather "NYC", the echo'd line is "weather = NYC", however if I run weather, the echo'd line is "weather = ". How do I set a default value for $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

Update:

I have been able to get the desired behavior with the following change but am still curious if the set -q location; or set location "San Francisco, CA" should work. It seems to on the command line, but not in the function.

if not test -n "$location"
  set location "Washington, DC"
end

Solution

  • See https://github.com/fish-shell/fish-shell/issues/2645

    The fishy solution of your workaround is this:

    set -q location[1]
    or set location "Washington, DC"
    

    In practice your solution and the fishy one above are equivalent to simply testing whether the var is set.