From 8d7224325f56b624a3059f28983b090725d1fb2a Mon Sep 17 00:00:00 2001 From: Mark McLoughlin Date: Mon, 23 Jan 2012 10:42:07 +0000 Subject: Add support to cfg for disabling interspersed args Implements blueprint cfg-disable-interspersed-args Nova currently relies on cfg being implemented with optparse because it uses optparse's disable_interspersed_args() The use case for this is if you do: $> nova-manage --verbose create --project foo --user bar you want invoking ConfigOpts() to return: ['create', '--project', 'foo', '--user', 'bar'] as the "extra" args rather than aborting when it doesn't recognize the --project arg. This is a reasonable use case for cfg to support and it should just have {disable,enable}_interspersed_args() methods. If we ever switch from optparse to argparse, we'll do something like this: parser.add_argument('--verbose') ... parser.add_argument( 'extra_args', nargs=argparse.REMAINDER if disable_interspersed_args else '*') ... ns = parser.parse_args(...) extra_args = ns.extra_args i.e. we will need an 'extra_args' multi-value positional argument in any case and we'll just pass nargs=REMAINDER if we want trailing options to be included in the extra args. Change-Id: I3ecb7dc18230327cf5aaaa7d832224e64aafa40c --- tests/unit/test_cfg.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'tests') diff --git a/tests/unit/test_cfg.py b/tests/unit/test_cfg.py index 2ca1182..3f5931b 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): -- cgit