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.
Install
Section titled “Install”pnpm add -g @lanexio/parser-cliSubcommands
Section titled “Subcommands”| Subcommand | Description |
|---|---|
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. |
parser parse index.htmlparser parse README.mdparser parse --grammar html --output json < input.htmlOptions
Section titled “Options”| Flag | Values | Default | Description |
|---|---|---|---|
--grammar | html, markdown, auto | auto | Grammar to use. auto infers from file extension. |
--output | tree, json | tree | Output format. tree prints an indented text representation; json prints the full serialized tree. |
--encoding | WHATWG label | utf-8 | Input file encoding (e.g. iso-8859-1, windows-1252, utf-16le). |
Grammar auto-detection
Section titled “Grammar auto-detection”| Extension | Grammar |
|---|---|
.html, .htm | html |
.md, .markdown | markdown |
When reading from stdin (-), --grammar is required.
Exit codes
Section titled “Exit codes”| Code | Meaning |
|---|---|
0 | Success. A tree was produced, even if it contains LexError nodes. |
1 | System error (file not found, I/O failure). |
2 | Argument 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.
parser query Element index.htmlparser query "Element, Text" --grammar html --output json < input.htmlOptions
Section titled “Options”| Flag | Values | Default | Description |
|---|---|---|---|
--grammar | html, markdown, auto | auto | Grammar to use. |
--output | text, json | text | Output format. |
--encoding | WHATWG label | utf-8 | Input file encoding. |
serialize
Section titled “serialize”parser serialize index.htmlparser serialize --grammar html < input.htmlParses 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.
parser serialize --grammar markdown README.mdGlobal flags
Section titled “Global flags”These flags work with any subcommand:
| Flag | Description |
|---|---|
--version | Print the version (1.0.0) and exit. Overrides the subcommand. |
--quiet | Suppress stderr diagnostic output. Useful in scripts where only the exit code and stdout matter. |
Reading from stdin
Section titled “Reading from stdin”Use - as the file argument, or pipe input:
echo '<p>Hello</p>' | parser parse --grammar htmlcat index.html | parser parse --grammar html --output jsonWhen reading from stdin, --grammar is required. Auto-detection needs a file extension.
-- separator
Section titled “-- separator”Use -- to prevent subsequent arguments from being interpreted as flags. Everything after -- is treated as a positional argument:
parser parse -- some-file.html--quiet for scripting
Section titled “--quiet for scripting”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):
if parser parse --quiet config.html; then echo "Parsed successfully"fiEncoding selection
Section titled “Encoding selection”For files not in UTF-8, use --encoding with a WHATWG Encoding label:
parser parse --encoding windows-1252 legacy.htmlparser parse --output json --encoding iso-8859-1 latin1-file.htmlKnown limitations in v1.0
Section titled “Known limitations in v1.0”- Query selectors are kind names, not CSS selectors.
query h1finds nodes of kindh1(which does not exist), not<h1>elements. - No
--include-errorsflag. Theparse --output jsonformat includeshasErrorper node. - Multi-file glob patterns and streaming parse for large inputs are not yet supported.