From be27982b4a53aaa19caa95c70922079e6fcd71b1 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Wed, 2 Dec 2026 19:16:30 -0600 Subject: [PATCH 1/3] cli(fix[render]): Require positive dimensions why: Invalid dimensions should fail before reading input. what: - Validate both geometry options at the argument boundary. - Check stderr and status when the input path is also invalid. Refs #128 --- src/libvt/__main__.py | 19 +++++++++++++++++-- tests/test_codec.py | 17 +++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/libvt/__main__.py b/src/libvt/__main__.py index afbaf3f..4ea3844 100644 --- a/src/libvt/__main__.py +++ b/src/libvt/__main__.py @@ -16,6 +16,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( @@ -29,8 +40,12 @@ 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 '-'") diff --git a/tests/test_codec.py b/tests/test_codec.py index f18bbf1..74d0626 100644 --- a/tests/test_codec.py +++ b/tests/test_codec.py @@ -83,3 +83,20 @@ 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( + ("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 From c2e715f023470886ec9f6e070030367c96724963 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Wed, 2 Dec 2026 19:16:30 -0600 Subject: [PATCH 2/3] cli(fix[input]): Report unreadable paths why: File read errors escape the command boundary as tracebacks. what: - Report unreadable input through the shared argument parser. - Exercise each subcommand's stderr and exit status. Fixes #128 --- src/libvt/__main__.py | 7 ++++++- tests/test_codec.py | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/libvt/__main__.py b/src/libvt/__main__.py index 4ea3844..8f65260 100644 --- a/src/libvt/__main__.py +++ b/src/libvt/__main__.py @@ -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 @@ -51,7 +52,11 @@ def main(argv: list[str] | None = None) -> int: 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) diff --git a/tests/test_codec.py b/tests/test_codec.py index 74d0626..f197729 100644 --- a/tests/test_codec.py +++ b/tests/test_codec.py @@ -85,6 +85,20 @@ def test_codec_conformance_tables_cover_all_modes_and_documented_osc() -> None: 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")] ) From d1694cef75189abdcc5be7b88e927ad27f40056f Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Wed, 2 Dec 2026 19:57:55 -0600 Subject: [PATCH 3/3] CHANGES(docs): Record cli input errors why: Readers need the behavior and scope of this change at release. what: - Add the unreleased entry for PR #188 - Preserve previously published notes and the parent branch's entries --- CHANGES | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGES b/CHANGES index ca1c339..d08210d 100644 --- a/CHANGES +++ b/CHANGES @@ -6,6 +6,14 @@ _Notes on the upcoming release will go here._ +### 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