Skip to content

Fixing the flag issue - #5447

Merged
fdncred merged 12 commits into
nushell:mainfrom
merelymyself:patch-1
May 9, 2022
Merged

fdncred merged 12 commits into
nushell:mainfrom
merelymyself:patch-1

Conversation

@merelymyself

@merelymyself merelymyself commented May 5, 2022 •

Copy link
Copy Markdown
Contributor

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?

def main [kernel: string, --version: string, --arch: string] {

} 

is working on my machine.

Tests

Make sure you've run and fixed any issues with these commands:

  • cargo fmt --all -- --check to check standard code formatting (cargo fmt --all applies these changes)
  • cargo clippy --all --all-features -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect to check that you're using the standard code style
  • cargo build; cargo test --all --all-features to check that all the tests pass

Comment thread crates/nu-parser/src/deparse.rs Resolved
Comment thread crates/nu-parser/src/deparse.rs Outdated

pub fn escape_quote_string(input: &str) -> String {
if input.starts_with('-') {
parse_input(input)

@uasi uasi May 5, 2022 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see. I'll keep working at it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

image

@uasi uasi May 6, 2022 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

@merelymyself
merelymyself marked this pull request as draft May 5, 2022 11:01
@merelymyself
merelymyself marked this pull request as ready for review May 6, 2022 02:01
@fdncred
fdncred marked this pull request as draft May 6, 2022 15:41
@fdncred

fdncred commented May 6, 2022

Copy link
Copy Markdown
Contributor

moving this to draft until the issues are resolved. please ping me here or in discord when you're ready.

@merelymyself

merelymyself commented May 7, 2022 •

Copy link
Copy Markdown
Contributor Author

@fdncred - I think I've got it working.

/home/gabriel/CodingProjects/nushell〉cat script.nu                           05/07/2022 04:55:16 PM
def main [--foo: string, arg: string] {
  echo $foo
  echo $arg
}
/home/gabriel/CodingProjects/nushell〉target/debug/nu script.nu "--this-is-arg--"
--this-is-arg--

@merelymyself
merelymyself marked this pull request as ready for review May 7, 2022 08:56
Comment thread crates/nu-parser/src/deparse.rs Resolved
@uasi

uasi commented May 7, 2022

Copy link
Copy Markdown
Contributor

Can you keep the original escape_quote_string() and add a new function with a different name? Because it is also used to escape strings unconditionally elsewhere.

@fdncred

fdncred commented May 8, 2022

Copy link
Copy Markdown
Contributor

@merelymyself, @uasi, is this ready to be merged or are we still working on it?

@merelymyself

Copy link
Copy Markdown
Contributor Author

@fdncred , I think it’s ready.

@fdncred

fdncred commented May 9, 2022

Copy link
Copy Markdown
Contributor

I'm kind of concerned about having escape_quote_string, escape_quote_string_new, and escape_quote_string_advanced. Maybe there could be some comments on the functions about when to use each of them and what they're specifically for? or maybe combine the new ones somehow?

@merelymyself

Copy link
Copy Markdown
Contributor Author

@fdncred sure thing!

@fdncred

fdncred commented May 9, 2022

Copy link
Copy Markdown
Contributor

thanks for making things more verbose. let's land now.

@fdncred
fdncred merged commit 8d8f25b into nushell:main May 9, 2022
@merelymyself
merelymyself deleted the patch-1 branch May 9, 2022 12:36
@gipsyh

gipsyh commented May 10, 2022 •

Copy link
Copy Markdown
Contributor

It still not work for #5445.

截屏2022-05-10 09 40 07

key value
version 0.62.1
branch
tag
short_commit 14d80d5
commit_hash 14d80d5
commit_date 2022-05-09 18:57:28 +00:00
build_os linux-aarch64
rust_version rustc 1.60.0 (7737e0b5c 2022-04-04)
rust_channel stable-aarch64-unknown-linux-gnu
cargo_version cargo 1.60.0 (d1fd9fe 2022-03-01)
pkg_version 0.62.1
build_time 2022-05-10 01:25:36 +00:00
build_rust_channel release
features default, trash, which, zip
installed_plugins

fennewald pushed a commit to fennewald/nushell that referenced this pull request Jun 27, 2022
* 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants