diff --git a/mopidy/utils/command.py b/mopidy/utils/command.py index b8e2aa85..74a327e3 100644 --- a/mopidy/utils/command.py +++ b/mopidy/utils/command.py @@ -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)) diff --git a/tests/utils/command_test.py b/tests/utils/command_test.py index 50006fc8..344e2b90 100644 --- a/tests/utils/command_test.py +++ b/tests/utils/command_test.py @@ -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')