Skip to content

getting_started.py now demonstrates how to use common cmd2 deocrators#1705

Merged
tleonhardt merged 8 commits into
mainfrom
getting_started_with_decorators
Jul 6, 2026
Merged

getting_started.py now demonstrates how to use common cmd2 deocrators#1705
tleonhardt merged 8 commits into
mainfrom
getting_started_with_decorators

Conversation

@tleonhardt

@tleonhardt tleonhardt commented Jul 1, 2026

Copy link
Copy Markdown
Member

getting_started.py now demonstrates how to use both the @with_argparser and @with_annotated decorators for defining command arguments.

The intent is to help make the getting_started.py example something of a one-stop shop to quickly orient new cmd2 users to what we view as best practices. It was previously parsing raw Statements for arguments, which frankly I view as an antipattern.

Closes #1704

@tleonhardt tleonhardt self-assigned this Jul 1, 2026
@tleonhardt tleonhardt requested a review from kmvanbrunt as a code owner July 1, 2026 22:24
@codecov

codecov Bot commented Jul 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.59%. Comparing base (a442a0d) to head (b763f19).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1705      +/-   ##
==========================================
+ Coverage   99.57%   99.59%   +0.01%     
==========================================
  Files          23       23              
  Lines        5894     5894              
==========================================
+ Hits         5869     5870       +1     
+ Misses         25       24       -1     
Flag Coverage Δ
unittests 99.59% <ø> (+0.01%) ⬆️

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

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

@tleonhardt

Copy link
Copy Markdown
Member Author

@kmvanbrunt I would appreciate any constructive feedback on how to improve the @with_argparser example here to make it more educational for new users.

@KelvinChung2000 I would appreciate any constructive feedback on how to improve the @with_annotated example here to make it more educational for new users.

Comment thread examples/getting_started.py Outdated
@KelvinChung2000

Copy link
Copy Markdown
Contributor

I recommend demonstrating an example with a positional Path type.

@cmd2.with_annotated
def do_cat(
    self,
    path: pathlib.Path,
    numbered: Annotated[
        bool, Option("-n", "--number", help_text="prefix each line with its number")
    ] = False,
) -> None:
    """Print a file's contents. `path` tab-completes filesystem paths automatically.

    Try:
        cat <TAB>              # path completes files/dirs -- no completer wired
        cat notes.txt
        cat notes.txt -n       # -n / --number, declared via Option metadata
        cat notes.txt --no-number
    """
    for index, line in enumerate(path.read_text().splitlines(), start=1):
        self.poutput(f"{index}: {line}" if numbered else line)

This demonstrates auto-completer inference and non-built-in type support while being simple and explicit. But this does not fit a "intro" command very well.

@tleonhardt

Copy link
Copy Markdown
Member Author

@KelvinChung2000 I took your advice and added a modified version of your cat command example for demonstrating use of the @with_annotated decorator.

I also reverted the do_intro command logic to its previous state.

@tleonhardt tleonhardt merged commit d35f7d7 into main Jul 6, 2026
29 checks passed
@tleonhardt tleonhardt deleted the getting_started_with_decorators branch July 6, 2026 19:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor getting_started.py example to demonstrate best-practices for defining command arguments

3 participants