Merge "Support hyphens in positional argument names"

This commit is contained in:
Zuul 2019-12-20 11:36:47 +00:00 committed by Gerrit Code Review
commit edcb46deae
2 changed files with 15 additions and 16 deletions

View File

@ -721,6 +721,13 @@ class Opt(object):
def hyphen(arg):
return arg if not positional else ''
# Because we must omit the dest parameter when using a positional
# argument, the name supplied for the positional argument must not
# include hyphens.
if positional:
prefix = prefix.replace('-', '_')
name = name.replace('-', '_')
args = [hyphen('--') + prefix + name]
if short:
args.append(hyphen('-') + short)

View File

@ -976,15 +976,11 @@ class PositionalTestCase(BaseTestCase):
self.useFixture(fixtures.MonkeyPatch('sys.stdout', moves.StringIO()))
self.assertRaises(SystemExit, self.conf, ['--help'])
self.assertIn(' [foo-bar]\n', sys.stdout.getvalue())
self.assertIn(' [foo_bar]\n', sys.stdout.getvalue())
self.conf(['baz'])
self.assertTrue(hasattr(self.conf, 'foo_bar'))
# FIXME(dolphm): Due to bug 1676989, this argument cannot be retrieved
# by oslo_config.cfg. Instead, the following commented-out code should
# work:
# self.assertEqual('baz', self.conf.foo_bar)
self.assertIsNone(self.conf.foo_bar)
self.assertEqual('baz', self.conf.foo_bar)
def test_optional_positional_hyphenated_opt_undefined(self):
self.conf.register_cli_opt(
@ -992,7 +988,7 @@ class PositionalTestCase(BaseTestCase):
self.useFixture(fixtures.MonkeyPatch('sys.stdout', moves.StringIO()))
self.assertRaises(SystemExit, self.conf, ['--help'])
self.assertIn(' [foo-bar]\n', sys.stdout.getvalue())
self.assertIn(' [foo_bar]\n', sys.stdout.getvalue())
self.conf([])
self.assertTrue(hasattr(self.conf, 'foo_bar'))
@ -1004,15 +1000,11 @@ class PositionalTestCase(BaseTestCase):
self.useFixture(fixtures.MonkeyPatch('sys.stdout', moves.StringIO()))
self.assertRaises(SystemExit, self.conf, ['--help'])
self.assertIn(' foo-bar\n', sys.stdout.getvalue())
self.assertIn(' foo_bar\n', sys.stdout.getvalue())
# FIXME(dolphm): Due to bug 1676989, this mistakenly raises an
# exception, even though the option is clearly defined. Instead, the
# following commented out lines should work:
# self.conf(['baz'])
# self.assertTrue(hasattr(self.conf, 'foo_bar'))
# self.assertEqual('baz', self.conf.foo_bar)
self.assertRaises(cfg.RequiredOptError, self.conf, ['baz'])
self.conf(['baz'])
self.assertTrue(hasattr(self.conf, 'foo_bar'))
self.assertEqual('baz', self.conf.foo_bar)
def test_required_positional_hyphenated_opt_undefined(self):
self.conf.register_cli_opt(
@ -1020,7 +1012,7 @@ class PositionalTestCase(BaseTestCase):
self.useFixture(fixtures.MonkeyPatch('sys.stdout', moves.StringIO()))
self.assertRaises(SystemExit, self.conf, ['--help'])
self.assertIn(' foo-bar\n', sys.stdout.getvalue())
self.assertIn(' foo_bar\n', sys.stdout.getvalue())
self.assertRaises(SystemExit, self.conf, [])