[enh] Allow to define deprecated action

This commit is contained in:
Jérôme Lebleu 2016-04-16 21:26:29 +02:00
parent 802c39f117
commit c92900edd8
3 changed files with 22 additions and 6 deletions

View file

@ -20,7 +20,8 @@
"ldap_attribute_already_exists" : "Attribute '{attribute}' already exists with value '{value}'",
"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",
"deprecated_command" : "'{prog} {command}' is deprecated and will be removed in the future",
"deprecated_command_alias" : "'{prog} {old}' is deprecated and will be removed in the future, use '{prog} {new}' instead",
"argument_required" : "Argument '{argument}' is required",
"invalid_argument": "Invalid argument '{argument}': {error}",
"pattern_not_match": "Does not match pattern",

View file

@ -446,6 +446,7 @@ class _ExtendedSubParsersAction(argparse._SubParsersAction):
It also provides the following additional properties for parsers,
e.g. using `subparsers.add_parser`:
- deprecated -- Wether the command is deprecated
- deprecated_alias -- A list of deprecated command alias names
"""
@ -457,7 +458,14 @@ class _ExtendedSubParsersAction(argparse._SubParsersAction):
self._deprecated_command_map = {}
def add_parser(self, name, **kwargs):
deprecated = kwargs.pop('deprecated', False)
deprecated_alias = kwargs.pop('deprecated_alias', [])
if deprecated:
self._deprecated_command_map[name] = None
if 'help' in kwargs:
del kwargs['help']
parser = super(_ExtendedSubParsersAction, self).add_parser(
name, **kwargs)
@ -477,10 +485,15 @@ class _ExtendedSubParsersAction(argparse._SubParsersAction):
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
# Warn the user about deprecated command
if correct_name is None:
logger.warning(m18n.g('deprecated_command', prog=parser.prog,
command=parser_name))
else:
logger.warning(m18n.g('deprecated_command_alias',
old=parser_name, new=correct_name,
prog=parser.prog))
values[0] = correct_name
return super(_ExtendedSubParsersAction, self).__call__(
parser, namespace, values, option_string)

View file

@ -269,12 +269,13 @@ class ActionsMapParser(BaseActionsMapParser):
'title': "actions", 'required': True
})
def add_action_parser(self, name, tid, action_help=None,
def add_action_parser(self, name, tid, action_help=None, deprecated=False,
deprecated_alias=[], **kwargs):
"""Add a parser for an action
Keyword arguments:
- action_help -- A brief description for the action
- deprecated -- Wether the action is deprecated
- deprecated_alias -- A list of deprecated action alias names
Returns:
@ -282,6 +283,7 @@ class ActionsMapParser(BaseActionsMapParser):
"""
return self._subparsers.add_parser(name, help=action_help,
deprecated=deprecated,
deprecated_alias=deprecated_alias)
def parse_args(self, args, **kwargs):