From 3b099ccb89b0358ff9377b988a3f88bf7b5cdfc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Thu, 24 Aug 2023 17:09:04 +0200 Subject: [PATCH] clan-cli: move arg parsing to extra method --- pkgs/clan-cli/clan_cli/__init__.py | 22 +++++++++++++--------- pkgs/clan-cli/tests/test_cli.py | 20 ++++---------------- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/pkgs/clan-cli/clan_cli/__init__.py b/pkgs/clan-cli/clan_cli/__init__.py index fa8573b2e..d12d0c65b 100644 --- a/pkgs/clan-cli/clan_cli/__init__.py +++ b/pkgs/clan-cli/clan_cli/__init__.py @@ -16,8 +16,7 @@ except ImportError: pass -# this will be the entrypoint under /bin/clan (see pyproject.toml config) -def main() -> None: +def parse_args(args: list[str]) -> argparse.Namespace: parser = argparse.ArgumentParser(description="cLAN tool") subparsers = parser.add_subparsers() @@ -48,13 +47,18 @@ def main() -> None: if len(sys.argv) == 1: parser.print_help() - args = parser.parse_args() - if hasattr(args, "func"): - try: - args.func(args) - except ClanError as e: - print(f"{sys.argv[0]}: {e}") - sys.exit(1) + return parser.parse_args(args) + + +# this will be the entrypoint under /bin/clan (see pyproject.toml config) +def main() -> None: + args = parse_args(sys.argv[1:]) + assert hasattr(args, "func") + try: + args.func(args) + except ClanError as e: + print(f"{sys.argv[0]}: {e}") + sys.exit(1) if __name__ == "__main__": diff --git a/pkgs/clan-cli/tests/test_cli.py b/pkgs/clan-cli/tests/test_cli.py index 2028602a1..24cbd5464 100644 --- a/pkgs/clan-cli/tests/test_cli.py +++ b/pkgs/clan-cli/tests/test_cli.py @@ -1,22 +1,10 @@ -import sys - import pytest - -import clan_cli +from cli import Cli -def test_no_args( - capsys: pytest.CaptureFixture, monkeypatch: pytest.MonkeyPatch -) -> None: - monkeypatch.setattr(sys, "argv", [""]) - clan_cli.main() - captured = capsys.readouterr() - assert captured.out.startswith("usage:") - - -def test_help(capsys: pytest.CaptureFixture, monkeypatch: pytest.MonkeyPatch) -> None: - monkeypatch.setattr(sys, "argv", ["", "--help"]) +def test_help(capsys: pytest.CaptureFixture) -> None: + cli = Cli() with pytest.raises(SystemExit): - clan_cli.main() + cli.run(["--help"]) captured = capsys.readouterr() assert captured.out.startswith("usage:")