clan-cli: move arg parsing to extra method
This commit is contained in:
@@ -16,8 +16,7 @@ except ImportError:
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
# this will be the entrypoint under /bin/clan (see pyproject.toml config)
|
def parse_args(args: list[str]) -> argparse.Namespace:
|
||||||
def main() -> None:
|
|
||||||
parser = argparse.ArgumentParser(description="cLAN tool")
|
parser = argparse.ArgumentParser(description="cLAN tool")
|
||||||
subparsers = parser.add_subparsers()
|
subparsers = parser.add_subparsers()
|
||||||
|
|
||||||
@@ -48,13 +47,18 @@ def main() -> None:
|
|||||||
if len(sys.argv) == 1:
|
if len(sys.argv) == 1:
|
||||||
parser.print_help()
|
parser.print_help()
|
||||||
|
|
||||||
args = parser.parse_args()
|
return parser.parse_args(args)
|
||||||
if hasattr(args, "func"):
|
|
||||||
try:
|
|
||||||
args.func(args)
|
# this will be the entrypoint under /bin/clan (see pyproject.toml config)
|
||||||
except ClanError as e:
|
def main() -> None:
|
||||||
print(f"{sys.argv[0]}: {e}")
|
args = parse_args(sys.argv[1:])
|
||||||
sys.exit(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__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
@@ -1,22 +1,10 @@
|
|||||||
import sys
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
from cli import Cli
|
||||||
import clan_cli
|
|
||||||
|
|
||||||
|
|
||||||
def test_no_args(
|
def test_help(capsys: pytest.CaptureFixture) -> None:
|
||||||
capsys: pytest.CaptureFixture, monkeypatch: pytest.MonkeyPatch
|
cli = Cli()
|
||||||
) -> 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"])
|
|
||||||
with pytest.raises(SystemExit):
|
with pytest.raises(SystemExit):
|
||||||
clan_cli.main()
|
cli.run(["--help"])
|
||||||
captured = capsys.readouterr()
|
captured = capsys.readouterr()
|
||||||
assert captured.out.startswith("usage:")
|
assert captured.out.startswith("usage:")
|
||||||
|
|||||||
Reference in New Issue
Block a user