From a1f86ec2212a1c31dafd5a10efd377846f4c685f Mon Sep 17 00:00:00 2001 From: joncrall Date: Mon, 24 Apr 2023 10:57:04 -0400 Subject: [PATCH] Cache aliases function / stub aliases property --- jsonargparse/actions.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/jsonargparse/actions.py b/jsonargparse/actions.py index 1b98374e..f51c1a30 100644 --- a/jsonargparse/actions.py +++ b/jsonargparse/actions.py @@ -39,6 +39,19 @@ class Action(LoggerProperty, ArgparseAction): """Base for jsonargparse Action classes.""" + @property + def aliases(self): + return _action_aliases(self) + + +def _action_aliases(self): + if not hasattr(self, '_aliases'): + options = {optstr.lstrip('-').replace('-', '_') + for optstr in self.option_strings} + options = {opt for opt in options if len(opt) > 1} + self._aliases = {self.dest} | options + return self._aliases + def _is_branch_key(parser, key: str) -> bool: root_key = split_key_root(key)[0] @@ -70,10 +83,10 @@ def _find_action_and_subcommand( actions = [a for a in actions if not isinstance(a, exclude)] fallback_action = None - ALLOW_ALIAS = True - for action in actions: - if action.dest == dest or (ALLOW_ALIAS and dest in get_alias_dest(action)): + # _StoreAction seems to break the property + # if dest in action.aliases: + if dest in _action_aliases(action): if isinstance(action, _ActionConfigLoad): fallback_action = action else: @@ -752,8 +765,5 @@ def handle_subcommands( def get_alias_dest(action): - def option_string_to_var(optstr): - " normalize a cli key as a variable " - return optstr.lstrip('-').replace('-', '_') - - return [option_string_to_var(optstr) for optstr in action.option_strings] + return [optstr.lstrip('-').replace('-', '_') + for optstr in action.option_strings]