From 0f550d7c79249900c7f23fdd53723a1f2443e01d Mon Sep 17 00:00:00 2001 From: Eric Brown Date: Thu, 5 Mar 2015 02:05:23 -0800 Subject: [PATCH] Generate help text indicating possible values Since the introduction of choices, the help text does not indicate possible values of these choices. This patch adds to the help a list of all values that are accepted. Change-Id: I68edda59ad7a690918f8e45abe2b3b56cd01ada7 Closes-Bug: 1427913 --- oslo_config/generator.py | 4 ++++ oslo_config/tests/test_generator.py | 16 ++++++++++++++++ tests/test_generator.py | 16 ++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/oslo_config/generator.py b/oslo_config/generator.py index 17977c21..0a4f7ce5 100644 --- a/oslo_config/generator.py +++ b/oslo_config/generator.py @@ -193,6 +193,10 @@ def format(self, opt): help_text = u'(%s)' % opt_type lines = self._format_help(help_text) + if getattr(opt.type, 'choices', None): + choices_text = ', '.join(opt.type.choices) + lines.append('# Allowed values: %s\n' % choices_text) + for d in opt.deprecated_opts: lines.append('# Deprecated group/name - [%s]/%s\n' % (d.group or 'DEFAULT', d.name or opt.dest)) diff --git a/oslo_config/tests/test_generator.py b/oslo_config/tests/test_generator.py index 5922fb41..afd500f3 100644 --- a/oslo_config/tests/test_generator.py +++ b/oslo_config/tests/test_generator.py @@ -48,6 +48,10 @@ class GeneratorTestCase(base.BaseTestCase): 'cupidatat non proident, sunt in culpa ' 'qui officia deserunt mollit anim id est ' 'laborum.'), + 'choices_opt': cfg.StrOpt('choices_opt', + default='a', + choices=('a', 'b', 'c'), + help='a string with choices'), 'deprecated_opt': cfg.StrOpt('bar', deprecated_name='foobar', help='deprecated'), @@ -280,6 +284,18 @@ class GeneratorTestCase(base.BaseTestCase): '(string value)' ''' #long_help = +''')), + ('choices_opt', + dict(opts=[('test', [(None, [opts['choices_opt']])])], + expected='''[DEFAULT] + +# +# From test +# + +# a string with choices (string value) +# Allowed values: a, b, c +#choices_opt = a ''')), ('deprecated', dict(opts=[('test', [('foo', [opts['deprecated_opt']])])], diff --git a/tests/test_generator.py b/tests/test_generator.py index 6717da08..41ddfec3 100644 --- a/tests/test_generator.py +++ b/tests/test_generator.py @@ -52,6 +52,10 @@ class GeneratorTestCase(base.BaseTestCase): 'cupidatat non proident, sunt in culpa ' 'qui officia deserunt mollit anim id est ' 'laborum.'), + 'choices_opt': cfg.StrOpt('choices_opt', + default='a', + choices=('a', 'b', 'c'), + help='a string with choices'), 'deprecated_opt': cfg.StrOpt('bar', deprecated_name='foobar', help='deprecated'), @@ -295,6 +299,18 @@ class GeneratorTestCase(base.BaseTestCase): '(string value)' ''' #long_help = +''')), + ('choices_opt', + dict(opts=[('test', [(None, [opts['choices_opt']])])], + expected='''[DEFAULT] + +# +# From test +# + +# a string with choices (string value) +# Allowed values: a, b, c +#choices_opt = a ''')), ('deprecated', dict(opts=[('test', [('foo', [opts['deprecated_opt']])])],