Skip to content

Coerce rest into globs #18399

Closed
Closed

Description

@Skyrocket1653

Basics

  • I have done a basic search through the issue tracker to find similar or related issues.
  • I have made myself familiar with the available features of Nushell for the particular area this enhancement request touches.

Related problem

When doing something like script.nu true false 011 nushell will make 011 into 11 and if we change the rest annotation from non annotated or annotated with any to string or glob it will error out because true and false are not globs.

The problem is that for example often programs that allows doing something like script.nu %command% and values in %command% don't usually quote true, false... or even pass things like 0011.

Recently #18372 changed rest when non annotated no longer make the values as globs which breaks my scripts in said scenarios.

Describe the solution you'd like

Add a --coerce-rest-into-globs and when passed to def and the annotation of rest is glob it will make values like true, false, 011 as globs instead of converting it into boolean or integer.

Describe alternatives you've considered

No response

Additional context and details

No response

Activity

  1. added
    category:enhancement
    status:needs-triage
    on Jun 12, 2026
  2. fdncred commented on Jun 12, 2026

    @fdncred
    Contributor

    You can quote your parameters to coerce them into globs.

    def cmd [...rest: glob] { $rest | each {describe}}
    ❯ cmd "true" "false" "011"
    ╭───┬──────╮
    │ 0 │ glob │
    │ 1 │ glob │
    │ 2 │ glob │
    ╰───┴──────╯
  3. Skyrocket1653 commented on Jun 12, 2026

    @Skyrocket1653
    ContributorAuthor

    that wont work if used as a script

    ./script.nu "true" "false" "011"
    Error: nu::parser::parse_mismatch_with_full_string_msg
    
      × Parse mismatch during operation.
       ╭─[<commandline>:1:6]
     1 │ main true false 011
       ·      ──┬─
       ·        ╰── expected glob
       ╰────
    
    #!/usr/bin/env nu
    
    export def main [...rest: glob] { $rest | each { {type: ($in | describe) value: $in} } }
    
  4. Skyrocket1653 commented on Jun 12, 2026

    @Skyrocket1653
    ContributorAuthor

    And game launchers that passes values for rest won't have extra quotations for "true", "false", "011" so when I saw before that rest without annotation left the values as glob and didn't break or error out when "true", "false", "011" i was very happy as it fixes said scenario.

    As it no longer covers my scenario can i get a fix for this pretty please?

  5. fdncred commented on Jun 12, 2026

    @fdncred
    Contributor

    You could also use def --wrapped

    ❯ def --wrapped cmd [...rest] { $rest | each {|p| {param: $p, type: ($p | describe)}}}
    ❯ cmd true false 011
    ╭─#─┬─param─┬──type──╮
    │ 0 │ true  │ string │
    │ 1 │ false │ string │
    │ 2 │ 011   │ string │
    ╰─#─┴─param─┴──type──╯
    ❯ nu test.nu true false 001
    ╭───┬───────┬────────╮
    │ # │ param │  type  │
    ├───┼───────┼────────┤
    │ 0 │ true  │ string │
    │ 1 │ false │ string │
    │ 2 │ 001   │ string │
    ╰───┴───────┴────────╯

    I'm not sure what the point of having globs because external programs do not understand nushell globs necessarily.

  6. Skyrocket1653 commented on Jun 12, 2026

    @Skyrocket1653
    ContributorAuthor

    When using --wrapped i can no longer do --:

    ./script.nu -- some-command --my-flag=hi true false --help
    ╭───┬────────┬──────────────╮
    │ # │  type  │    value     │
    ├───┼────────┼──────────────┤
    │ 0 │ string │ --           │
    │ 1 │ string │ some-command │
    │ 2 │ string │ true         │
    │ 3 │ string │ false        │
    │ 4 │ string │ --help       │
    ╰───┴────────┴──────────────╯
    my flag: hi
    
    #!/usr/bin/env nu
    
    export def main --wrapped [--my-flag: string ...rest] {
       $rest | each { {type: ($in | describe) value: $in} } | print
       print $"my flag: ($my_flag)"
    }
    

    Now if both script or command have the same flag the first one will consume it instead. For that reason I can't rely on --wrapped. As chaining two nushell script with the same flag wont behave correctly.

  7. fdncred commented on Jun 12, 2026

    @fdncred
    Contributor

    We could revert #18372 but I'm not clear why you want globs since strings are what is used to pass to externals.

  8. Skyrocket1653 commented on Jun 12, 2026

    @Skyrocket1653
    ContributorAuthor

    Oh i guess it can either be glob or string i was mentioning glob because it was the previous behaviour. And since glob didn't modify the values like '011' i was okay with globs. I understand that the pr intention was to make so that not annotated rest would convert it into nushell data types which understandable which is why I wanted something like a flag to bring back the other behaviour.

  9. Skyrocket1653 commented on Jun 12, 2026

    @Skyrocket1653
    ContributorAuthor

    Futhermore, I believe --wrapped wont be needed anymore in the future as there is --. As we can see the $my_flag has the correct value of hello and the other --my-flag is correctly not consumed.

    For example:

    ./script3.nu --my-flag=hello -- some-other-command -- more-commands --my-flag=goodbye
    ╭───┬────────┬────────────────────╮
    │ # │  type  │       value        │
    ├───┼────────┼────────────────────┤
    │ 0 │ string │ some-other-command │
    │ 1 │ string │ --                 │
    │ 2 │ string │ more-commands      │
    │ 3 │ string │ --my-flag=goodbye  │
    ╰───┴────────┴────────────────────╯
    my flag: hello
    #!/usr/bin/env nu
    
    export def main [--my-flag: string ...rest] {
       $rest | each { {type: ($in | describe) value: $in} } | print
       print $"my flag: ($my_flag)"
    }

    Unless there is a usage of --wrapped where -- does not solve that i missed though i believe --wrapped was needed because -- was not a thing before.

    Or we can reuse and make --wrapped behave like a normal def (where -- works) but when --wrapped is included we make rest values coerced to either string or glob but passing true or "true" won't make an error and force coercion and not have it do any conversions and what i mean by that is to make sure so that 011 or "011" dont get converted into 11.

  10. fdncred commented on Jun 12, 2026

    @fdncred
    Contributor

    Yes, --wrapped was because -- did not exist. I'm not sure it's necessary anymore since we have -- now. What is the problem with just using --? I'd probably fix it if there were a problem with it.

  11. Skyrocket1653 commented on Jun 12, 2026

    @Skyrocket1653
    ContributorAuthor

    There is no problem with -- I was just saying that if we were going to eventually drop --wrapped, instead we could reuse it (if the flag name is appropiate for this feature) so when rest: string is defined when doing script.nu true false 011 the values passed will be as is (string): true ,false, 011 and no error of incorrect data type which is currently doing without --wrapped.

    I do need something like that because game launchers won't quote true, 011 and quoting true -> "true" does not help when is being passed to a nushell script as it will still complain about incorrect data type.

  12. Skyrocket1653 commented on Jun 12, 2026

    @Skyrocket1653
    ContributorAuthor

    I do need to hear your thoughts when you are available whether this feature will be worked on as currently I don't know what to do, whether I should wait for the feature or roll back to a previous commit while the feature is worked on.

  13. fdncred commented on Jun 12, 2026

    @fdncred
    Contributor

    You could always wrap them in quotes yourself.

    ❯ def --wrapped cmd [...rest] { $rest | each {|p| {param: $"(char dq)($p)(char dq)", type: ($p | describe)}}}
    ❯ cmd true false 011
    ╭─#─┬──param──┬──type──╮
    │ 0 │ "true"  │ string │
    │ 1 │ "false" │ string │
    │ 2 │ "011"   │ string │
    ╰─#─┴──param──┴──type──╯
  14. Skyrocket1653 commented on Jun 12, 2026

    @Skyrocket1653
    ContributorAuthor

    I guess I can use that technique to forward them as "true", "false" and "011" so like cmd.nu true false 011 will pass to exec script.nu \"true\", \"false\" and \"011". I guess I can make use of this but some stuff will be broken. For example i can think of if exec receives script.nu where script.nu has some user defined flags script.nu \"--my-flag=hi\" this wont be recognised as a proper flag but I guess i could parse it myself and check whether it is a flag and remove the literal quotation but this is still a unconvinient workaround compared to having nushell just have rest: string and have something like def --coerce so true, false and 011 does not cause any issues.

    For a proper solution in the future can we have something that I don't have to use a workaround like def --coerce or something that I was trying to convey?

  15. Skyrocket1653 commented on Jun 12, 2026

    @Skyrocket1653
    ContributorAuthor

    I'm sorry if i was confusing I just realized this myself but essentially I just want -- to work with --wrapped without rest consuming -- as a rest item that it does currently.

  16. Tyarel8 commented on Jun 13, 2026

    @Tyarel8
    Contributor

    The problem is that --wrapped is there to wrap external commands, and some of those use the -- themselves, so if you don't put it in ...$rest, it won't work. For example

    cargo run --target=aarch64-unknown-linux-gnu -- arg1 arg2
    

    Uses the -- to separate compiler flags from command arguments. If I want to use that command inside nushell with --wrapped, I need the -- or it won't work properly.

    def --wrapped "cargo raarch" [...rest] {
    	^cargo run --target=aarch64-unknown-linux-gnu ...$rest
    }

    Also, it would be a unexpected footgun, since the purpose of the flag is to not do any parsing or special handling and treat everything as a string.

  17. Skyrocket1653 commented on Jun 13, 2026

    @Skyrocket1653
    ContributorAuthor

    Okay I've left --wrapped how it was and added another flag to do it --coerce.

  18. added this to the v0.114.0 milestone on Jun 13, 2026
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

    category:enhancementstatus:needs-triage

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions