Skip to content

Commit 9366754

Browse files
fix: improve argument parsing for direct and nested CLI invocations
1 parent 0611eb8 commit 9366754

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

lib/dt_shell/commands/commands.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,11 @@ def _resolve_parsed(
8787
case the parent's values are overlaid on top of this command's
8888
defaults so that every argument always has its declared default value.
8989
90-
:func:`~argparse.ArgumentParser.parse_known_args` is used throughout
91-
so that unknown arguments are silently forwarded in both code paths,
92-
matching the behaviour expected by commands that call nested commands
93-
with an extended argument set.
90+
:func:`~argparse.ArgumentParser.parse_known_args` is used for direct
91+
CLI invocations so that unknown arguments are silently forwarded.
92+
For nested invocations, defaults are reconstructed from the parser
93+
without re-validating required arguments, then overlaid with the
94+
supplied namespace.
9495
9596
Args:
9697
args: Raw argument list forwarded from ``command(shell, args, **kwargs)``.
@@ -109,7 +110,15 @@ def _resolve_parsed(
109110
if pre_parsed is None:
110111
resolved, _ = _parser.parse_known_args(args=args)
111112
else:
112-
defaults, _ = _parser.parse_known_args(args=[])
113+
defaults = argparse.Namespace()
114+
for action in _parser._actions:
115+
dest = action.dest
116+
if dest == argparse.SUPPRESS:
117+
continue
118+
default = _parser.get_default(dest)
119+
if default == argparse.SUPPRESS:
120+
continue
121+
setattr(defaults, dest, default)
113122
defaults.__dict__.update(pre_parsed.__dict__)
114123
resolved = defaults
115124
return resolved

0 commit comments

Comments
 (0)