[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}'", "ldap_attribute_already_exists" : "Attribute '{attribute}' already exists with value '{value}'",
"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", "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", "argument_required" : "Argument '{argument}' is required",
"invalid_argument": "Invalid argument '{argument}': {error}", "invalid_argument": "Invalid argument '{argument}': {error}",
"pattern_not_match": "Does not match pattern", "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, It also provides the following additional properties for parsers,
e.g. using `subparsers.add_parser`: e.g. using `subparsers.add_parser`:
- deprecated -- Wether the command is deprecated
- deprecated_alias -- A list of deprecated command alias names - deprecated_alias -- A list of deprecated command alias names
""" """
@ -457,7 +458,14 @@ class _ExtendedSubParsersAction(argparse._SubParsersAction):
self._deprecated_command_map = {} self._deprecated_command_map = {}
def add_parser(self, name, **kwargs): def add_parser(self, name, **kwargs):
deprecated = kwargs.pop('deprecated', False)
deprecated_alias = kwargs.pop('deprecated_alias', []) 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( parser = super(_ExtendedSubParsersAction, self).add_parser(
name, **kwargs) name, **kwargs)
@ -477,10 +485,15 @@ class _ExtendedSubParsersAction(argparse._SubParsersAction):
except KeyError: except KeyError:
pass pass
else: else:
# Warn the user and set the proper parser_name # Warn the user about deprecated command
logger.warning(m18n.g('deprecated_command', prog=parser.prog, if correct_name is None:
old=parser_name, new=correct_name)) logger.warning(m18n.g('deprecated_command', prog=parser.prog,
values[0] = correct_name 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__( return super(_ExtendedSubParsersAction, self).__call__(
parser, namespace, values, option_string) parser, namespace, values, option_string)

View file

@ -269,12 +269,13 @@ class ActionsMapParser(BaseActionsMapParser):
'title': "actions", 'required': True '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): deprecated_alias=[], **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 -- Wether the action is deprecated
- deprecated_alias -- A list of deprecated action alias names - deprecated_alias -- A list of deprecated action alias names
Returns: Returns:
@ -282,6 +283,7 @@ class ActionsMapParser(BaseActionsMapParser):
""" """
return self._subparsers.add_parser(name, help=action_help, return self._subparsers.add_parser(name, help=action_help,
deprecated=deprecated,
deprecated_alias=deprecated_alias) deprecated_alias=deprecated_alias)
def parse_args(self, args, **kwargs): def parse_args(self, args, **kwargs):