@@ -247,7 +247,9 @@ external command.
247
247
Here is an example custom command that has a rest parameter:
248
248
249
249
``` nu
250
- def foo [ --flag req opt? ...args ] { [$flag, $req, $opt, $args] | to nuon }
250
+ def foo [ --flag req opt? ...args ] {
251
+ { flag: $flag, req: $req, opt: $opt, args: $args } | to nuon
252
+ }
251
253
```
252
254
253
255
It has one flag (` --flag ` ), one required positional parameter (` req ` ), one optional positional parameter
@@ -259,9 +261,9 @@ recognized before variables, subexpressions, and list literals, and no whitespac
259
261
260
262
``` nu
261
263
foo "bar" "baz" ...[1 2 3] # With ..., the numbers are treated as separate arguments
262
- # => [ false, bar, baz, [1, 2, 3]]
264
+ # => { flag: false, req: bar, opt: baz, args: [1, 2, 3] }
263
265
foo "bar" "baz" [1 2 3] # Without ..., [1 2 3] is treated as a single argument
264
- # => [ false, bar, baz, [[1, 2, 3]]]
266
+ # => { flag: false, req: bar, opt: baz, args: [[1, 2, 3]] }
265
267
```
266
268
267
269
A more useful way to use the spread operator is if you have another command with a rest parameter
@@ -270,27 +272,27 @@ and you want it to forward its arguments to `foo`:
270
272
``` nu
271
273
def bar [ ...args ] { foo --flag "bar" "baz" ...$args }
272
274
bar 1 2 3
273
- # => [ true, bar, baz, [1, 2, 3]]
275
+ # => { flag: true, req: bar, opt: baz, args: [1, 2, 3] }
274
276
```
275
277
276
278
You can spread multiple lists in a single call, and also intersperse individual arguments:
277
279
278
280
``` nu
279
281
foo "bar" "baz" 1 ...[2 3] 4 5 ...(6..9 | take 2) last
280
- # => [ false, bar, baz, [1, 2, 3, 4, 5, 6, 7, last]]
282
+ # => { flag: false, req: bar, opt: baz, args: [1, 2, 3, 4, 5, 6, 7, last] }
281
283
```
282
284
283
285
Flags/named arguments can go after a spread argument, just like they can go after regular rest arguments:
284
286
285
287
``` nu
286
288
foo "bar" "baz" 1 ...[2 3] --flag 4
287
- # => [ true, bar, baz, [1, 2, 3, 4]]
289
+ # => { flag: true, req: bar, opt: baz, args: [1, 2, 3, 4] }
288
290
```
289
291
290
292
If a spread argument comes before an optional positional parameter, that optional parameter is treated
291
293
as being omitted:
292
294
293
295
``` nu
294
296
foo "bar" ...[1 2] "not opt" # The null means no argument was given for opt
295
- # => [ false, bar, null, [1, 2, "not opt"]]
297
+ # => { flag: false, req: bar, opt: null, args: [1, 2, "not opt"] }
296
298
```
0 commit comments