Skip to content

Commit 98eb77c

Browse files
committed
Move straggler Command-Signature info into main chapter
1 parent a76e102 commit 98eb77c

File tree

3 files changed

+43
-23
lines changed

3 files changed

+43
-23
lines changed

.vuepress/configs/sidebar/en.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ export const sidebarEn: SidebarConfig = {
5353
'/book/scripts.md',
5454
'/book/modules.md',
5555
'/book/overlays.md',
56-
'/book/command_signature.md',
5756
'/book/testing.md',
5857
'/book/style_guide.md',
5958
],

book/command_signature.md

Lines changed: 0 additions & 22 deletions
This file was deleted.

book/custom_commands.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,49 @@ vip-greet $vip ...$guests
550550
# => And a special welcome to our VIP today, Tanisha!
551551
```
552552

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)):
580+
581+
```nu
582+
def xhide [module: string, members?]: nothing -> nothing { }
583+
```
584+
585+
::: tip Note
586+
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+
553596
## Documenting Your Command
554597

555598
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

Comments
 (0)