Skip to content

Smarter use of the spread operator #15801

Description

@j-xella

Related problem

Not sure if this is a bug of a lack of feature ... Let's consider the which command, for example. Its signature is

which {flags} <application> ...(rest)

Basically, it wants at least one application name to look up, but could have more. So, why I can't do this:

[ notepad cmd powershell ] | which ...$in

Error: nu::parser::missing_positional

  × Missing required positional argument.
   ╭─[entry #10:1:36]
 1 │ [ notepad cmd powershell ] | which ...$in
   ·                                    ▲
   ·                                    ╰── missing application
   ╰────
  help: Usage: which {flags} <application> ...(rest) . Use `--help` for more information.

Describe the solution you'd like

It would be great if the following:

[ notepad cmd powershell ] | which ...$in

would be allowed, with the result as in the alternatives below.

  • The ...$in part would be split into elements
  • The first items would be used for explicit parameters, as per function
  • If the first items don't satisfy the explicit parameters (not enough, wrong type, etc...), runtime error would be thrown
  • Only what is left would be used for the ...(rest) part

Describe alternatives you've considered

There are workarounds, for example:

[ notepad cmd powershell ] | each { which $in } | flatten
# below is what I'd like nushell to do automatically
[ notepad cmd powershell ] | which $in.0 ...($in | skip 1)

But they are quite verbose...

Another approach is to change the signature of which, leaving only the ...(rest) part. But that would be a solution for one command only.

Additional context and details

No response

Activity

  1. added
    category:enhancement
    status:needs-triage
    on May 22, 2025
  2. added
    good first issue
    and removed
    status:needs-triage
    on May 23, 2025
  3. ysthakur commented on May 23, 2025

    @ysthakur
    Member

    I'm not sure if you're asking specifically about which or other commands too. This is something that needs to be done on a per-command basis. We can't allow [foo bar baz] | f ...$in if f has signature def f [ a: int, ...b: string ] or something. That would let you bypass Nushell's static analysis.


    If you're only asking about which, I think all that needs to be done here is changing the signature of which to only take rest arguments, rather than taking a single application followed by rest arguments. That mostly just means removing this bit:

    .required("application", SyntaxShape::String, "Application.")

    At runtime, which already handles receiving an empty list of applications:

    let which_args = WhichArgs {
    applications: call.rest(engine_state, stack, 0)?,
    all: call.has_flag(engine_state, stack, "all")?,
    };
    if which_args.applications.is_empty() {
    return Err(ShellError::MissingParameter {
    param_name: "application".into(),
    span: head,
    });
    }

  4. 132ikl commented on Jun 2, 2025

    @132ikl
    Member

    There are definitely other commands with this issue, I'm not sure if we've tackled them all yet so I think we should leave this open until we've hit them all. I have a list in a drawing board thread called "restification" or something like that

  5. added this to the v0.105.0 milestone on Jun 2, 2025
  6. kumarUjjawal commented on Jun 3, 2025

    @kumarUjjawal
    Contributor

    There are definitely other commands with this issue, I'm not sure if we've tackled them all yet so I think we should leave this open until we've hit them all. I have a list in a drawing board thread called "restification" or something like that

    If you know any more know commands with this issue I can work on that.

  7. 132ikl commented on Jun 3, 2025

    @132ikl
    Member

    this command should list all commands like this:

    help commands 
    | where ((($it.params | length) > 1) and
             ($it.params.1.name starts-with "...") and 
             ($it.params.0.type == $it.params.1.type) and 
             ($it.params.0.required))

    Excluding split column which is a false positive, that leaves us with:

    • drop nth
    • get
    • sleep

    get is somewhat complicated because I believe it has different code paths for single selectors versus multiple selectors, but I think the other ones should be doable. They seem notably less useful than some of the previous ones we've changed like this but it's good to be consistent I suppose

  8. kumarUjjawal commented on Jun 3, 2025

    @kumarUjjawal
    Contributor

    this command should list all commands like this:

    help commands
    | where ((($it.params | length) > 1) and
    ($it.params.1.name starts-with "...") and
    ($it.params.0.type == $it.params.1.type) and
    ($it.params.0.required))
    Excluding split column which is a false positive, that leaves us with:

    • drop nth
    • get
    • sleep

    get is somewhat complicated because I believe it has different code paths for single selectors versus multiple selectors, but I think the other ones should be doable. They seem notably less useful than some of the previous ones we've changed like this but it's good to be consistent I suppose

    I will take a look at this.

  9. sholderbach commented on Jun 3, 2025

    @sholderbach
    Member

    I think for sleep it is not really sensible to spread. The support for multiple durations through the rest parameters there is to fake complex duration literals: sleep 1hr 30min 24sec which is a courtesy to manual entry but not replicated in other places that I am aware of and not really the recommended way to express that programmatically (it would be a single duration value expressing that)

  10. kumarUjjawal commented on Jun 4, 2025

    @kumarUjjawal
    Contributor

    I was thinking going in this direction for drop nth

    1. support multiple arguments, each of which can be:

    an integer (e.g., drop nth 0 2)

    an integer range (e.g., drop nth 3..5)

    1. collect all rows to drop from both integers and ranges into one combined list.

    2. Supports mixed args, like:

      drop nth 0 2 4..5 7.. # drops 0, 2, 4, 5, 7 and onward

    3. Automatically sorts and deduplicates the list of row indices to drop.

    4. reject:

    i) Negative numbers

    ii) Decreasing ranges like 5..2

    iii) Float ranges

  11. 132ikl commented on Jun 4, 2025

    @132ikl
    Member

    sounds good, except for:

    • collect all rows to drop from both integers and ranges into one combined list.

    if you just mean collecting the indices into a list that should be fine (although you would have to do something fancy to express unbounded ranges), but we definitely don't want to collect the actual input since drop nth is a streaming command

  12. kumarUjjawal commented on Jun 5, 2025

    @kumarUjjawal
    Contributor

    Thanks for the clarification! Yes, I meant collecting only the indices from both integers and ranges into a list—not the actual input. The input itself remains streamed. I will try if If I am able to achieve this.

  13. kumarUjjawal commented on Jun 5, 2025

    @kumarUjjawal
    Contributor

    @132ikl Does this look good?

    Image

  14. 132ikl commented on Jun 5, 2025

    @132ikl
    Member

    yes, looks good!

  15. removed this from the v0.105.0 milestone on Jun 7, 2025
  16. kumarUjjawal commented on Jul 3, 2025

    @kumarUjjawal
    Contributor

    @132ikl since drop now supports spread I was thinking to work on the get. Can you guide me as per what we are expecting the get command to look like after supporting the spread?

  17. A-Lamia commented on Jul 13, 2025

    @A-Lamia

    idk if this is the same issue but with pacman, yay, paru this fails

    ["ouch" "nushell" "jq"] | paru -S ...$in

    The command will execute correctly but it will terminate before you can interact with it.

    EDIT: just for some added context i came across this issue by trying to install a list of packages from a file, sorry if this is not the same issue.

  18. mvanhorn commented on May 16, 2026

    @mvanhorn
    Contributor

    Implemented in #18220 - wires the spread case through the parser and get's rest-arg handling with tests for single-spread, mixed, and empty-spread cases.

  19. added a commit that references this issue on Oct 19, 2026
    04ba983
  20. added this to the v0.117.0 milestone on Oct 19, 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:enhancementgood first issue

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions