Merge "Fix sorting of optional arguments"

This commit is contained in:
Jenkins 2015-06-27 03:19:05 +00:00 committed by Gerrit Code Review
commit 56073f066d
2 changed files with 19 additions and 2 deletions
oslo_config

@ -1718,10 +1718,13 @@ class _CachedArgumentParser(argparse.ArgumentParser):
# option and then sort the values slice.
for container, values in six.iteritems(self._args_cache):
index = 0
has_positional = False
for index, argument in enumerate(values):
if not argument['args'][0].startswith('-'):
has_positional = True
break
values[:index] = sorted(values[:index], key=lambda x: x['args'])
size = index if has_positional else len(values)
values[:size] = sorted(values[:size], key=lambda x: x['args'])
for argument in values:
try:
container.add_argument(*argument['args'],

@ -146,8 +146,8 @@ class HelpTestCase(BaseTestCase):
def test_print_sorted_help(self):
f = moves.StringIO()
self.conf.register_cli_opt(cfg.StrOpt('zba'))
self.conf.register_cli_opt(cfg.StrOpt('abc'))
self.conf.register_cli_opt(cfg.StrOpt('zba'))
self.conf.register_cli_opt(cfg.StrOpt('ghi'))
self.conf.register_cli_opt(cfg.StrOpt('deb'))
self.conf([])
@ -159,6 +159,20 @@ class HelpTestCase(BaseTestCase):
list = [abc, deb, ghi, zba]
self.assertEqual(sorted(list), list)
def test_print_sorted_help_with_positionals(self):
f = moves.StringIO()
self.conf.register_cli_opt(cfg.StrOpt('pst', positional=True))
self.conf.register_cli_opt(cfg.StrOpt('abc'))
self.conf.register_cli_opt(cfg.StrOpt('zba'))
self.conf.register_cli_opt(cfg.StrOpt('ghi'))
self.conf([])
self.conf.print_help(file=f)
zba = f.getvalue().find('--zba')
abc = f.getvalue().find('--abc')
ghi = f.getvalue().find('--ghi')
list = [abc, ghi, zba]
self.assertEqual(sorted(list), list)
class FindConfigFilesTestCase(BaseTestCase):