commands: Make sure parser errors get translated to correct error type.
This commit is contained in:
parent
fa7eee3bdf
commit
88bc046605
@ -7,6 +7,11 @@ class CommandError(Exception):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class ArgumentParser(argparse.ArgumentParser):
|
||||||
|
def error(self, message):
|
||||||
|
raise CommandError(message)
|
||||||
|
|
||||||
|
|
||||||
class Command(object):
|
class Command(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._children = collections.OrderedDict()
|
self._children = collections.OrderedDict()
|
||||||
@ -14,7 +19,7 @@ class Command(object):
|
|||||||
|
|
||||||
def _build(self):
|
def _build(self):
|
||||||
actions = []
|
actions = []
|
||||||
parser = argparse.ArgumentParser(add_help=False)
|
parser = ArgumentParser(add_help=False)
|
||||||
|
|
||||||
for args, kwargs in self._arguments:
|
for args, kwargs in self._arguments:
|
||||||
actions.append(parser.add_argument(*args, **kwargs))
|
actions.append(parser.add_argument(*args, **kwargs))
|
||||||
|
|||||||
@ -103,6 +103,13 @@ class CommandParsingTest(unittest.TestCase):
|
|||||||
result = cmd.parse([])
|
result = cmd.parse([])
|
||||||
self.assertEqual(result.command, cmd)
|
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):
|
class UsageTest(unittest.TestCase):
|
||||||
@mock.patch('sys.argv')
|
@mock.patch('sys.argv')
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user