diff options
| author | Jenkins <jenkins@review.openstack.org> | 2012-01-23 15:36:37 +0000 |
|---|---|---|
| committer | Gerrit Code Review <review@openstack.org> | 2012-01-23 15:36:37 +0000 |
| commit | fbf2500e62faf0bbd8ae202b66fd0d0e8561b2f3 (patch) | |
| tree | 8a5d2fbcd5c3268a8321ae4117dea661fc6b1afe | |
| parent | 3ca8215426f8eb651174b6859e12e0a9f29d4494 (diff) | |
| parent | 8d7224325f56b624a3059f28983b090725d1fb2a (diff) | |
| download | oslo-fbf2500e62faf0bbd8ae202b66fd0d0e8561b2f3.tar.gz oslo-fbf2500e62faf0bbd8ae202b66fd0d0e8561b2f3.tar.xz oslo-fbf2500e62faf0bbd8ae202b66fd0d0e8561b2f3.zip | |
Merge "Add support to cfg for disabling interspersed args"
| -rw-r--r-- | openstack/common/cfg.py | 41 | ||||
| -rw-r--r-- | tests/unit/test_cfg.py | 12 |
2 files changed, 53 insertions, 0 deletions
diff --git a/openstack/common/cfg.py b/openstack/common/cfg.py index 891fcbe..eacd180 100644 --- a/openstack/common/cfg.py +++ b/openstack/common/cfg.py @@ -193,6 +193,22 @@ Option values may reference other values using PEP 292 string substitution: ] Note that interpolation can be avoided by using '$$'. + +For command line utilities that dispatch to other command line utilities, the +disable_interspersed_args() method is available. If this this method is called, +then parsing e.g. + + script --verbose cmd --debug /tmp/mything + +will no longer return: + + ['cmd', '/tmp/mything'] + +as the leftover arguments, but will instead return: + + ['cmd', '--debug', '/tmp/mything'] + +i.e. argument parsing is stopped at the first non-option argument. """ import sys @@ -875,6 +891,31 @@ class ConfigOpts(object): opt_info = self._get_opt_info(name, group) opt_info['default'] = default + def disable_interspersed_args(self): + """Set parsing to stop on the first non-option. + + If this this method is called, then parsing e.g. + + script --verbose cmd --debug /tmp/mything + + will no longer return: + + ['cmd', '/tmp/mything'] + + as the leftover arguments, but will instead return: + + ['cmd', '--debug', '/tmp/mything'] + + i.e. argument parsing is stopped at the first non-option argument. + """ + self._oparser.disable_interspersed_args() + + def enable_interspersed_args(self): + """Set parsing to not stop on the first non-option. + + This it the default behaviour.""" + self._oparser.enable_interspersed_args() + def log_opt_values(self, logger, lvl): """Log the value of all registered opts. diff --git a/tests/unit/test_cfg.py b/tests/unit/test_cfg.py index 0b5ee46..7763e75 100644 --- a/tests/unit/test_cfg.py +++ b/tests/unit/test_cfg.py @@ -196,6 +196,18 @@ class CliOptsTestCase(BaseTestCase): self.assertEquals(self.conf.config_file, paths) + def test_disable_interspersed_args(self): + self.conf.register_cli_opt(BoolOpt('foo')) + self.conf.register_cli_opt(BoolOpt('bar')) + + args = ['--foo', 'blaa', '--bar'] + + self.assertEquals(self.conf(args), args[1:2]) + self.conf.disable_interspersed_args() + self.assertEquals(self.conf(args), args[1:]) + self.conf.enable_interspersed_args() + self.assertEquals(self.conf(args), args[1:2]) + class ConfigFileOptsTestCase(BaseTestCase): |
