summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2013-07-15 19:16:52 +0000
committerGerrit Code Review <review@openstack.org>2013-07-15 19:16:52 +0000
commitb96ebd3e5d958d3bf52508a0df5fcc77daa4bdf5 (patch)
treeab4db04f493f0d0ca0f4e83b1b27ff4197f95fd6
parent4f44b97017e7f2b5920065f63df9ebe50027f4fb (diff)
parent3302fb05723e62f9b02c2cce74e30ded1edfda51 (diff)
downloadoslo-config-b96ebd3e5d958d3bf52508a0df5fcc77daa4bdf5.tar.gz
Merge "Raise an exception when _oparser is not initialized"
-rw-r--r--oslo/config/cfg.py33
-rw-r--r--tests/test_cfg.py17
2 files changed, 47 insertions, 3 deletions
diff --git a/oslo/config/cfg.py b/oslo/config/cfg.py
index bf8613a..6aafd30 100644
--- a/oslo/config/cfg.py
+++ b/oslo/config/cfg.py
@@ -288,6 +288,13 @@ class Error(Exception):
return self.msg
+class NotInitializedError(Error):
+ """Raised if parser is not initialized yet."""
+
+ def __str__(self):
+ return "call expression on parser has not been invoked"
+
+
class ArgsAlreadyParsedError(Error):
"""Raised if a CLI opt is registered after parsing."""
@@ -1635,7 +1642,7 @@ class ConfigOpts(collections.Mapping):
removed as a side-effect of this method.
"""
self._args = None
- self._oparser = argparse.ArgumentParser()
+ self._oparser = None
self._namespace = None
self.unregister_opts(self._config_opts)
for group in self._groups.values():
@@ -1909,11 +1916,31 @@ class ConfigOpts(collections.Mapping):
logger.log(lvl, "*" * 80)
def print_usage(self, file=None):
- """Print the usage message for the current program."""
+ """Print the usage message for the current program.
+
+ This method is for use after all CLI options are known
+ registered using __call__() method. If this method is called
+ before the __call__() is invoked, it throws NotInitializedError
+
+ :param file: the File object (if None, output is on sys.stdout)
+ :raises: NotInitializedError
+ """
+ if not self._oparser:
+ raise NotInitializedError()
self._oparser.print_usage(file)
def print_help(self, file=None):
- """Print the help message for the current program."""
+ """Print the help message for the current program.
+
+ This method is for use after all CLI options are known
+ registered using __call__() method. If this method is called
+ before the __call__() is invoked, it throws NotInitializedError
+
+ :param file: the File object (if None, output is on sys.stdout)
+ :raises: NotInitializedError
+ """
+ if not self._oparser:
+ raise NotInitializedError()
self._oparser.print_help(file)
def _get(self, name, group=None, namespace=None):
diff --git a/tests/test_cfg.py b/tests/test_cfg.py
index 698012a..b75f4bc 100644
--- a/tests/test_cfg.py
+++ b/tests/test_cfg.py
@@ -2831,3 +2831,20 @@ class ChoicesTestCase(BaseTestCase):
self.assertTrue(hasattr(self.conf, 'foo'))
self.assertEqual(self.conf.foo, 'baaar')
+
+
+class PrintHelpTestCase(utils.BaseTestCase):
+
+ def test_print_help_without_init(self):
+ conf = cfg.ConfigOpts()
+ conf.register_opts([])
+ self.assertRaises(cfg.NotInitializedError,
+ conf.print_help)
+
+ def test_print_help_with_clear(self):
+ conf = cfg.ConfigOpts()
+ conf.register_opts([])
+ conf([])
+ conf.clear()
+ self.assertRaises(cfg.NotInitializedError,
+ conf.print_help)