Skip to content

CLI

The parser CLI parses HTML and Markdown files, runs LexQuery patterns, and serializes parse trees back to markup.

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

Terminal window
pnpm add -g @lanexio/parser-cli
SubcommandDescription
parser parse <file>Parse a file and print the tree or JSON.
parser query <pattern> <file>Run a LexQuery pattern and print matching nodes.
parser serialize <file>Parse and re-serialize to HTML or Markdown source.
Terminal window
parser parse index.html
parser parse README.md
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.
--encodingWHATWG labelutf-8Input file encoding (e.g. iso-8859-1, windows-1252, utf-16le).
ExtensionGrammar
.html, .htmhtml
.md, .markdownmarkdown

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

CodeMeaning
0Success. A tree was produced, even if it contains LexError nodes.
1System error (file not found, I/O failure).
2Argument misuse (missing argument, unknown flag, invalid grammar, invalid encoding).

The CLI always exits 0 when a tree was produced, even when the input has parse errors. Check the JSON output for hasError to detect malformed input.

Terminal window
parser query Element index.html
parser query "Element, Text" --grammar html --output json < input.html
FlagValuesDefaultDescription
--grammarhtml, markdown, autoautoGrammar to use.
--outputtext, jsontextOutput format.
--encodingWHATWG labelutf-8Input file encoding.
Terminal window
parser serialize index.html
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
parser serialize --grammar markdown README.md

These flags work with any subcommand:

FlagDescription
--versionPrint the version (1.0.0) and exit. Overrides the subcommand.
--quietSuppress stderr diagnostic output. Useful in scripts where only the exit code and stdout matter.

Use - as the file argument, or pipe input:

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

When reading from stdin, --grammar is required. Auto-detection needs a file extension.

Use -- to prevent subsequent arguments from being interpreted as flags. Everything after -- is treated as a positional argument:

Terminal window
parser parse -- some-file.html

Use --quiet to suppress error messages on stderr. The exit code still reflects the result (0 for success, 1 for system errors, 2 for argument errors):

Terminal window
if parser parse --quiet config.html; then
echo "Parsed successfully"
fi

For files not in UTF-8, use --encoding with a WHATWG Encoding label:

Terminal window
parser parse --encoding windows-1252 legacy.html
parser parse --output json --encoding iso-8859-1 latin1-file.html
  • 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.
  • Multi-file glob patterns and streaming parse for large inputs are not yet supported.