You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Nu adds uneceseray quotes to ints, giving type errors #5472
〉cat foo.nu
def main [a: string] { echo $a }
〉target/debug/nu foo.nu -10
Error: nu::parser::unknown_flag (https://docs.rs/nu-parser/0.62.1/nu_parser/enum.ParseError.html#variant.UnknownFlag)
× The `main` command doesn't have flag `-1`.
╭─[<commandline>:1:1]
1 │ main -10
· ┬
· ╰── unknown flag
╰────
help: use main --help for a list of flags
〉target/debug/nu foo.nu "-10"
Error: nu::parser::unknown_flag (https://docs.rs/nu-parser/0.62.1/nu_parser/enum.ParseError.html#variant.UnknownFlag)
× The `main` command doesn't have flag `-1`.
╭─[<commandline>:1:1]
1 │ main -10
· ┬
· ╰── unknown flag
╰────
help: use main --help for a list of flags
Let's put things in perspective. Command line arguments are passed to nushell as an array of strings. To convert strings to values of the appropriate type, we quote (or not quote) arguments by escape_quote_string_*(), concatenate the results, and evaluate the whole string as a command invocation.
The problem is, it is sometimes impossible to determine what the appropriate type is from a given string alone. Does the string -10 mean a negative int? If the signature of the main function is [x: int], we can probably say yes. So what if the signature is [x: any]? Does the string [a b] mean a string or an array of two bare strings? Does --flag mean a flag? We can't be sure, because if the signature is [--flag: bool, x?: string], it can either be a flag or a string value to x.
Activity
merelymyself commentedon May 9, 2022
This was probably happening because of
nushell/crates/nu-parser/src/deparse.rs
Lines 1 to 14 in ae9c0fc
Not quite sure how to deal with this, while still resolving #5370 and #5371. @uasi , any ideas?
uasi commentedon May 10, 2022
@merelymyself We have to quote values or not depending on the parameter type. For example,
-10must not be quoted if and only if the parameter isint:merelymyself commentedon May 10, 2022
@uasi I tried using
.parse()to check for this. Should work because unquoted ints translate nicely to strings but not the other way round.uasi commentedon May 10, 2022
@merelymyself unquoted negative ints don't, though.
merelymyself commentedon May 10, 2022
Good catch, @uasi . Will have to look more into this.
uasi commentedon May 10, 2022
Let's put things in perspective. Command line arguments are passed to nushell as an array of strings. To convert strings to values of the appropriate type, we quote (or not quote) arguments by
escape_quote_string_*(), concatenate the results, and evaluate the whole string as a command invocation.The problem is, it is sometimes impossible to determine what the appropriate type is from a given string alone. Does the string
-10mean a negative int? If the signature of the main function is[x: int], we can probably say yes. So what if the signature is[x: any]? Does the string[a b]mean a string or an array of two bare strings? Does--flagmean a flag? We can't be sure, because if the signature is[--flag: bool, x?: string], it can either be a flag or a string value tox.