elves / elvish

Powerful scripting language & versatile interactive shell
https://elv.sh/
BSD 2-Clause "Simplified" License
5.53k stars 297 forks source link

flag::call does not recognize options with default $nil #1755

Closed stephenmw closed 5 months ago

stephenmw commented 5 months ago

flag::call fails to find options with a default of $nil. Any other default works. A minimal example:

~> flag:call {|&port=$nil| echo $port } [-port 1]
Exception: flag provided but not defined: -port
[tty 23]:1:1: flag:call {|&port=$nil| echo $port } [-port 1]
~> flag:call {|&port=0| echo $port } [-port 1]
1
krader1961 commented 5 months ago

The documentation says it should behave the same as flag:parse which does not allow $nil as a default value:

elvish> flag:parse [-port 1] [[port $nil 'port number']]
Exception: bad value: flag default value must be boolean, number, string or list, but is $nil
[tty 12]:1:1: flag:parse [-port 1] [[port $nil 'port number']]
elvish> flag:parse [-port 1] [[port 0 'port number']]
▶ [&port=1]
▶ []
krader1961 commented 5 months ago

Fixing this is trivial. It simply requires replacing

addFlag(fs, name, value, "")

with

if err := addFlag(fs, name, value, ""); err != nil {
    return err
}

to match the same behavior as flag:parse.