Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions book/operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,9 @@ external command.
Here is an example custom command that has a rest parameter:

```nu
def foo [ --flag req opt? ...args ] { [$flag, $req, $opt, $args] | to nuon }
def foo [ --flag req opt? ...args ] {
{ flag: $flag, req: $req, opt: $opt, args: $args } | to nuon
}
```

It has one flag (`--flag`), one required positional parameter (`req`), one optional positional parameter
Expand All @@ -259,9 +261,9 @@ recognized before variables, subexpressions, and list literals, and no whitespac

```nu
foo "bar" "baz" ...[1 2 3] # With ..., the numbers are treated as separate arguments
# => [false, bar, baz, [1, 2, 3]]
# => { flag: false, req: bar, opt: baz, args: [1, 2, 3] }
foo "bar" "baz" [1 2 3] # Without ..., [1 2 3] is treated as a single argument
# => [false, bar, baz, [[1, 2, 3]]]
# => { flag: false, req: bar, opt: baz, args: [[1, 2, 3]] }
```

A more useful way to use the spread operator is if you have another command with a rest parameter
Expand All @@ -270,27 +272,27 @@ and you want it to forward its arguments to `foo`:
```nu
def bar [ ...args ] { foo --flag "bar" "baz" ...$args }
bar 1 2 3
# => [true, bar, baz, [1, 2, 3]]
# => { flag: true, req: bar, opt: baz, args: [1, 2, 3] }
```

You can spread multiple lists in a single call, and also intersperse individual arguments:

```nu
foo "bar" "baz" 1 ...[2 3] 4 5 ...(6..9 | take 2) last
# => [false, bar, baz, [1, 2, 3, 4, 5, 6, 7, last]]
# => { flag: false, req: bar, opt: baz, args: [1, 2, 3, 4, 5, 6, 7, last] }
```

Flags/named arguments can go after a spread argument, just like they can go after regular rest arguments:

```nu
foo "bar" "baz" 1 ...[2 3] --flag 4
# => [true, bar, baz, [1, 2, 3, 4]]
# => { flag: true, req: bar, opt: baz, args: [1, 2, 3, 4] }
```

If a spread argument comes before an optional positional parameter, that optional parameter is treated
as being omitted:

```nu
foo "bar" ...[1 2] "not opt" # The null means no argument was given for opt
# => [false, bar, null, [1, 2, "not opt"]]
# => { flag: false, req: bar, opt: null, args: [1, 2, "not opt"] }
```