Skip to content

Commit 306d464

Browse files
committed
POC to allow alias
1 parent 42e9c0a commit 306d464

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

jsonargparse/actions.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,11 @@ def _find_action_and_subcommand(
7070
if exclude is not None:
7171
actions = [a for a in actions if not isinstance(a, exclude)]
7272
fallback_action = None
73+
74+
ALLOW_ALIAS = True
75+
7376
for action in actions:
74-
if action.dest == dest:
77+
if action.dest == dest or (ALLOW_ALIAS and dest in get_alias_dest(action)):
7578
if isinstance(action, _ActionConfigLoad):
7679
fallback_action = action
7780
else:
@@ -747,3 +750,11 @@ def handle_subcommands(
747750
# Handle inner subcommands
748751
if subparser._subparsers is not None:
749752
_ActionSubCommands.handle_subcommands(subparser, cfg, env, defaults, key+'.', fail_no_subcommand=fail_no_subcommand)
753+
754+
755+
def get_alias_dest(action):
756+
def option_string_to_var(optstr):
757+
" normalize a cli key as a variable "
758+
return optstr.lstrip('-').replace('-', '_')
759+
760+
return [option_string_to_var(optstr) for optstr in action.option_strings]

jsonargparse/core.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1260,7 +1260,25 @@ def _apply_actions(
12601260
continue
12611261

12621262
action_dest = action.dest if subcommand is None else subcommand+'.'+action.dest
1263-
value = cfg[action_dest]
1263+
ALLOW_ALIAS = 1
1264+
try:
1265+
value = cfg[action_dest]
1266+
except KeyError:
1267+
from jsonargparse.actions import get_alias_dest
1268+
if ALLOW_ALIAS:
1269+
# If the main key isn't in the config, check if it exists
1270+
# under an alias.
1271+
found = None
1272+
for alias in get_alias_dest(action):
1273+
if alias in cfg:
1274+
value = cfg[alias]
1275+
found = True
1276+
break
1277+
if not found:
1278+
raise
1279+
else:
1280+
raise
1281+
...
12641282
if skip_fn and skip_fn(value):
12651283
continue
12661284
with parser_context(parent_parser=self, lenient_check=True):

0 commit comments

Comments
 (0)