Skip to content

Command line

The Diagrist command line: format, check and build .dgr files, and look up the grammar of a kind.

Installed as dgr and diagrist. Usage: dgr <command> [arguments] [options]; the command comes first.

CommandWhat it does
fmtFormats .dgr files in place.
checkChecks a diagram against the grammar and the rules of its kind and prints every finding, those about one picture of a file with variants marked with the variant.
buildRenders a diagram as SVG, and with --png also as PNG: the base picture as <file>.svg and each variant as <file>.<variant>.svg.
grammarShows what a diagram kind allows.
helpExplains the commands.
OptionMeaning
--jsonPrint one JSON document on stdout instead of text, for agents and tools. Its form is stable, see “JSON output”.
--locale <en|de>Language of messages and diagnostics. Default: en.
-h, --helpShow the help of the command instead of running it.

Without a command:

OptionMeaning
--versionPrint the version of the CLI.
CodeMeaning
0Done: no finding with severity error. Warnings alone do not fail.
1A finding with severity error, or fmt --check found a file that is not formatted.
2The call cannot be carried out: unknown command or option, missing or extra argument, a value out of range, unreadable or unwritable file, a PNG that cannot be rendered. Also an unexpected error inside dgr, which is a bug and printed in one line on stderr.
Terminal window
dgr fmt <files...> [options]

Formats .dgr files in place. Two spaces per indent level and at most one blank line; token order, aligned columns and comments stay as written. A file with read errors is left unchanged, because formatting it could drop lines.

ArgumentMeaning
<files...>The .dgr files to format.
OptionMeaning
--checkChange nothing; exit with 1 if a file is not formatted. For CI and pre-commit hooks.

Examples:

Terminal window
# Format one file in place.
dgr fmt diagrams/order.dgr
# Fail when a file is not formatted, without changing it.
dgr fmt --check diagrams/*.dgr

With --json, besides the fields of every document:

FieldMeaning
filesOne entry per file, { file, status }. status is formatted (rewritten), unchanged (already formatted), unformatted (only with --check: formatting would change it) or invalid (has read errors, left unchanged).
diagnosticsRead errors and warnings of all files, see “Diagnostics”.
Terminal window
dgr check <file> [options]

Checks a diagram against the grammar and the rules of its kind and prints every finding, those about one picture of a file with variants marked with the variant. Writes nothing.

ArgumentMeaning
<file>The .dgr file to check.

Examples:

Terminal window
# Print the findings; exit with 1 on an error.
dgr check diagrams/order.dgr
# The same as one JSON document, for agents.
dgr check diagrams/order.dgr --json

With --json, besides the fields of every document:

FieldMeaning
fileThe checked file.
diagnosticsAll findings, see “Diagnostics”.
Terminal window
dgr build <file> [options]

Renders a diagram as SVG, and with --png also as PNG: the base picture as <file>.svg and each variant as <file>.<variant>.svg. Nothing is written when the diagram has an error; warnings are printed and do not stop the build. With --watch it builds again after every change of the source or of a file it reads, through use or a value naming a file.

ArgumentMeaning
<file>The .dgr file to render.
OptionMeaning
-o, --out <file>Where to write the SVG of a diagram with one picture, or of the one --variant picks. Default: next to the source, with .svg instead of .dgr. The PNG goes beside the SVG.
--variant <id>Build only the picture of this variant, as <file>.<id>.svg or where -o says.
--pngAlso write a PNG with the name of the SVG and .png. Rendered without a browser and with the bundled fonts, so the same diagram gives the same bytes on every machine.
--scale <factor>Pixels of the PNG per SVG unit, from 0.25 to 8; 2 doubles width and height. Default: 2. Needs --png.
--watchBuild, then build again each time the source or a file it reads changes, until Ctrl+C. Each build prints what it wrote and its findings, with --json one document per build. Errors in the diagram and a source that is gone for a moment do not end the watch; only a call refused at the first build does. Ends with the exit code of the last build.

Examples:

Terminal window
# Write diagrams/order.svg, and one SVG per variant beside it.
dgr build diagrams/order.dgr
# Write the SVG somewhere else.
dgr build diagrams/order.dgr -o site/order.svg
# Also write diagrams/order.png, twice the size of the SVG.
dgr build diagrams/order.dgr --png
# Write the PNG at three times the size of the SVG, e.g. for print.
dgr build diagrams/order.dgr --png --scale 3
# Write only diagrams/order.next.svg.
dgr build diagrams/order.dgr --variant next
# Write SVG and PNG again on every save.
dgr build diagrams/order.dgr --png --watch

With --json, besides the fields of every document:

FieldMeaning
fileThe rendered source.
outputPath of the first written SVG, the base unless --variant picks another; null when nothing was written.
pngPath of the PNG beside output, null without --png or when nothing was written.
picturesEvery picture asked for: { variant, output, png }, variant null for the base, the paths null when nothing was written.
diagnosticsAll findings, see “Diagnostics”.
Terminal window
dgr grammar [kind] [options]

Shows what a diagram kind allows. It lists the header, statements, arguments, attributes, flags and line forms, each with its doc and examples. Without a kind it lists the kinds. Derived from the same tables that check the diagrams.

ArgumentMeaning
[kind]A diagram kind such as sequence.

Examples:

Terminal window
# List the diagram kinds.
dgr grammar
# The grammar of sequence diagrams as data, for agents.
dgr grammar sequence --json

With --json, besides the fields of every document:

FieldMeaning
kindsWithout a kind: [{ kind, doc }] for every kind.
grammarWith a kind: its grammar as data, { kind, doc, header, statements }; each statement lists keyword, doc, examples, arguments, clauses, attributes, flags, children and its line form lines. Each table is described once; elsewhere a child is { keyword, ref }, where ref is a JSON Pointer into grammar such as /statements/9. It validates against the JSON Schema in schemas/grammar.schema.json.
Terminal window
dgr help [command] [options]

Explains the commands. Without a command it lists them all, with the options every command takes and the exit codes.

ArgumentMeaning
[command]The command to explain, such as fmt.

Examples:

Terminal window
# List the commands.
dgr help
# Arguments, options and examples of `fmt`; the same as `dgr fmt --help`.
dgr help fmt
# All command tables as data, for agents.
dgr help --json

With --json, besides the fields of every document:

FieldMeaning
commandsThe tables of the explained commands: { name, doc, usage, arguments, options, examples, json }, all of them without a command.
optionsOptions every command takes, in the same form as the options of a command.
rootOptionsOptions of dgr without a command.
exitCodes[{ code, doc }].

With --json every call prints exactly one JSON document on stdout, also when it is refused. Fields in every document:

FieldMeaning
diagrist-cliVersion of this JSON form, currently 1. It is raised only when a field changes meaning or goes away.
commandThe command that ran, null for dgr --version and when no command was recognized.
oktrue exactly when the exit code is 0.
errorOnly when the call was refused (exit code 2): { code, message }, with a code C… that is stable for tools.
versionOnly for dgr --version: the version of the CLI.

The fields of each command are listed in its section.

FieldMeaning
filePath of the file the finding is in: the source as given on the command line, or a file it uses.
pictureOnly for a finding about one picture of a file with variants: the id of the variant.
codeStable code such as L001; the prefix names the package or plugin that reports it.
severityerror (exit code 1) or warning.
span{ line, column, length }: 1-based line and column, length in characters.
valuesThe values the message is built from, by name. Use these instead of parsing the message.
messageThe text in the requested locale.