Skip to content

Commit 2015c43

Browse files
authored
Use a record instead of a list to clarify arguments in examples. (#2004)
In Programming in Nu#Operators:262 inferring the arguments from the list position could introduce some misunderstanding.
1 parent 22957e0 commit 2015c43

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

book/operators.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,9 @@ external command.
247247
Here 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

253255
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
259261

260262
```nu
261263
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] }
263265
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]] }
265267
```
266268

267269
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`:
270272
```nu
271273
def bar [ ...args ] { foo --flag "bar" "baz" ...$args }
272274
bar 1 2 3
273-
# => [true, bar, baz, [1, 2, 3]]
275+
# => { flag: true, req: bar, opt: baz, args: [1, 2, 3] }
274276
```
275277

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

278280
```nu
279281
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] }
281283
```
282284

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

285287
```nu
286288
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] }
288290
```
289291

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

293295
```nu
294296
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"] }
296298
```

0 commit comments

Comments
 (0)