changeset 89:0804a706d6bf

handle CLI errors with optparse
author Jeff Hammel <jhammel@mozilla.com>
date Sat, 28 Apr 2012 15:29:57 -0700
parents 36cf1d9bf40b
children 6bf4a58b0988
files configuration/configuration.py tests/unit.py
diffstat 2 files changed, 11 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/configuration/configuration.py	Fri Apr 27 15:18:24 2012 -0700
+++ b/configuration/configuration.py	Sat Apr 28 15:29:57 2012 -0700
@@ -402,6 +402,7 @@
             # generate configuration
             self(*config)
         except MissingValueException, missingvalues:
+            # errors are handled below
             pass
 
         # dump configuration, if specified
@@ -412,7 +413,7 @@
 
         if missingvalues and not dump:
             # XXX assuming if you don't have values you were just dumping
-            raise missingvalues
+            self.error(str(missingvalues))
 
         # update options from config
         options.__dict__.update(self.config)
--- a/tests/unit.py	Fri Apr 27 15:18:24 2012 -0700
+++ b/tests/unit.py	Sat Apr 28 15:29:57 2012 -0700
@@ -78,12 +78,15 @@
 
         example = ExampleConfiguration()
 
-        exception = None
-        try:
-            example.parse_args(args=[])
-        except Exception, exception:
-            pass
-        self.assertTrue(isinstance(exception, configuration.MissingValueException))
+        # monkey-patch the error method from optparse.OptionParser
+        error_msg = []
+        def error(msg):
+            error_msg.append(msg)
+        example.error = error
+
+        # trigger it
+        example.parse_args(args=[])
+        self.assertEqual(error_msg, ['Parameter browser_path is required but not present'])
 
     def test_required(self):
         """ensure you have to have required values"""