[microversions] fix help msg for versioned args

If command has arguments with one name, but for different versions(see an
example below), both arguments will be displayed. This patch fixes this
issue.

Example of arguments:

    @cliutils.args("name",
                   help="Name of a good action.",
                   start_version="2.1",
                   end_version="2.20")
    @cliutils.args("name",
                   help="Name of a very good action.",
                   start_version="2.21")
    def do_something_good(cs, args):
        pass

Example of helpoutput before patch:

    Positional arguments:
      <name>    Name of a good action. (Supported by API versions '2.0' - '2.20')
      <name>    Name of a very good action. (Supported by API versions '2.21' -
                '2.latest')

Change-Id: I59f155675e2aae642a5b90cb70008eb5647ffe79
This commit is contained in:
Andrey Kurilin 2016-02-24 17:11:41 +02:00
parent 0fcfa02841
commit ca5b06f6ae
3 changed files with 38 additions and 5 deletions

View File

@ -694,9 +694,8 @@ class OpenStackComputeShell(object):
kwargs["help"] = kwargs.get("help", "") + (msg % {
"start": start_version.get_string(),
"end": end_version.get_string()})
else:
if not version.matches(start_version, end_version):
continue
if not version.matches(start_version, end_version):
continue
kw = kwargs.copy()
kw.pop("start_version", None)
kw.pop("end_version", None)

View File

@ -42,3 +42,16 @@ def do_another_fake_action():
end_version='2.4')
def do_fake_action2():
return 3
@cliutils.arg(
'--foo',
help='first foo',
start_version='2.10',
end_version='2.20')
@cliutils.arg(
'--foo',
help='second foo',
start_version='2.21')
def do_fake_action3():
return 3

View File

@ -912,11 +912,32 @@ class TestLoadVersionedActions(utils.TestCase):
api_versions.APIVersion("2.4"), True)
mock_add_arg.assert_has_calls([
mock.call('-h', '--help', action='help', help='==SUPPRESS=='),
mock.call('--foo',
help=" (Supported by API versions '2.1' - '2.2')"),
mock.call('--bar',
help=" (Supported by API versions '2.3' - '2.4')")])
@mock.patch.object(novaclient.shell.NovaClientArgumentParser,
'add_argument')
def test_load_actions_with_versioned_args(self, mock_add_arg):
parser = novaclient.shell.NovaClientArgumentParser(add_help=False)
subparsers = parser.add_subparsers(metavar='<subcommand>')
shell = novaclient.shell.OpenStackComputeShell()
shell.subcommands = {}
shell._find_actions(subparsers, fake_actions_module,
api_versions.APIVersion("2.20"), False)
self.assertIn(mock.call('--foo', help="first foo"),
mock_add_arg.call_args_list)
self.assertNotIn(mock.call('--foo', help="second foo"),
mock_add_arg.call_args_list)
mock_add_arg.reset_mock()
shell._find_actions(subparsers, fake_actions_module,
api_versions.APIVersion("2.21"), False)
self.assertNotIn(mock.call('--foo', help="first foo"),
mock_add_arg.call_args_list)
self.assertIn(mock.call('--foo', help="second foo"),
mock_add_arg.call_args_list)
class ShellTestKeystoneV3(ShellTest):
def make_env(self, exclude=None, fake_env=FAKE_ENV):