# HG changeset patch # User Jeff Hammel # Date 1335652197 25200 # Node ID 0804a706d6bfd715a7d6bf81c0df681fc5794764 # Parent 36cf1d9bf40b112cbe4eaaf1f449e6adb755f289 handle CLI errors with optparse diff -r 36cf1d9bf40b -r 0804a706d6bf configuration/configuration.py --- 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) diff -r 36cf1d9bf40b -r 0804a706d6bf tests/unit.py --- 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"""