Skip to content

Commit

Permalink
fix cli running without args
Browse files Browse the repository at this point in the history
  • Loading branch information
orsinium committed Jul 8, 2021
1 parent 91034d9 commit d88648e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
16 changes: 14 additions & 2 deletions deal/_cli/_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,24 @@ def main(
if root is None: # pragma: no cover
root = Path()
parser = ArgumentParser(prog='python3 -m deal')
parser.set_defaults(cmd=None)
subparsers = parser.add_subparsers()
for cmd_name, cmd_class in commands.items():
subparser = subparsers.add_parser(cmd_name)
descr = cmd_class.__doc__ or ''
descr = (descr.splitlines() or [''])[0]
subparser = subparsers.add_parser(
name=cmd_name,
description=descr,
)
cmd = cmd_class(stream=stream, root=root)
cmd.init_parser(subparser)
subparser.set_defaults(cmd=cmd)

args = parser.parse_args(argv)
try:
args = parser.parse_args(argv)
except SystemExit as exc:
return exc.code
if args.cmd is None:
main(['--help'], commands=commands, root=root, stream=stream)
return 2
return args.cmd(args)
14 changes: 10 additions & 4 deletions tests/test_cli/test_main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from argparse import ArgumentParser

import pytest

from deal._cli import main
from deal._cli._base import Command

Expand All @@ -22,8 +20,16 @@ def test_calls_command():


def test_unknown_command(capsys):
with pytest.raises(SystemExit):
main(argv=['unknown', 'a1', 'a2'], commands=dict(fake=FakeCommand))
result = main(argv=['unknown', 'a1', 'a2'], commands=dict(fake=FakeCommand))
assert result == 2
captured = capsys.readouterr()
exp = "invalid choice: 'unknown' (choose from 'fake')"
assert exp in captured.err


def test_no_command_specified(capsys):
result = main(argv=[], commands=dict(fake=FakeCommand))
assert result == 2
captured = capsys.readouterr()
assert 'positional arguments:' in captured.out
assert 'fake' in captured.out

0 comments on commit d88648e

Please sign in to comment.