Skip to content

Commit f427ba4

Browse files
authored
Improve pipelines input output types documentation (#1636)
* Improve pipelines input output types documentation Replace "Notes on Errors when Piping Commands" section content with PowerShell references with neutral and more general and extensive "Command Input and Output Types" content. * Approach the input output types docs from the Basics context rather than PowerShell behavior and situational errors * Introduce in a more natural and introductory order: 1. Discover input output types of commands via `help` 2. Usable case 3. Problematic case with unintended behavior due to command default behavior 4. Unsupported case with error * Add example pipeline expressions for the supported good case * Describe alternatives like before * Keep description of why and in what way commands differ in input output support With the new approach, mentioning PowerShell felt misplaced. Nushell stands well on its own. The relevant error is still there (remains searchable and discoverable). PowerShell specifics would feel more appropriate in a "Coming from PowerShell" documentation. * Link command docs Link first occurrences of commands in section to command docs.
1 parent cebeb5c commit f427ba4

File tree

1 file changed

+60
-32
lines changed

1 file changed

+60
-32
lines changed

book/pipelines.md

Lines changed: 60 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -295,51 +295,65 @@ Data coming from an external command into Nu will come in as bytes that Nushell
295295

296296
Nu works with data piped between two external commands in the same way as other shells, like Bash would. The `stdout` of external_command_1 is connected to the `stdin` of external_command_2. This lets data flow naturally between the two commands.
297297

298-
### Notes on Errors when Piping Commands
298+
### Command Input and Output Types
299299

300-
Sometimes, it might be unclear as to why you cannot pipe to a command.
300+
The Basics section above describes how commands can be combined in pipelines as input, filters, or output.
301+
How you can use commands depends on what they offer in terms of input/output handling.
301302

302-
For example, PowerShell users may be used to piping the output of any internal PowerShell command directly to another, e.g.:
303+
You can check what a command supports with [`help <command name>`](/commands/docs/help.md), which shows the relevant *Input/output types*.
303304

304-
`echo 1 | sleep`
305+
For example, through `help first` we can see that the [`first` command](/commands/docs/first.md) supports multiple input and output types:
305306

306-
(Where for PowerShell, `echo` is an alias to `Write-Output` and `sleep` is to `Start-Sleep`.)
307+
```nu
308+
> help first
309+
[…]
310+
Input/output types:
311+
╭───┬───────────┬────────╮
312+
│ # │ input │ output │
313+
├───┼───────────┼────────┤
314+
│ 0 │ list<any> │ any │
315+
│ 1 │ binary │ binary │
316+
│ 2 │ range │ any │
317+
╰───┴───────────┴────────╯
318+
319+
> [a b c] | first took 1ms
320+
a
321+
322+
> 1..4 | first took 21ms
323+
1
324+
```
307325

308-
However, it might be surprising that for some commands, the same in Nushell errors:
326+
As another example, the [`ls` command](/commands/docs/ls.md) supports output but not input:
309327

310328
```nu
311-
> echo 1sec | sleep
312-
Error: nu::parser::missing_positional
313-
314-
× Missing required positional argument.
315-
╭─[entry #53:1:1]
316-
1 │ echo 1sec | sleep
317-
╰────
318-
help: Usage: sleep <duration> ...(rest) . Use `--help` for more information.
329+
> help ls
330+
[…]
331+
Input/output types:
332+
╭───┬─────────┬────────╮
333+
│ # │ input │ output │
334+
├───┼─────────┼────────┤
335+
│ 0 │ nothing │ table │
336+
╰───┴─────────┴────────╯
319337
```
320338

321-
While there is no steadfast rule, Nu generally tries to copy established conventions,
322-
or do what 'feels right'. And with `sleep`, this is actually the same behaviour as Bash.
339+
This means, for example, that attempting to pipe into `ls` (`echo .. | ls`) leads to unintended results.
340+
The input stream is ignored, and `ls` defaults to listing the current directory.
323341

324-
Many commands do have piped input/output however, and if it's ever unclear,
325-
you can see what you can give to a command by invoking `help <command name>`:
342+
To integrate a command like `ls` into a pipeline, you have to explicitly reference the input and pass it as a parameter:
326343

327344
```nu
328-
> help sleep
329-
Delay for a specified amount of time.
330-
331-
Search terms: delay, wait, timer
345+
> echo .. | ls $in
346+
```
332347

333-
Usage:
334-
> sleep <duration> ...(rest)
348+
Note that this only works if `$in` matches the argument type. For example, `[dir1 dir2] | ls $in` will fail with the error `can't convert list<string> to string`.
335349

336-
Flags:
337-
-h, --help - Display the help message for this command
350+
Other commands without default behavior may fail in different ways, and with explicit errors.
338351

339-
Parameters:
340-
duration <duration>: Time to sleep.
341-
...rest <duration>: Additional time.
352+
For example, `help sleep` tells us that [`sleep`](/commands/docs/sleep.md) supports no input and no output types:
342353

354+
```nu
355+
> help sleep
356+
[…]
343357
Input/output types:
344358
╭───┬─────────┬─────────╮
345359
│ # │ input │ output │
@@ -348,10 +362,24 @@ Input/output types:
348362
╰───┴─────────┴─────────╯
349363
```
350364

351-
In this case, sleep takes `nothing` and instead expects an argument.
365+
When we erroneously pipe into it, instead of unintended behavior like in the `ls` example above, we receive an error:
366+
367+
```nu
368+
> echo 1sec | sleep
369+
Error: nu::parser::missing_positional
370+
371+
× Missing required positional argument.
372+
╭─[entry #53:1:18]
373+
1 │ echo 1sec | sleep
374+
╰────
375+
help: Usage: sleep <duration> ...(rest) . Use `--help` for more information.
376+
```
377+
378+
While there is no steadfast rule, Nu generally tries to copy established conventions in command behavior,
379+
or do what 'feels right'.
380+
The `sleep` behavior of not supporting an input stream matches Bash `sleep` behavior for example.
352381

353-
So, we can supply the output of the `echo` command as an argument to it:
354-
`echo 1sec | sleep $in` or `sleep (echo 1sec)`
382+
Many commands do have piped input/output however, and if it's ever unclear, check their `help` documentation as described above.
355383

356384
## Behind the Scenes
357385

0 commit comments

Comments
 (0)