Skip to content

fix arg parse#5754

Merged
sophiajt merged 4 commits intonushell:mainfrom
WindSoilder:fix_script_args
Jun 11, 2022
Merged

fix arg parse#5754
sophiajt merged 4 commits intonushell:mainfrom
WindSoilder:fix_script_args

Conversation

@WindSoilder
Copy link
Copy Markdown
Contributor

@WindSoilder WindSoilder commented Jun 11, 2022

Description

Fixes: #5472
Fixes: #5753

General idea

By default, we don't want to escape input arguments, except the following case:

  1. input argument contains ' '(like abc def), we will convert it to `abc def` (It's the original behavior)
  2. input argument contains --version='xx yy', we will convert it to --version=`xx yy`
  3. input argument contains " or \, we will try to escape input.

In other cases, keep input arguments to the same.

About change

I rewrite original escape_quote_string_with_file to escape_for_script_arg.

It seems that we don't really need to read and parse script file to make escape working.

About testing

I have manually tests for the following scenario which shows in some issues (#5371 , #5532):

# a.nu
# with test code:
# nu a.nu 3
def main [x: int] {
  $x + 10
}


# b.nu
# with test code:
# nu b.nu linux --version v5.2
# nu b.nu linux --version=v5.2
def main [kernel: string, --version: string] {
    echo $kernel;
    echo $version
}

# c.nu
# nu c.nu test_ver --version='xx yy' --arch=ghi
# nu c.nu 'test_ver' --version=version --arch="ghi"
def main [kernel: string, --version: string, --arch: string] {
    echo $kernel
    echo $version
    echo $arch
}


# d.nu
# nu d.nu asdf --arch asfd
def main [kernel: string, --version: string, --arch: string] {
    echo $kernel
    echo $version
    echo $arch
}


# e.nu
# nu script.nu '"'
def main [x: any] {
  echo $x
}

For testing code, it seems that our nu! macro doesn't works well with something like:

nu!(cwd: ".", "nu script.nu");

So I just make unit tests for relative function to test escaping result.

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 --workspace --features=extra -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect to check that you're using the standard code style
  • cargo test --workspace --features=extra to check that all the tests pass

@WindSoilder WindSoilder marked this pull request as ready for review June 11, 2022 04:22
@WindSoilder WindSoilder marked this pull request as draft June 11, 2022 05:21
@WindSoilder WindSoilder marked this pull request as ready for review June 11, 2022 06:50
@sophiajt
Copy link
Copy Markdown
Contributor

Thanks!

@sophiajt sophiajt merged commit 2dea9e6 into nushell:main Jun 11, 2022
@WindSoilder WindSoilder deleted the fix_script_args branch June 11, 2022 09:49
@rgwood
Copy link
Copy Markdown
Contributor

rgwood commented Jun 11, 2022

Thanks @WindSoilder!

fennewald pushed a commit to fennewald/nushell that referenced this pull request Jun 27, 2022
* fix arg parse

* add ut, fix clippy

* simplify code

* fmt code
@fdncred fdncred mentioned this pull request Jul 28, 2022
6 tasks
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.

Broken commandline arg types Nu adds uneceseray quotes to ints, giving type errors

3 participants