mirror of
https://github.com/ansible/awx.git
synced 2026-05-11 03:17:38 -02:30
Merge pull request #4651 from ryanpetrello/py2-argparse-alias
cli: add support for deprecated tower-cli aliases in py2 Reviewed-by: https://github.com/apps/softwarefactory-project-zuul
This commit is contained in:
@@ -170,9 +170,9 @@ class CLI(object):
|
|||||||
if formatted:
|
if formatted:
|
||||||
print(utils.to_str(formatted), file=self.stdout)
|
print(utils.to_str(formatted), file=self.stdout)
|
||||||
else:
|
else:
|
||||||
self.parser.print_help()
|
if six.PY3:
|
||||||
|
self.parser.print_help()
|
||||||
if six.PY2 and not self.help:
|
elif six.PY2 and not self.help:
|
||||||
# Unfortunately, argparse behavior between py2 and py3
|
# Unfortunately, argparse behavior between py2 and py3
|
||||||
# changed in a notable way when required subparsers
|
# changed in a notable way when required subparsers
|
||||||
# have invalid (or missing) arguments specified
|
# have invalid (or missing) arguments specified
|
||||||
|
|||||||
@@ -137,9 +137,10 @@ def parse_resource(client, skip_deprecated=False):
|
|||||||
|
|
||||||
# argparse aliases are *only* supported in Python3 (not 2.7)
|
# argparse aliases are *only* supported in Python3 (not 2.7)
|
||||||
kwargs = {}
|
kwargs = {}
|
||||||
if not skip_deprecated and PY3:
|
if not skip_deprecated:
|
||||||
if k in DEPRECATED_RESOURCES:
|
if k in DEPRECATED_RESOURCES:
|
||||||
kwargs['aliases'] = [DEPRECATED_RESOURCES[k]]
|
kwargs['aliases'] = [DEPRECATED_RESOURCES[k]]
|
||||||
|
|
||||||
client.subparsers[k] = subparsers.add_parser(
|
client.subparsers[k] = subparsers.add_parser(
|
||||||
k, help='', **kwargs
|
k, help='', **kwargs
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ import os
|
|||||||
import sys
|
import sys
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
|
import six
|
||||||
|
|
||||||
_color = threading.local()
|
_color = threading.local()
|
||||||
_color.enabled = True
|
_color.enabled = True
|
||||||
|
|
||||||
@@ -36,6 +38,29 @@ class CustomRegistryMeta(type):
|
|||||||
|
|
||||||
class HelpfulArgumentParser(ArgumentParser):
|
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
|
def error(self, message): # pragma: nocover
|
||||||
"""Prints a usage message incorporating the message to stderr and
|
"""Prints a usage message incorporating the message to stderr and
|
||||||
exits.
|
exits.
|
||||||
|
|||||||
Reference in New Issue
Block a user