You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: book/custom_commands.md
+43Lines changed: 43 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -550,6 +550,49 @@ vip-greet $vip ...$guests
550
550
# => And a special welcome to our VIP today, Tanisha!
551
551
```
552
552
553
+
## Input-Output Signature
554
+
555
+
Custom commands can be given explicit signatures.
556
+
557
+
For example, the signature for [`str stats`](/commands/docs/str_stats.md) looks like this:
558
+
559
+
```nu
560
+
def "str stats" []: string -> record { }
561
+
```
562
+
563
+
Here, `string -> record` defines the allowed types of the _pipeline input and output_ of the command:
564
+
565
+
- It accepts a `string` as pipeline input
566
+
- It outputs a `record`
567
+
568
+
If there are multiple input/output types, they can be placed within brackets and separated with commas or newlines, as in [`str join`](/commands/docs/str_join.md):
569
+
570
+
```nu
571
+
def "str join" [separator?: string]: [
572
+
list -> string
573
+
string -> string
574
+
] { }
575
+
```
576
+
577
+
This indicates that `str join` can accept either a `list<any>` or a `string` as pipeline input. In either case, it will output a `string`.
578
+
579
+
Some commands don't accept or require data as pipeline input. In this case, the input type will be `<nothing>`. The same is true for the output type if the command returns `null` (e.g., [`rm`](/commands/docs/rm.md) or [`hide`](/commands/docs/hide.md)):
The example above is renamed `xhide` so that copying it to the REPL will not shadow the built-in `hide` command.
587
+
:::
588
+
589
+
Input-output signatures are shown in the `help` for a command (both built-in and custom) and can also be introspected through:
590
+
591
+
```nu
592
+
help commands | where name == <command_name>
593
+
scope commands | where name == <command_name>
594
+
```
595
+
553
596
## Documenting Your Command
554
597
555
598
In order to best help users understand how to use your custom commands, you can also document them with additional descriptions for the commands and parameters.
0 commit comments