Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
7e41c10
feat: add annotated argparse building
KelvinChung2000 May 6, 2026
f140150
chore: fix rebase
KelvinChung2000 May 15, 2026
076defc
chore: clean up test
KelvinChung2000 May 15, 2026
d4756eb
chore: more clean up
KelvinChung2000 May 15, 2026
bacaab3
chore: move documentation
KelvinChung2000 May 15, 2026
70dfcf1
fix: address PR review comments on annotated argparse
KelvinChung2000 May 19, 2026
98f33ce
feat: add parser customization to annotated argparse
KelvinChung2000 May 19, 2026
c129613
docs: demonstrate formatter_class and parser_class in example
KelvinChung2000 May 19, 2026
e30763e
refactor: require Group for groups/mutually_exclusive_groups
KelvinChung2000 May 19, 2026
d913f06
docs: demonstrate mutually_exclusive_groups in example
KelvinChung2000 May 19, 2026
b6a9832
chore: format
KelvinChung2000 May 19, 2026
bc1157d
Merge branch 'main' into feat/annotated-argparse
tleonhardt May 20, 2026
f05bc31
Adding another argument to an example method to help demonstrate a re…
tleonhardt May 20, 2026
3232e13
Fix subtle edge case bug where booleans were accepted as integer lite…
tleonhardt May 20, 2026
da26c3a
feat: cover 0-or-1 case
KelvinChung2000 May 20, 2026
5727076
chore: extend verification
KelvinChung2000 May 20, 2026
e6790c3
chore: minor clean up
KelvinChung2000 May 20, 2026
4fc726c
chore: a few extra test
KelvinChung2000 May 20, 2026
c0bb4bb
chore: remove collection type doc and sort
KelvinChung2000 May 21, 2026
3746e62
chore: address comment and update test
KelvinChung2000 May 21, 2026
f0ad6d7
chore: update doc
KelvinChung2000 May 22, 2026
faa2420
feat: better *args and **kwargs handling, with extend test
KelvinChung2000 May 22, 2026
c087b8b
chore: more *args edge case handling
KelvinChung2000 May 22, 2026
06cc398
Merge branch 'main' into feat/annotated-argparse
tleonhardt May 25, 2026
5ffbdac
chore: large scale refactor
KelvinChung2000 May 29, 2026
a4db7ce
Add with_annotated to top-level cmd2 namespace
tleonhardt May 30, 2026
a6de465
chore: doc clean up and some more edge cases
KelvinChung2000 May 30, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ prompt is displayed.
is enabled.
- `alias` and `macro` subcommands for `create` and `delete` now output their non-essential
success case output using `pfeedback`
- Experimental features
- New `@with_annotated` decorator, a type-hint-driven alternative to `@with_argparse` that
builds the parser automatically from a command's signature (positional/option inference,
enum/literal/path/collection handling, subcommands, groups, mutex). See the
[annotated_example.py](https://github.com/python-cmd2/cmd2/blob/main/examples/annotated_example.py)
example for demonstration of usage.

## 3.5.1 (April 24, 2026)

Expand Down
2 changes: 2 additions & 0 deletions cmd2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
rich_utils,
string_utils,
)
from .annotated import with_annotated
from .argparse_completer import set_default_ap_completer_type
from .argparse_utils import (
Cmd2ArgumentParser,
Expand Down Expand Up @@ -88,6 +89,7 @@
"CompletionItem",
"Completions",
# Decorators
"with_annotated",
"with_argument_list",
"with_argparser",
"with_category",
Expand Down
2,319 changes: 2,319 additions & 0 deletions cmd2/annotated.py

Large diffs are not rendered by default.

445 changes: 445 additions & 0 deletions docs/features/annotated.md

Large diffs are not rendered by default.

24 changes: 19 additions & 5 deletions docs/features/argument_processing.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,21 @@ following for you:
1. Adds the usage message from the argument parser to your command's help.
1. Checks if the `-h/--help` option is present, and if so, displays the help message for the command

These features are all provided by the [@with_argparser][cmd2.with_argparser] decorator which is
imported from `cmd2`.
These features are provided by two decorators:

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

See the
[argparse_example](https://github.com/python-cmd2/cmd2/blob/main/examples/argparse_example.py)
example to learn more about how to use the various `cmd2` argument processing decorators in your
`cmd2` applications.
[argparse_completion](https://github.com/python-cmd2/cmd2/blob/main/examples/argparse_completion.py)
and [annotated_example](https://github.com/python-cmd2/cmd2/blob/main/examples/annotated_example.py)
examples to compare the two styles side by side.

`cmd2` provides the following [decorators](../api/decorators.md) for assisting with parsing
arguments passed to commands:

- [cmd2.decorators.with_argparser][]
- [cmd2.annotated.with_annotated][]
- [cmd2.decorators.with_argument_list][]

All of these decorators accept an optional **preserve_quotes** argument which defaults to `False`.
Expand All @@ -52,6 +55,17 @@ stores internally. A consequence is that parsers don't need to be unique across
to dynamically modify this parser at a later time, you need to retrieve this deep copy. This can
be done using `self.command_parsers.get(self.do_commandname)`.

## with_annotated decorator

!!! warning "Experimental"

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

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

## Argument Parsing

For each command in the `cmd2.Cmd` subclass which requires argument parsing, create an instance of
Expand Down
Loading
Loading