Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix(cli): require a subcommand in the CLI parser (#143)
  • Loading branch information
mhaye9545 committed Sep 2, 2026
commit 846b78f23e18afc5bb033b3c1f0e9b8825057782
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

### Bugfixes

- `gcmon` with no subcommand prints a usage message and exits 2, where it used to print an `AttributeError` traceback
- An instant sent close to the end of a run reaches the trace, where the last one a client sent could be dropped without a word
- A `Processes` slice names the program its own process was running. A reused PID used to put the first process's command line on every slice of that PID, and a process that exited before the first flush got none at all
- A `Processes` slice covers only the process it names, where a reused PID used to draw one slice spanning both and the stretch between them
Expand Down
15 changes: 2 additions & 13 deletions src/gcmon/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
add_run_parser,
)

logger = logging.getLogger("gcmon")


class _VersionAction(argparse.Action):
"""Print gcmon's version and exit.
Expand Down Expand Up @@ -49,7 +47,7 @@ def _create_parser() -> argparse.ArgumentParser:
dest=argparse.SUPPRESS,
help="Print the installed gcmon version and exit",
)
subparsers = parser.add_subparsers(dest="command", help="Available commands")
subparsers = parser.add_subparsers(dest="command", required=True, help="Available commands")

add_monitor_parser(subparsers.add_parser)
add_combine_parser(subparsers.add_parser)
Expand Down Expand Up @@ -141,16 +139,7 @@ def main(argv: list[str] | None = None) -> int:
_setup_logging(args.verbose)

# Dispatch via args.func (set by each subparser's set_defaults)
if hasattr(args, "func"):
return int(args.func(args))

# No command specified: default to monitor
if args.command is None:
return main(["monitor", *argv])

# Unknown command (should not happen due to argparse)
logger.error("Unknown command: %s", args.command)
return 1
return int(args.func(args))


if __name__ == "__main__":
Expand Down
50 changes: 50 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,56 @@ def test_main_combine_command(tmp_path: Path) -> None:
assert cli.main(["combine", str(input_file), "-o", str(tmp_path / "output.pftrace")]) == 0


def test_main_no_subcommand_exits_2(capsys: pytest.CaptureFixture[str]) -> None:
from gcmon.cli import main as cli

with pytest.raises(SystemExit) as excinfo:
cli.main([])

assert excinfo.value.code == 2
captured = capsys.readouterr()
assert "the following arguments are required: command" in captured.err
for subcommand in ("monitor", "combine", "run"):
assert subcommand in captured.err


def test_main_invalid_subcommand_exits_2(capsys: pytest.CaptureFixture[str]) -> None:
from gcmon.cli import main as cli

with pytest.raises(SystemExit) as excinfo:
cli.main(["12345"])

assert excinfo.value.code == 2
captured = capsys.readouterr()
assert "invalid choice: '12345'" in captured.err


def test_main_version_exits_0(capsys: pytest.CaptureFixture[str]) -> None:
from gcmon.cli import main as cli

with pytest.raises(SystemExit) as excinfo:
cli.main(["--version"])

assert excinfo.value.code == 0


def test_main_subcommands_dispatch(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
from gcmon.cli import main as cli

calls: list[str] = []
monkeypatch.setattr("gcmon.cli.commands.monitor_cmd.cmd_monitor", lambda args: calls.append("monitor") or 0)
assert cli.main(["monitor", "12345"]) == 0
assert calls == ["monitor"]

monkeypatch.setattr("gcmon.cli.commands.run_cmd.cmd_run", lambda args: calls.append("run") or 0)
assert cli.main(["run", "-m", "timeit"]) == 0
assert calls == ["monitor", "run"]

monkeypatch.setattr("gcmon.cli.commands.convert_cmd.cmd_combine", lambda args: calls.append("combine") or 0)
assert cli.main(["combine", str(tmp_path / "in.jsonl"), "-o", str(tmp_path / "out.pftrace")]) == 0
assert calls == ["monitor", "run", "combine"]


# =============================================================================
# CLI Help Tests
# =============================================================================
Expand Down
Loading