From f5a5f9e9b1bb26f85a01dd089dac8aa3341b9974 Mon Sep 17 00:00:00 2001 From: Thomas Adamcik Date: Tue, 12 Nov 2013 22:58:59 +0100 Subject: [PATCH] commands: Add str method CommandError --- mopidy/utils/command.py | 3 +++ tests/utils/command_test.py | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/mopidy/utils/command.py b/mopidy/utils/command.py index b6645c51..7c6e874b 100644 --- a/mopidy/utils/command.py +++ b/mopidy/utils/command.py @@ -9,6 +9,9 @@ class CommandError(Exception): self.message = message self.usage = usage + def __str__(self): + return '%s\n\nerror: %s\n' % (self.usage, self.message) + class ArgumentParser(argparse.ArgumentParser): def error(self, message): diff --git a/tests/utils/command_test.py b/tests/utils/command_test.py index 14530cc6..016de37f 100644 --- a/tests/utils/command_test.py +++ b/tests/utils/command_test.py @@ -423,3 +423,14 @@ class HelpTest(unittest.TestCase): ' some text about this sub-command.\n\n' ' --test TEST the great and wonderful') self.assertEqual(expected, cmd.format_help('foo').strip()) + + +class CommandErrorTest(unittest.TestCase): + def test_args_get_stored(self): + error = command.CommandError('message', usage='usage: foo') + self.assertEqual(error.message, 'message') + self.assertEqual(error.usage, 'usage: foo') + + def test_str_command_error(self): + error = command.CommandError('message', usage='usage: foo') + self.assertEqual(str(error), 'usage: foo\n\nerror: message\n')