Repository navigation
Fixing the flag issue - #5447
Conversation
|
|
||
| pub fn escape_quote_string(input: &str) -> String { | ||
| if input.starts_with('-') { | ||
| parse_input(input) |
There was a problem hiding this comment.
If we run nu script.nu --foo='bar baz' qux, input will be "--foo=bar baz" and "qux", and the results will be the same. This is incorrect because the results will then be concatenated into "main --foo=bar baz qux" and evaluated as a nu command. Here, "--foo=bar baz" is split into a flag --foo=bar and a separate argument baz to the main function in script.nu.
The input "--foo=bar baz" should be escaped as "--foo=\"bar baz\"" in this case.
There was a problem hiding this comment.
Ah, I see. I'll keep working at it.
There was a problem hiding this comment.
Found another corner case with the proposed approach. That is, values start with -cannot be passed as positional arguments.
bash-3.2$ cat script.nu
def main [--foo: string, arg: string] {
echo $foo
echo $arg
}
bash-3.2$ target/debug/nu script.nu --foo=bar -this-is-arg-
Error: nu::parser::unknown_flag (https://docs.rs/nu-parser/0.61.1/nu_parser/enum.ParseError.html#variant.UnknownFlag)
× The `main` command doesn't have flag `-t`.
╭─[<commandline>:1:1]
1 │ main --foo=bar -this-is-arg-
· ┬
· ╰── unknown flag
╰────
help: use main --help for a list of flags
bash-3.2$ target/debug/nu script.nu --foo=bar -- -this-is-arg-
Error: nu::parser::unknown_flag (https://docs.rs/nu-parser/0.61.1/nu_parser/enum.ParseError.html#variant.UnknownFlag)
× The `main` command doesn't have flag ``.
╭─[<commandline>:1:1]
1 │ main --foo=bar -- -this-is-arg-
· ─┬
· ╰── unknown flag
╰────
help: use main --help for a list of flags
There was a problem hiding this comment.
So... a flag-looking value must be quoted if and only if the main function does not have a flag of that name. I have no idea how to achieve this.
There was a problem hiding this comment.
bash-3.2$ cat script.nu
def main [--foo: string, arg: string] {
echo $foo
echo $arg
}
bash-3.2$ target/debug/nu script.nu --foo=bar -this-is-arg-
is working for me now. Is it working for you, @uasi?
There was a problem hiding this comment.
@merelymyself It works, but still fails with --this-is-arg--.
bash-3.2$ target/debug/nu script.nu --foo=bar --this-is-arg--
Error: nu::parser::unknown_flag (https://docs.rs/nu-parser/0.62.0/nu_parser/enum.ParseError.html#variant.UnknownFlag)
× The `main` command doesn't have flag `this-is-arg--`.
╭─[<commandline>:1:1]
1 │ main --foo="bar" --this-is-arg--
· ───────┬───────
· ╰── unknown flag
╰────
help: use main --help for a list of flags
I doubt older versions of nu can handle it, so I think we can go ahead with the status quo and fix it later.
There was a problem hiding this comment.
To be precise, nu functions can take values that start with -- as arguments if they are quoted, but nu scripts cannot.
〉def f [arg: string] { echo $arg }
〉f --this-is-arg--
Error: nu::parser::unknown_flag (https://docs.rs/nu-parser/0.62.0/nu_parser/enum.ParseError.html#variant.UnknownFlag)
× The `f` command doesn't have flag `this-is-arg--`.
╭─[entry #2:1:1]
1 │ f --this-is-arg--
· ───────┬───────
· ╰── unknown flag
╰────
help: use f --help for a list of flags
〉f "--this-is-arg--"
--this-is-arg--
〉target/debug/nu script.nu --foo=bar "--this-is-arg--"
× The `main` command doesn't have flag `this-is-arg--`.
╭─[<commandline>:1:1]
1 │ main --foo="bar" --this-is-arg--
· ───────┬───────
· ╰── unknown flag
╰────
help: use main --help for a list of flags
There was a problem hiding this comment.
That is an issue. I agree that that's most likely out of the scope of this PR, because I don't think older versions of nu handled it appropriately either. Still, I'll look more into it!
|
moving this to draft until the issues are resolved. please ping me here or in discord when you're ready. |
|
@fdncred - I think I've got it working. |
|
Can you keep the original |
|
@merelymyself, @uasi, is this ready to be merged or are we still working on it? |
|
@fdncred , I think it’s ready. |
|
I'm kind of concerned about having |
|
@fdncred sure thing! |
|
thanks for making things more verbose. let's land now. |
|
It still not work for #5445.
|
* Fixing the flag issue * whoops, forgot the original point of the function * Update deparse.rs * Update deparse.rs * Update deparse.rs * maybe this might work * fmt * quotation marks works now due to a rigorous check for args. * fmt and clippy * kept the original escape_quote_string(), escaped " and \ * removed script.nu * Added appropriate comments.


Description
As per @uasi 's suggestions on #5445. This shouldn't affect anything other than flags, afaik - they're the only command line args that start with dashes, right?
is working on my machine.
Tests
Make sure you've run and fixed any issues with these commands:
cargo fmt --all -- --checkto check standard code formatting (cargo fmt --allapplies these changes)cargo clippy --all --all-features -- -D warnings -D clippy::unwrap_used -A clippy::needless_collectto check that you're using the standard code stylecargo build; cargo test --all --all-featuresto check that all the tests pass