Skip to content
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
29 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
b0abc4b
Fix potential infinite recursion when a nested base command is execut…
tleonhardt Jun 1, 2026
08f027e
Update details on experimental type annotation feature in CHANGELOG
tleonhardt Jun 2, 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
2,471 changes: 2,471 additions & 0 deletions cmd2/annotated.py

Large diffs are not rendered by default.

440 changes: 440 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.annotated.with_annotated] -- build parsers automatically from type hints
Comment thread
tleonhardt marked this conversation as resolved.
Outdated

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.annotated.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