commands: Make sure parser errors get translated to correct error type.

This commit is contained in:
Thomas Adamcik 2013-11-12 01:42:38 +01:00
parent fa7eee3bdf
commit 88bc046605
2 changed files with 13 additions and 1 deletions

View File

@ -7,6 +7,11 @@ class CommandError(Exception):
pass
class ArgumentParser(argparse.ArgumentParser):
def error(self, message):
raise CommandError(message)
class Command(object):
def __init__(self):
self._children = collections.OrderedDict()
@ -14,7 +19,7 @@ class Command(object):
def _build(self):
actions = []
parser = argparse.ArgumentParser(add_help=False)
parser = ArgumentParser(add_help=False)
for args, kwargs in self._arguments:
actions.append(parser.add_argument(*args, **kwargs))

View File

@ -103,6 +103,13 @@ class CommandParsingTest(unittest.TestCase):
result = cmd.parse([])
self.assertEqual(result.command, cmd)
def test_missing_positionals(self):
cmd = command.Command()
cmd.add_argument('foo')
with self.assertRaises(command.CommandError):
cmd.parse([])
class UsageTest(unittest.TestCase):
@mock.patch('sys.argv')