# The command line

`powercraft-cli` runs Voltecx without a window: in a terminal, a script or a CI job. Every subcommand dispatches
the same engine commands as the menus, so a result from the command line matches the app's.

Every subcommand explains itself: `powercraft-cli SUBCOMMAND --help`. `powercraft-cli --help` lists them all.

## Exit codes

| Code | Meaning |
|---|---|
| 0 | It worked |
| 1 | A command failed: a file could not be read or written, or an engine command refused |
| 2 | Usage error: an unknown subcommand or option, a missing or malformed value |
| 3 | `sim`, `batch`, `sweep`: an `--expect` did not hold |
| 4 | `sim`, `batch`, `sweep`: the simulation or the analysis failed |
| 5 | `sim`, `batch`, `sweep`: there were warnings and `--strict` was given |

`batch` and `sweep` exit with the code of the first file or point that failed.

## Find a command

```sh
powercraft-cli commands --filter export          # every command whose id or label holds "export" (JSON)
powercraft-cli commands describe analysis.loopGain
```

`commands describe ID` prints the command's menu path, its kind, each parameter with its type, default and
whether it is required, and an example `run` line. Add `--json` for the same as JSON.

## Run commands

```sh
powercraft-cli run --cmd file.openExample --param name=buck \
    --cmd simulation.run --param solver=fast \
    --cmd spec.verify --params '{"spec":{"vin":12,"vout":5,"iout":2}}' --format md
```

