M test/test_cli_commands.py +30 -0
@@ 1,5 1,7 @@
import unittest
+import StringIO
+import sys
from vanity import commander
@@ 16,9 18,11 @@ def cmd1(fn, **opts):
"""
return 1
+
def cmd2(**opts):
return 2
+
def cmd3(**opts):
return 3
@@ 206,6 210,32 @@ class TestCliCommands(unittest.TestCase)
tst('help zork'.split())
self.assertRaises(commander.InvalidCommand, tst, 'zork -h'.split())
+ def test_main_dispatch(self):
+ arguments = 'foo check.txt'.split()
+ self.assertEqual(1,
+ commander.main(GOPT_A, CMDTABLE_A, arguments))
+
+ def test_main_dispatch_special(self):
+ sys.stderr = StringIO.StringIO()
+ try:
+ commander.main(GOPT_A, CMDTABLE_A, 'foo -h'.split())
+ caught = False
+ except:
+ caught = True
+ self.assertTrue(caught)
+ try:
+ commander.main(GOPT_A, CMDTABLE_A, 'fizz'.split())
+ caught = False
+ except:
+ caught = True
+ self.assertTrue(caught)
+ try:
+ commander.main(GOPT_A, CMDTABLE_A, 'b'.split())
+ caught = False
+ except:
+ caught = True
+ self.assertTrue(caught)
+
class Capture(list):
M vanity/commander.py +9 -4
@@ 23,11 23,15 @@ import sys
class InvalidCommand(cli.CliError):
"""The CLI was given an invalid command"""
- pass
+ def __init__(self, value):
+ cli.CliError.__init__(self, 'invalid command: %s' % value)
+
class MissingCommand(cli.CliError):
"""The user failed to specify a command"""
- pass
+ def __init__(self):
+ cli.CliError.__init__(self, 'missing required command name')
+
class AmbiguousCommand(cli.CliError):
"""The given command was ambigouous"""
@@ 216,8 220,9 @@ class Command(object):
def docstring(self):
"""Return a documentation string for the command.
"""
- if self.target and hasattr(self.target, '__doc__'):
- return self.target.__doc__ or 'No usage available'
+ doc = getattr(self.target, '__doc__', None)
+ if doc:
+ return doc
else:
return 'No usage available'