Skip to content

feat: Experiemtal annotated argparse#1666

Open
KelvinChung2000 wants to merge 5 commits into
mainfrom
feat/annotated-argparse
Open

feat: Experiemtal annotated argparse#1666
KelvinChung2000 wants to merge 5 commits into
mainfrom
feat/annotated-argparse

Conversation

@KelvinChung2000
Copy link
Copy Markdown

Adds @with_annotated, a type-hint-driven alternative to @with_argparser that builds the parser automatically from a command's signature (positional/option inference, enum/literal/path/collection handling, subcommands, groups, mutex). Marked experimental.

  • New module cmd2/annotated.py plus Argument / Option metadata classes exported from cmd2
  • Underscored param names auto-dasherize in generated flags (dry_run--dry-run); opt out via Option("--my_flag")
  • Dedicated docs page docs/features/annotated.md with an experimental admonition; argument_processing.md keeps a short pointer
  • Example app examples/annotated_example.py and test suite in tests/test_annotated.py

Adds @with_annotated decorator that builds argparse parsers from type-annotated
function signatures. Supports Annotated[T, Argument(...)] / Annotated[T, Option(...)]
metadata, automatic positional/option detection, optional unwrapping, collections,
enums, literals, Path completion, subcommands via subcommand_to=, base_command=True
with cmd2_handler dispatch, and argument/mutually-exclusive groups.

- New module cmd2/annotated.py with Argument, Option, with_annotated, and
  build_parser_from_function helpers
- Comprehensive test suite in tests/test_annotated.py
- Example in examples/annotated_example.py
- Docs updates in docs/features/argument_processing.md
@codecov
Copy link
Copy Markdown

codecov Bot commented May 15, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.58%. Comparing base (eded33c) to head (bacaab3).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1666      +/-   ##
==========================================
+ Coverage   99.55%   99.58%   +0.03%     
==========================================
  Files          22       23       +1     
  Lines        4920     5345     +425     
==========================================
+ Hits         4898     5323     +425     
  Misses         22       22              
Flag Coverage Δ
unittests 99.58% <100.00%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@tleonhardt
Copy link
Copy Markdown
Member

I plan to review this PR this weekend. @kmvanbrunt @bambu I'd very much appreciate your feedback as well.

@tleonhardt tleonhardt added this to the 4.0.0 milestone May 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants