Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
_Notes on the upcoming release will go here._
<!-- END PLACEHOLDER - ADD NEW CHANGELOG ENTRIES BELOW THIS LINE -->

### Fixes

#### Report invalid CLI arguments (#188)

`python -m libvt` reports unreadable input on stderr. The `render`
command identifies an invalid `--rows` or `--cols` before reading input.
Both errors exit with status 2.

## libvt 0.0.1a5 (2026-11-30)

`libvt` 0.0.1a5 adds rectangular grid blitting, display-column text writes, and
Expand Down
26 changes: 23 additions & 3 deletions src/libvt/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pathlib
import sys

from libvt._ucd import sanitize_terminal_text
from libvt.codec import format_explain
from libvt.terminal import render, strip

Expand All @@ -16,6 +17,17 @@ def _read_input(path: str | None) -> bytes:
return pathlib.Path(path).read_bytes()


def _positive_dimension(value: str) -> int:
try:
dimension = int(value)
except ValueError:
dimension = 0
if dimension < 1:
msg = f"expected a positive integer, got {value!r}"
raise argparse.ArgumentTypeError(msg)
return dimension


def main(argv: list[str] | None = None) -> int:
"""Run ``python -m libvt`` subcommands."""
parser = argparse.ArgumentParser(
Expand All @@ -29,14 +41,22 @@ def main(argv: list[str] | None = None) -> int:

p_render = sub.add_parser("render", help="Render VT stream to plain screen text")
p_render.add_argument("file", nargs="?", default="-", help="Input file or '-'")
p_render.add_argument("--rows", type=int, default=24, help="Screen rows")
p_render.add_argument("--cols", type=int, default=80, help="Screen columns")
p_render.add_argument(
"--rows", type=_positive_dimension, default=24, help="Screen rows"
)
p_render.add_argument(
"--cols", type=_positive_dimension, default=80, help="Screen columns"
)

p_strip = sub.add_parser("strip", help="Strip VT control sequences from input")
p_strip.add_argument("file", nargs="?", default="-", help="Input file or '-'")

args = parser.parse_args(argv)
raw = _read_input(args.file)
try:
raw = _read_input(args.file)
except OSError as exc:
path = sanitize_terminal_text(args.file)
parser.error(f"cannot read {path}: {exc.strerror or exc}")

if args.command == "explain":
out = format_explain(raw)
Expand Down
31 changes: 31 additions & 0 deletions tests/test_codec.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,34 @@ def test_codec_conformance_tables_cover_all_modes_and_documented_osc() -> None:

assert len(_C0_NAMES) >= 9
assert {"CUP", "SGR", "DECSTBM", "XTWINOPS", "REP"} <= set(_CSI_NAMES.values())


@pytest.mark.parametrize("command", ["render", "strip", "explain"])
def test_cli_reports_missing_input(
command: str, tmp_path: pathlib.Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""Show the failing path on stderr and retain a nonzero process exit."""
path = tmp_path / r"missing\session.vt"
with pytest.raises(SystemExit) as exit_info:
main([command, str(path)])
assert exit_info.value.code == 2
out, err = capsys.readouterr()
assert not out
assert str(path) in err and "Traceback" not in err


@pytest.mark.parametrize(
("flag", "value"), [("--rows", "0"), ("--cols", "-1"), ("--cols", "bad-width")]
)
def test_cli_rejects_geometry_before_opening_input(
flag: str, value: str, tmp_path: pathlib.Path, capsys: pytest.CaptureFixture[str]
) -> None:
"""Point to the invalid dimension even when the input path is missing."""
missing = tmp_path / "missing.vt"
with pytest.raises(SystemExit) as exit_info:
main(["render", flag, value, str(missing)])
assert exit_info.value.code == 2
out, err = capsys.readouterr()
assert not out
assert flag in err and "positive integer" in err and repr(value) in err
assert str(missing) not in err
Loading