Skip to content

CLI

The lanexio-parser CLI parses HTML and Markdown files, runs LexQuery patterns, and serializes parse trees back to source. The binary is installed under two names: lanexio-parser (canonical) and parser (short alias). This page uses the canonical name throughout.

Package: @lanexio/parser-cli
Runtime: Node.js only.

Terminal window
pnpm add -g @lanexio/parser-cli
SubcommandDescription
lanexio-parser parse <file>Parse a file and print the tree or JSON.
lanexio-parser query <selector> <file>Run a LexQuery pattern and print matching nodes.
lanexio-parser serialize <file>Parse and re-serialize to source markup.

Every subcommand also accepts --help/-h and prints its own usage screen (exit code 0).

Terminal window
lanexio-parser parse index.html
lanexio-parser parse README.md
lanexio-parser parse --grammar html --output json - < input.html
FlagValuesDefaultDescription
--grammarhtml, markdown, autoautoGrammar to use. auto infers from file extension.
--outputtree, jsontreeOutput format. tree prints an indented text representation; json prints the full serialized tree.
ExtensionGrammar
.html, .htm, .xhtml, .svghtml
.md, .markdown, .mdxmarkdown

When reading from stdin (-), --grammar is required.

CodeMeaning
0Success. A tree was produced, even if it contains LexError nodes. Also: any --help invocation.
1Invocation or I/O error: file not found, unknown subcommand, unknown flag, invalid flag value, unrecognized grammar, invalid selector, or stdin with no --grammar.

The CLI always exits 0 when a tree was produced, even when the input has parse errors — check the JSON output’s per-node hasError to detect malformed input. Invocation mistakes, by contrast, always fail loudly:

Terminal window
lanexio-parser pares file.html # unknown subcommand → stderr + exit 1
lanexio-parser parse --ouput json a.html # unknown flag → stderr + exit 1
lanexio-parser parse --output yaml a.html # invalid value → "must be tree or json" + exit 1

Unknown subcommands, unknown flags, and invalid flag values are all rejected with a message on stderr and exit code 1 — they are never silently ignored.

Terminal window
lanexio-parser query Element index.html
lanexio-parser query "Element, Text" --grammar html --output json - < input.html
FlagValuesDefaultDescription
--grammarhtml, markdown, autoautoGrammar to use.
--outputtext, jsontextOutput format.
Terminal window
lanexio-parser serialize index.html
lanexio-parser serialize --grammar html - < input.html

Parses the input and prints the serialized output to stdout. Both HTML and Markdown serialization are supported. The grammar is detected from the file extension or specified with --grammar.

Terminal window
lanexio-parser serialize --grammar markdown README.md

serialize writes its output byte-exact — no trailing newline is appended — so round-trip comparisons like lanexio-parser serialize a.html | diff - a.html behave predictably. The parse and query dumps end with a single trailing newline.

Use - as the file argument (the file argument is always required — piping without - is an error):

Terminal window
echo '<p>Hello</p>' | lanexio-parser parse --grammar html -
cat index.html | lanexio-parser parse --grammar html --output json -

When reading from stdin, --grammar is required, and the check happens before any bytes are read — lanexio-parser parse - without --grammar fails immediately instead of blocking on stdin until end-of-input.

  • Query selectors are kind names, not CSS selectors. query h1 finds nodes of kind h1 (which does not exist), not <h1> elements.
  • No --include-errors flag. The parse --output json format includes hasError per node.
  • One input file per invocation. Extra positional arguments are rejected.