@@ -247,7 +247,9 @@ external command.
247247Here is an example custom command that has a rest parameter:
248248
249249``` 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+ }
251253```
252254
253255It 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
259261
260262``` nu
261263foo "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] }
263265foo "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]] }
265267```
266268
267269A 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`:
270272``` nu
271273def bar [ ...args ] { foo --flag "bar" "baz" ...$args }
272274bar 1 2 3
273- # => [ true, bar, baz, [1, 2, 3]]
275+ # => { flag: true, req: bar, opt: baz, args: [1, 2, 3] }
274276```
275277
276278You can spread multiple lists in a single call, and also intersperse individual arguments:
277279
278280``` nu
279281foo "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] }
281283```
282284
283285Flags/named arguments can go after a spread argument, just like they can go after regular rest arguments:
284286
285287``` nu
286288foo "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] }
288290```
289291
290292If a spread argument comes before an optional positional parameter, that optional parameter is treated
291293as being omitted:
292294
293295``` nu
294296foo "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"] }
296298```
0 commit comments