`--param KEY=VALUE` adds one parameter to the `--cmd` before it; the value is read as JSON when it is JSON
(`5`, `true`, `[1,2]`) and as text otherwise (`2k`, `v(out)`). `--params JSON` gives several at once.
`--format csv` or `md` prints a result that holds a table (the rows of `spec.verify`, a BOM, an AC sweep) as
that table; other results stay one JSON line each. `--root DIR` gives the file commands (open, save and the
exports) access to the files under DIR. A file opened with `--in` that lies under DIR is the circuit's own file,
so `--cmd file.save` writes the changes back to it. When a command stops because the session lacks something
(most often a simulation result: `bom.export`, the scope's measurements and `analysis.stress` read a run), the
error says what to run first.

A step that fails also prints one JSON line on stdout, after the lines of the steps before it, so a program reading
the output can tell which step stopped the chain: `{"command":"bom.export","error":{"message":"...","problem":{...}}}`.
`problem` is the engine's problem behind the error (its `code`, `hint` and fixes) and is left out when there is
none. `powercraft-cli script FILE` prints the same line for the step that stopped the script, with its `"line"`; a
script that does not parse runs nothing and names `script.run`. The exit code is still 1, and the `error:` text on
stderr stays.

Each `--cmd` is one line of the app's console (**View > Console**, Cmd+Shift+Y), so you can try a chain there before
you script it:

![The console running the first two commands of the run above, marked 1 to 3](https://voltecx-web.nguyenvanqui291.workers.dev/docs/manual/img/en/command-line/01-console.png)

1. `file.openExample {"name": "buck"}`: what `--cmd file.openExample --param name=buck` runs.
2. `simulation.run {"solver": "fast"}`: what `--cmd simulation.run --param solver=fast` runs.
3. Its reply: the run's number, the time it simulated, how long it took and the solver, with Show run N in the scope.

Each line of the console starts with its level: INFO for a reply, WARN for a warning a reply carried, ERROR for a
line that failed. The bar above picks a level (**All**, **Errors**, **Warnings**, **Info**, each with its count),
**Command lines** shows or hides the commands that ran, and the filter field keeps the lines that contain its text (any
case). **Copy visible** copies the lines shown and **Clear** empties the console. An error about a part links to the
part on the schematic and to **Problems**, and its copy icon copies the message.

## Simulate and check: `sim`

```sh
powercraft-cli sim buck.voltecx --set Vin.v=24 --tend 5m \
    --measure 'mean(v(out))' --measure 'power(v(out), i(R1), 4m, 5m)' \
    --measure 'efficiency(v(in), i(S1), v(out), i(R1), 4m, 5m)' \
    --expect 'mean(v(out)) in 5.7..6.3' --png buck.png
```

`buck.voltecx` is the example `buck` saved to a file (its load is `R1`). The run prints `v(out) mean = 6.043 V`,
`v(out) power = 14.99 W` and `v(in) efficiency = 99.96 %`, then `PASS`. The input current of `efficiency` is the
current into the circuit, here the switch's `i(S1)`: a source's own current `i(Vin)` is negative while it delivers
power, and `efficiency` refuses a negative input power. Measure over a steady window inside the run (`--tend`), since
the start-up charges the capacitors.

The functions are `mean`, `rms`, `min`, `max`, `pp`, `last`, `thd` (percent), `freq` (Hz), `power(V, I)` (the
mean of v·i, W) and `efficiency(VIN, IIN, VOUT, IOUT)` (percent). Each takes an optional window: `mean(v(out), 4m,
5m)`. `--model averaged` runs a buck, boost, buck-boost, SEPIC or flyback on its averaged model, and `--solver
fast|ref` picks the solver.

`--analysis NAME` runs an analysis and prints its table:

| NAME | Command | Notes |
|---|---|---|
| `ac` | `analysis.acSweep` | needs `--analysis-param output=v(out)` |
| `loopGain` | `analysis.loopGain` | prints the crossover and the margins |
| `steadyState` | `simulation.steadyState` | in place of the transient run; measurements read its result |
| `thermal` | `analysis.thermal` | after the transient run |
| `emi` | `emi.conducted` | the bands of the conducted emission and their margins |

`--analysis-param KEY=VALUE` passes any parameter of that command (`commands describe` lists them).
`--format text|json|csv|md` sets how the table is printed. `ac`, `loopGain` and `emi` give no waveforms, so
`--measure`, `--expect`, `--csv` and `--png` go only with `steadyState`, `thermal` or no analysis.

`--watch` runs again every time the file is saved, until Ctrl-C.

## Many files, many values: `batch` and `sweep`

```sh
powercraft-cli batch designs/ -- --tend 5m --expect 'mean(v(out)) > 4.9' --format md
powercraft-cli sweep buck.voltecx --set L1.l=10u,22u,47u --set R1.r=1,5 --tend 5m --measure 'pp(v(out), 4m, 5m)'
```

`batch` takes folders, files and globs (`'designs/*.voltecx'`) before `--`, and `sim` options after it. It prints
one row per file with its status (`ok`, `fail`, `error`), its exit code and each measurement. `sweep` runs every
combination of the listed values (at most 1000) and prints one row per point. A comma inside parentheses does
not split a value. A window must lie inside the run: the buck example ends at 4 ms, hence `--tend 5m`.

## Compare two designs: `compare`

```sh
powercraft-cli compare buck_v1.voltecx buck_v2.voltecx
powercraft-cli compare buck_v1.voltecx buck_v2.voltecx --tend 5m --measure 'pp(v(out), 4m, 5m)' --lang zh-Hant
```

It lists the parts added, removed and renamed, and the parameters, nets and variables that changed
(`file.diff`). With `--measure` or `--expect` it also simulates both files the same way and shows each value
for A and B with the change.

## Inside a Voltecx terminal: the open window

In a terminal opened in Voltecx (View › Terminal), `run`, `sim` and `script` act on the open window instead of a
separate session: the circuit they build or open appears there, each command is one step you can undo, and the
version history records it as the command line's. After acting they print one state line on stderr, so you (or
an agent working in that terminal) see what the window now shows:

```text
window: 0 errors, 1 warnings, 0 notes (value.range (R1.r)); last run: v(out) mean 12.01, ripple 0.0412 pp, efficiency 96.4 %
```

The line names the first three problems (`powercraft-cli app problems` lists them all) and the last run's key
figures, or `not run since the change` when the circuit changed after it. `--shot PATH` also writes a picture of
the window. A simulation runs in the window as a job, with its progress bar and Cancel button.

A `.voltecx` file that was written outside Voltecx (it has no version history next to it, as a file a script
wrote by hand) is not opened in the window: build the circuit with commands instead
(`run --new '{"name":"buck"}' --cmd circuit.apply ...`). Add `--headless` to any of the three to work in a
separate session as before; it says that the result is not in the window. `--root` and `sim --watch` work only
with `--headless`. In any other terminal nothing changes: the commands run without a window.

## Exports and the report

```sh
powercraft-cli export buck.voltecx --out out --schematic pdf --kicad --bom csv
powercraft-cli export buck-closed-loop.voltecx --out out --code stm32g474
powercraft-cli report buck.voltecx --out out --lang zh-Hant
```

`export` writes the schematic (`png`, `svg` or `pdf`), a KiCad 8 project, the BOM (`csv`, `json` or `md`; the
file is simulated first, since parts are picked from the stresses) and the control's C code (`portable`,
`stm32g474` or `f28379d`) into `out/FILE-code`. `--code` needs a circuit with a digital controller, such as the
example `buckClosedLoop`; the open-loop `buck` has none. `report` writes the design report as PDF or HTML (`--format html`).

## Shell completion

```sh
powercraft-cli completions bash > ~/.local/share/bash-completion/completions/powercraft-cli
powercraft-cli completions zsh > "${fpath[1]}/_powercraft-cli"
powercraft-cli completions fish > ~/.config/fish/completions/powercraft-cli.fish
powercraft-cli completions powershell >> $PROFILE
```

The scripts complete the subcommands and the options of each.
