Skip to content

Commit a1f86ec

Browse files
committed
Cache aliases function / stub aliases property
1 parent af8b185 commit a1f86ec

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

jsonargparse/actions.py

+18-8
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@
3939
class Action(LoggerProperty, ArgparseAction):
4040
"""Base for jsonargparse Action classes."""
4141

42+
@property
43+
def aliases(self):
44+
return _action_aliases(self)
45+
46+
47+
def _action_aliases(self):
48+
if not hasattr(self, '_aliases'):
49+
options = {optstr.lstrip('-').replace('-', '_')
50+
for optstr in self.option_strings}
51+
options = {opt for opt in options if len(opt) > 1}
52+
self._aliases = {self.dest} | options
53+
return self._aliases
54+
4255

4356
def _is_branch_key(parser, key: str) -> bool:
4457
root_key = split_key_root(key)[0]
@@ -70,10 +83,10 @@ def _find_action_and_subcommand(
7083
actions = [a for a in actions if not isinstance(a, exclude)]
7184
fallback_action = None
7285

73-
ALLOW_ALIAS = True
74-
7586
for action in actions:
76-
if action.dest == dest or (ALLOW_ALIAS and dest in get_alias_dest(action)):
87+
# _StoreAction seems to break the property
88+
# if dest in action.aliases:
89+
if dest in _action_aliases(action):
7790
if isinstance(action, _ActionConfigLoad):
7891
fallback_action = action
7992
else:
@@ -752,8 +765,5 @@ def handle_subcommands(
752765

753766

754767
def get_alias_dest(action):
755-
def option_string_to_var(optstr):
756-
" normalize a cli key as a variable "
757-
return optstr.lstrip('-').replace('-', '_')
758-
759-
return [option_string_to_var(optstr) for optstr in action.option_strings]
768+
return [optstr.lstrip('-').replace('-', '_')
769+
for optstr in action.option_strings]

0 commit comments

Comments
 (0)