[enh] Allow to define deprecated action names

This commit is contained in:
Jérôme Lebleu 2016-04-09 19:33:57 +02:00
parent c04f723ac2
commit 46e6e4dc7a
3 changed files with 51 additions and 5 deletions

View file

@ -20,6 +20,7 @@
"ldap_attribute_already_exists" : "Attribute already exists: '{:s}={:s}'", "ldap_attribute_already_exists" : "Attribute already exists: '{:s}={:s}'",
"invalid_usage" : "Invalid usage, pass --help to see help", "invalid_usage" : "Invalid usage, pass --help to see help",
"deprecated_command" : "'{prog} {old}' is deprecated and will be removed in the future, use '{prog} {new}' instead",
"argument_required" : "Argument {:s} is required", "argument_required" : "Argument {:s} is required",
"invalid_argument": "Invalid argument '{:s}': {:s}", "invalid_argument": "Invalid argument '{:s}': {:s}",
"pattern_not_match": "Does not match pattern", "pattern_not_match": "Does not match pattern",

View file

@ -437,11 +437,53 @@ class _CallbackAction(argparse.Action):
else: else:
setattr(namespace, self.dest, value) setattr(namespace, self.dest, value)
class _OptionalSubParsersAction(argparse._SubParsersAction): class _ExtendedSubParsersAction(argparse._SubParsersAction):
"""Subparsers with extended properties for argparse
It provides the following additional properties at initialization,
e.g. using `parser.add_subparsers`:
- required -- Either the subparser is required or not (default: False)
It also provides the following additional properties for parsers,
e.g. using `subparsers.add_parser`:
- deprecated -- A list of deprecated command names
"""
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
required = kwargs.pop('required', False) required = kwargs.pop('required', False)
super(_OptionalSubParsersAction, self).__init__(*args, **kwargs) super(_ExtendedSubParsersAction, self).__init__(*args, **kwargs)
self.required = required self.required = required
self._deprecated_command_map = {}
def add_parser(self, name, **kwargs):
deprecated = kwargs.pop('deprecated', [])
parser = super(_ExtendedSubParsersAction, self).add_parser(
name, **kwargs)
# Append each deprecated command name
for command in deprecated:
self._deprecated_command_map[command] = name
self._name_parser_map[command] = parser
return parser
def __call__(self, parser, namespace, values, option_string=None):
parser_name = values[0]
try:
# Check for deprecated command name
correct_name = self._deprecated_command_map[parser_name]
except KeyError:
pass
else:
# Warn the user and set the proper parser_name
logger.warning(m18n.g('deprecated_command', prog=parser.prog,
old=parser_name, new=correct_name))
values[0] = correct_name
return super(_ExtendedSubParsersAction, self).__call__(
parser, namespace, values, option_string)
class ExtendedArgumentParser(argparse.ArgumentParser): class ExtendedArgumentParser(argparse.ArgumentParser):
@ -450,7 +492,7 @@ class ExtendedArgumentParser(argparse.ArgumentParser):
# Register additional actions # Register additional actions
self.register('action', 'callback', _CallbackAction) self.register('action', 'callback', _CallbackAction)
self.register('action', 'parsers', _OptionalSubParsersAction) self.register('action', 'parsers', _ExtendedSubParsersAction)
def enqueue_callback(self, namespace, callback, values): def enqueue_callback(self, namespace, callback, values):
queue = self._get_callbacks_queue(namespace) queue = self._get_callbacks_queue(namespace)

View file

@ -264,17 +264,20 @@ class ActionsMapParser(BaseActionsMapParser):
'title': "actions", 'required': True 'title': "actions", 'required': True
}) })
def add_action_parser(self, name, tid, action_help=None, **kwargs): def add_action_parser(self, name, tid, action_help=None, deprecated=[],
**kwargs):
"""Add a parser for an action """Add a parser for an action
Keyword arguments: Keyword arguments:
- action_help -- A brief description for the action - action_help -- A brief description for the action
- deprecated -- A list of deprecated action names
Returns: Returns:
A new ExtendedArgumentParser object for the action A new ExtendedArgumentParser object for the action
""" """
return self._subparsers.add_parser(name, help=action_help) return self._subparsers.add_parser(name, help=action_help,
deprecated=deprecated)
def parse_args(self, args, **kwargs): def parse_args(self, args, **kwargs):
try: try: