# Command line

Every dmog command and option, what each reads and writes, and the rules every command shares.

dmog takes a command and its operands. With no command, or a command line it cannot read, it prints its usage as a failure and exits with status 2:

```text
usage: dmog canon|format|check|explain FILE
       dmog run [--grammar FORMAT] [--render csv|json] [--max-output-bytes N] PROGRAM INPUT
       dmog translate --from FORMAT --to FORMAT [--path PATH] [--key KEY] [--with PROGRAM] [--whole] [--max-output-bytes N] INPUT
       dmog formats
       (a FILE may be - for standard input)
```

## Rules every command follows

- `FILE`, `PROGRAM`, and `INPUT` name a file, or `-` for standard input. One operand of a run may be `-`.
- Standard output carries the answer and nothing else.
- A failure is one JSON object on standard error, and the exit status is not 0. The object's fields and the statuses are in [Failures](https://dmog.tabnas.dev/reference/failures.html).
- An option takes its value as the next word: `--from csv`, not `--from=csv`.
- A program is a text in the alchemy language. The language is defined in [the alchemy language reference](https://github.com/tabnas/alchemy/blob/main/docs/language.md), on GitHub.

## `dmog canon FILE`

Prints the program in `FILE` in canonical form: each top-level form on one line, every list in parentheses. Reads the program and does not check it. Exits 2 with `DSL_PARSE_ERROR` when the text does not read.

## `dmog format FILE`

Prints the program in `FILE` in layout form, where a line indented two spaces under another is its child. Reads the program and does not check it. Exits 2 with `DSL_PARSE_ERROR` when the text does not read.

## `dmog check FILE`

Reads the program in `FILE`, desugars it, resolves its names, checks its types, and builds its plan. Prints nothing and exits 0 when all of these succeed. Exits 2 with `DSL_PARSE_ERROR` or `DSL_TYPE_ERROR` when one fails, and 1 when building the plan reaches a `fail` in the program.

## `dmog explain FILE`

Checks the program in `FILE` as `check` does, then prints its plan report: the chain of calls, how many times the input is read, the protocols, what is held in memory and the limit on each, the output order, and the guarantee on memory. The report's fields are defined in [the alchemy language reference](https://github.com/tabnas/alchemy/blob/main/docs/language.md#explain).

## `dmog run PROGRAM INPUT`

Runs the program in `PROGRAM` over the JSON document `INPUT`, and writes what its `export` returns:

- A text the program writes is written as it is.
- A table is written as CSV, or as JSON with `--render json`.
- JSON events are written as compact JSON.

`INPUT` is read a window at a time, and output is written as the program produces it.

### `--grammar FORMAT`

Parses `INPUT` with the grammar of `FORMAT` in place of JSON, and runs the program over the parser's rule passes instead of a document's events. `FORMAT` is one of the formats [`dmog formats`](https://dmog.tabnas.dev/reference/formats.html) lists with a transducer part. This is how a format's transducer part, itself an alchemy program, is run on its own.

### `--render csv|json`

How a result the program leaves to dmog is written. A table is written as CSV by default, and as an array of objects, one for each row, with `json`. JSON events are written as JSON, and `--render csv` refuses them with `DSL_TYPE_ERROR` and the finer code `protocol_mismatch`. A program that writes its own text takes no `--render`, and fails with `DSL_TYPE_ERROR` and the finer code `render_of_text` if given one.

### `--max-output-bytes N`

The most bytes the run may write. A run that would write more fails with `RESOURCE_LIMIT_EXCEEDED` and exits 5. The limit also bounds a text the program builds before writing it.

## `dmog translate --from FORMAT --to FORMAT INPUT`

Reads `INPUT` as the format `--from` names and writes it as the format `--to` names. A document is read through its format's transducer part where the format has one, and written as it is read. A format with no transducer part, and every format under `--whole`, is read whole first.

### `--from FORMAT`

The format `INPUT` is in. Required. One of the identifiers [`dmog formats`](https://dmog.tabnas.dev/reference/formats.html) lists.

### `--to FORMAT`

The format to write. Required. One of the identifiers [`dmog formats`](https://dmog.tabnas.dev/reference/formats.html) lists. A target that cannot hold the document refuses it with `TARGET_VALUE_UNREPRESENTABLE` and status 1, before writing anything where the refusal can be decided at the start.

### `--path PATH`

Writes the value at `PATH` in place of the whole document. `PATH` is a JSON array of object keys, as strings, and array indexes, as numbers, from the root: `["server"]`, `["paths",1]`. The value is written as a plain tree, whatever the source's shapes. The whole document is still read, and held to the same refusals. A path that names nothing fails with `INPUT_INVALID` once the document has been read, with nothing written. Under `--whole`, the path selects in the document as the format's own reading builds it.

### `--key KEY`

The key a document is written under when the target's root must be an object and the document's root is not one. The default is `items`. Has no effect otherwise.

### `--with PROGRAM`

Runs the program in `PROGRAM` over the document read as `--from`, and writes what its `export` returns as `--to`. The export must return JSON events or a table. A program that writes its own text fails with `DSL_TYPE_ERROR` and the finer code `bad_output`.

### `--whole`

Reads the document whole, as the format's own reading builds it, before writing anything. A document that cannot be streamed fails without `--whole` with a message that ends by naming it: an object that repeats a key, a YAML stream of several documents, a YAML mapping with two plain `<<` merge keys or a merge that repeats a key, a TOML table or an INI section entered again after another, and a feed whose own fields come after its first entry. Under `--whole`, a repeated key keeps the value the format's reading keeps, and a YAML stream is an array of its documents.

### `--max-output-bytes N`

The most bytes the translation may write, as for `run`.

## `dmog formats`

Prints the formats `translate` reads and writes, as one JSON array: for each format its `id`, the shapes it `reads` and `writes`, the `root` its render needs, its `schema`, `whole`, the parts it ships (`lift`, `embed`, `render`, and `transduce`), and its `loss` sentences. [Formats](https://dmog.tabnas.dev/reference/formats.html) is this list, written out.
