|
| 1 | +--- |
| 2 | +outline: deep |
| 3 | +--- |
| 4 | + |
| 5 | +# Stdin Specification |
| 6 | + |
| 7 | +Formatters **MAY** also implement the Stdin Specification, which allows |
| 8 | +formatting "virtual files" passed via stdin. |
| 9 | + |
| 10 | +A formatter **MUST** implement the Stdin Specification if its formatting behavior |
| 11 | +can depend on the name of the file being formatted. |
| 12 | + |
| 13 | +## Rules |
| 14 | + |
| 15 | +In order for the formatter to comply with this spec, it **MUST** satisfy the following: |
| 16 | + |
| 17 | +### 1. `--stdin` flag |
| 18 | + |
| 19 | +The formatter's CLI must be of the form: |
| 20 | + |
| 21 | +``` |
| 22 | +<command> [options] [--stdin] [...<files>] |
| 23 | +``` |
| 24 | + |
| 25 | +Where: |
| 26 | + |
| 27 | +- `<command>` is the name of the formatting tool. |
| 28 | +- `[options]` are any number of flags and options that the formatter accepts. |
| 29 | +- `--stdin` is an optional flag that puts the formatter in "stdin mode". In |
| 30 | + stdin mode, the formatter reads file contents from stdin rather than the |
| 31 | + filesystem. |
| 32 | +- `[...<files>]` are one or more files given to the formatter for processing. If |
| 33 | + `--stdin` is specified, then exactly 1 must be present, and is treated as a |
| 34 | + filename. |
| 35 | + |
| 36 | +Example: |
| 37 | + |
| 38 | +``` |
| 39 | +$ echo "{}" | nixfmt --stdin path/to/file.nix |
| 40 | +``` |
| 41 | + |
| 42 | +!!! note |
| 43 | + |
| 44 | + This CLI **MUST** be implemented in a way that is compatibile with the |
| 45 | + vanilla [Formatter Specification](#1-files-passed-as-arguments). |
| 46 | + |
| 47 | +### 2. Print to stdout, do not assume file is present on filesystem |
| 48 | + |
| 49 | +When in stdin mode, the formatter: |
| 50 | + |
| 51 | +1. **MUST** print the formatted file to stdout. |
| 52 | +2. **MUST NOT** attempt to read the file on the filesystem. Instead, it |
| 53 | + **MUST** read from stdin. |
| 54 | +3. **MUST NOT** write to the given path on the filesytem. It _MAY_ write to |
| 55 | + temporary files elsewhere on disk, but _SHOULD_ clean them up when done. |
| 56 | + |
| 57 | +### 3. Exit nonzero on error |
| 58 | + |
| 59 | +When something goes wrong when formatting (for example, the source code is |
| 60 | +syntactically invalid), the formatter **MUST** exit nonzero. |
| 61 | + |
| 62 | +The formatter _SHOULD_ print useful information about what went wrong to |
| 63 | +stderr. |
0 commit comments