cli: add support for deprecated tower-cli aliases in py2

This commit is contained in:
Ryan Petrello 2019-09-03 21:08:28 -04:00
parent ba8b876dd3
commit b888c4b75a
No known key found for this signature in database
GPG Key ID: F2AA5F2122351777
3 changed files with 30 additions and 4 deletions

View File

@ -170,9 +170,9 @@ class CLI(object):
if formatted:
print(utils.to_str(formatted), file=self.stdout)
else:
self.parser.print_help()
if six.PY2 and not self.help:
if six.PY3:
self.parser.print_help()
elif six.PY2 and not self.help:
# Unfortunately, argparse behavior between py2 and py3
# changed in a notable way when required subparsers
# have invalid (or missing) arguments specified

View File

@ -137,9 +137,10 @@ def parse_resource(client, skip_deprecated=False):
# argparse aliases are *only* supported in Python3 (not 2.7)
kwargs = {}
if not skip_deprecated and PY3:
if not skip_deprecated:
if k in DEPRECATED_RESOURCES:
kwargs['aliases'] = [DEPRECATED_RESOURCES[k]]
client.subparsers[k] = subparsers.add_parser(
k, help='', **kwargs
)

View File

@ -5,6 +5,8 @@ import os
import sys
import threading
import six
_color = threading.local()
_color.enabled = True
@ -36,6 +38,29 @@ class CustomRegistryMeta(type):
class HelpfulArgumentParser(ArgumentParser):
def __init__(self, *args, **kwargs):
super(HelpfulArgumentParser, self).__init__(*args, **kwargs)
if six.PY2:
# backport parser aliases support to py2
# see: https://github.com/python/cpython/commit/fd311a712d5876c3a3efff265978452eea759f85
SubParsersAction = self._registries['action']['parsers']
class _SubParsersAction(SubParsersAction):
def add_parser(self, name, **kwargs):
aliases = kwargs.pop('aliases', [])
parser = super(_SubParsersAction, self).add_parser(name, **kwargs)
if aliases:
self._choices_actions[-1].metavar = ' '.join([
name,
'({})'.format(', '.join(aliases))
])
for alias in aliases:
self._name_parser_map[alias] = parser
return parser
self._registries['action']['parsers'] = _SubParsersAction
def error(self, message): # pragma: nocover
"""Prints a usage message incorporating the message to stderr and
exits.