diff --git a/locales/en.json b/locales/en.json index d737f944..51efcd82 100644 --- a/locales/en.json +++ b/locales/en.json @@ -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", diff --git a/moulinette/interfaces/__init__.py b/moulinette/interfaces/__init__.py index 36c07153..bff0b5c6 100644 --- a/moulinette/interfaces/__init__.py +++ b/moulinette/interfaces/__init__.py @@ -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) diff --git a/moulinette/interfaces/cli.py b/moulinette/interfaces/cli.py index f394271e..19662fb2 100644 --- a/moulinette/interfaces/cli.py +++ b/moulinette/interfaces/cli.py @@ -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):