Skip to content

Commit a4db7ce

Browse files
committed
Add with_annotated to top-level cmd2 namespace
Also: - Fix minor edge case silent failure bug - Add description of this new feature to CHANGELOG under an "Experimental Features" section for the 4.0.0 release
1 parent 5ffbdac commit a4db7ce

5 files changed

Lines changed: 17 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,12 @@ prompt is displayed.
171171
is enabled.
172172
- `alias` and `macro` subcommands for `create` and `delete` now output their non-essential
173173
success case output using `pfeedback`
174+
- Experimental features
175+
- New `@with_annotated` decorator, a type-hint-driven alternative to `@with_argparse` that
176+
builds the parser automatically from a command's signature (positional/option inference,
177+
enum/literal/path/collection handling, subcommands, groups, mutex). See the
178+
[annotated_example.py](https://github.com/python-cmd2/cmd2/blob/main/examples/annotated_example.py)
179+
example for demonstration of usage.
174180

175181
## 3.5.1 (April 24, 2026)
176182

cmd2/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
rich_utils,
1212
string_utils,
1313
)
14+
from .annotated import with_annotated
1415
from .argparse_completer import set_default_ap_completer_type
1516
from .argparse_utils import (
1617
Cmd2ArgumentParser,
@@ -88,6 +89,7 @@
8889
"CompletionItem",
8990
"Completions",
9091
# Decorators
92+
"with_annotated",
9193
"with_argument_list",
9294
"with_argparser",
9395
"with_category",

cmd2/annotated.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1925,7 +1925,7 @@ def _invoke_command_func(
19251925
"""Call *func* from parsed kwargs, unpacking ``*args`` positionally when present."""
19261926
if var_positional_name is None:
19271927
return func(self_arg, **func_kwargs)
1928-
positional = [func_kwargs.pop(name) for name in leading_names if name in func_kwargs]
1928+
positional = [func_kwargs.pop(name) for name in leading_names]
19291929
var_values = func_kwargs.pop(var_positional_name, None) or ()
19301930
return func(self_arg, *positional, *var_values, **func_kwargs)
19311931

docs/features/annotated.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111
For production code that needs stable behavior, use
1212
[@with_argparser](argument_processing.md#with_argparser-decorator) instead.
1313

14-
The [@with_annotated][cmd2.annotated.with_annotated] decorator builds an argparse parser
15-
automatically from the decorated function's type annotations. No manual `add_argument()` calls are
16-
required, and the command body receives typed keyword arguments directly instead of an
17-
`argparse.Namespace`.
14+
The [@with_annotated][cmd2.with_annotated] decorator builds an argparse parser automatically from
15+
the decorated function's type annotations. No manual `add_argument()` calls are required, and the
16+
command body receives typed keyword arguments directly instead of an `argparse.Namespace`.
1817

1918
The two decorators are interchangeable -- here is the same command written both ways:
2019

docs/features/argument_processing.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ following for you:
1919
These features are provided by two decorators:
2020

2121
- [@with_argparser][cmd2.with_argparser] -- build parsers manually with `add_argument()` calls
22-
- [@with_annotated][cmd2.annotated.with_annotated] -- build parsers automatically from type hints
22+
- [@with_annotated][cmd2.with_annotated] -- build parsers automatically from type hints
2323

2424
See the
2525
[argparse_completion](https://github.com/python-cmd2/cmd2/blob/main/examples/argparse_completion.py)
@@ -61,10 +61,10 @@ stores internally. A consequence is that parsers don't need to be unique across
6161

6262
The `@with_annotated` decorator is **experimental** and its API may change in future releases.
6363

64-
The [@with_annotated][cmd2.annotated.with_annotated] decorator builds an argparse parser
65-
automatically from the decorated function's type annotations -- no manual `add_argument()` calls
66-
required. See [Annotated Argument Processing](annotated.md) for the full reference, including type
67-
mapping, metadata classes, subcommands, and stability caveats.
64+
The [@with_annotated][cmd2.with_annotated] decorator builds an argparse parser automatically from
65+
the decorated function's type annotations -- no manual `add_argument()` calls required. See
66+
[Annotated Argument Processing](annotated.md) for the full reference, including type mapping,
67+
metadata classes, subcommands, and stability caveats.
6868

6969
## Argument Parsing
7070

0 commit comments

Comments
 (0)