Skip to content

scripts do not take arguments as in the REPL #9766

Closed as not planned
Closed as not planned

Description

@amtoine

Describe the bug

when running a script, it appears quotes, double quotes and backticks are removed from arguments before being passed down to main.

How to reproduce

  1. put the following in your $env.PATH as an executable file called foo
#!/usr/bin/env nu

export def main [...args: string] {
    print $args
}
  1. run the following command
foo "-a" 123 '-b' 456 `-c` 789
  1. see the following error
Error: nu::parser::unknown_flag

  × The `main` command doesn't have flag `-a`.
   ╭─[<commandline>:1:1]
 1 │ main -a 123 -b 456 -c 789
   ·       ┬
   ·       ╰── unknown flag
   ╰────
  help: Available flags: --help(-h). Use `--help` for more information.

Expected behavior

i expected it to work as in the REPL, i.e.

def foo [...args: string] {
    print $args
}

and

> foo "-a" 123 '-b' 456 `-c` 789
╭───┬─────╮
│ 0 │ -a  │
│ 1 │ 123 │
│ 2 │ -b  │
│ 3 │ 456 │
│ 4 │ -c  │
│ 5 │ 789 │
╰───┴─────╯

Screenshots

No response

Configuration

key value
version 0.82.1
branch main
commit_hash 656f707
build_os linux-x86_64
build_target x86_64-unknown-linux-gnu
rust_version rustc 1.69.0 (84c898d65 2023-04-16)
rust_channel 1.69.0-x86_64-unknown-linux-gnu
cargo_version cargo 1.69.0 (6e9a83356 2023-04-12)
build_time 2023-07-17 19:32:26 +02:00
build_rust_channel release
allocator standard
features default, sqlite, trash, which, zip
installed_plugins hist, plot

Additional context

i'm trying to run an external command with arguments from a script.

hope i'm not completely forgetting about how to pass arguments 👀

Activity

  1. added
    status:needs-triage
    usage:script-file
    on Jul 21, 2023
  2. amtoine commented on Jul 28, 2023

    @amtoine
    MemberAuthor

    it appears writting

    #!/usr/bin/env nu
    
    extern-wrapped main [...args: string] {
        print $args
    }

    solves the issue 😮

  3. mengsuenyan commented on Jul 29, 2023

    @mengsuenyan
    Contributor

    the main function just like the normal custom command,

    file: foo.nu

    export def main [
        --a (-a): number,
        --b (-b): number,
        --c (-c): number,
    ] {
        $a + $b + $c
    }
    > use foo.nu
    > foo -a 1 -b 2 -c 3
  4. amtoine commented on Jul 29, 2023

    @amtoine
    MemberAuthor

    @mengsuenyan
    yes, your example works fine but i do not think it solves this issue 😕

    we can see that

    > def foo [...args] { print $args }; foo "-a"
    ╭───┬────╮
    │ 0 │ -a │
    ╰───┴────╯

    works fine in the REPL but the exact same command in a script fails.
    it looks like the quotes are removed when passing the arguments to the script, but "-a" is a string, not an invalid option -a.

  5. 132ikl commented on Feb 28, 2025

    @132ikl
    Member

    I think this is working as intended since the quotes aren't part of the literal string value which are passed to the child nu process. There's no way for the child nu (or any external command) process to disambiguate between whether the caller used quotes or not. def --wrapped seems to be a sufficient workaround for this. I'm going to close this, if you disagree we can re-open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status:needs-triageusage:script-file

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions