Skip to content

Make a command's argparse spec public as Cmd.get_argparse_command_spec() - #1859

Merged
tleonhardt merged 4 commits into
mainfrom
public-argparse-command-spec
Dec 3, 2026
Merged

tleonhardt merged 4 commits into
mainfrom
public-argparse-command-spec

Conversation

@tleonhardt

@tleonhardt tleonhardt commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

Summary

@with_argparser and @with_annotated record an ArgparseCommandSpec on the command function:
the source of its parser, and whether argparse is given the command's arguments with their quotes
(preserve_quotes). The class is documented in cmd2.argparse_utils, but a command's spec could
only be read through a private attribute.

The MCP server example in #1855 (issue #1852) needs preserve_quotes to say how a subcommand is
typed: with preserve_quotes=True, argparse gets "list all" with its quotes, so a subcommand
whose name has quotes in it is reached that way, and one named list all cannot be reached at all.
A review of #1855 found that the example got this wrong for such commands. As with Stage 1 (#1856),
this makes the name public rather than having the example read a private one.

Changes

  • Cmd.get_argparse_command_spec(func) returns the spec, or None for a command which neither
    decorator made, such as one made with @with_argument_list. A disabled command's stand-in
    carries its spec, since disable_command() copies the command's attributes onto it.
  • ArgparseCommandSpec is frozen, so its fields cannot be reassigned. A parser given to
    @with_argparser is still the spec's parser_source itself, which each application copies when
    it builds the command's parser. The changelog entry and the getter's docstring say so, and that
    one application's parser is the one self.command_parsers.get(func) returns. The docstring says
    what parser_source is for each decorator: for @with_annotated, a factory which cmd2 made from
    the command's type hints, not the command's function.
  • Argparse completion uses the getter in place of the private attribute. CommandParsers.get()
    still reads the attribute: the argparse decorators pass it their own function, which is not
    bound, and the getter is given only bound methods.
  • docs/features/argument_processing.md shows how to ask whether a command keeps its quotes.
  • CHANGELOG.md: an entry under Enhancements in 4.5.0.

Testing

  • New tests in tests/test_argparse.py: a command made with @with_argparser with and without
    preserve_quotes, one made with @with_annotated, one from a CommandSet, a disabled one,
    commands with no spec (@with_argument_list, and a plain do_* method), and the two below.
  • make check, make test (3229 passed, 3 skipped) and make docs-test pass. No new line of
    cmd2/cmd2.py is left uncovered.

After review

A review at 9aac81b2 found two points, both real, both fixed in daa6c79f with a test which
failed first:

  • The getter returned the one spec which every application shares, and it could be changed: after
    spec.preserve_quotes = False through one application, another reported False while the
    command still kept its quotes, and completion followed the changed spec. The spec is now frozen.
    A test checks that it cannot be changed through one application to change what another reports.
  • CommandParsers.get() called the getter, and on a parser cache miss the decorators pass it their
    own function, which is not bound, though the getter promises a bound method. It also called an
    override of the getter during Cmd.__init__(). CommandParsers.get() reads the spec directly
    again. A test overrides the getter, runs a command for the first time and completes it, and
    checks that every call was given a bound method.

A second review, at daa6c79f, found one point: the changelog said that freezing the spec kept what
a decorator recorded from being changed, but the parser which @with_argparser was given is the
spec's parser_source itself, and a change to it reaches every application which has not built the
command's parser yet. 41695f0c narrows the changelog entry and the getter's docstring to say that
the spec's fields cannot be reassigned, and sends a reader who wants to change one application's
parser to self.command_parsers.get(func).

A third review, at 41695f0c, found one point in the getter's docstring, which said that
parser_source is what the decorator was given. That is so for @with_argparser, but
@with_annotated records a factory which cmd2 makes from the command's type hints. 6904ef92
names each case, so that a caller inspecting an annotated command does not take parser_source
for the command's function.

@with_argparser and @with_annotated record an ArgparseCommandSpec on the command function: the
source of its parser, and whether argparse is given the command's arguments with their quotes. It
was readable only through a private attribute. Code which describes an application's commands, as
the MCP server example in #1855 does, needs preserve_quotes to say how a subcommand is typed.

- Cmd.get_argparse_command_spec(func) returns the spec, or None for a command which neither
  decorator made. A disabled command's stand-in carries its spec.
- CommandParsers.get() and argparse completion use it in place of the private attribute.
- docs/features/argument_processing.md shows how to ask, and CHANGELOG.md has an entry under
  4.5.0.
@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.76%. Comparing base (9c8b15d) to head (6904ef9).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1859   +/-   ##
=======================================
  Coverage   99.76%   99.76%           
=======================================
  Files          23       23           
  Lines        6773     6777    +4     
=======================================
+ Hits         6757     6761    +4     
  Misses         16       16           
Flag Coverage Δ
unittests 99.76% <100.00%> (+<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.

… bound methods

- ArgparseCommandSpec is frozen. Through the getter, one application could change the spec which
  every application shares, so that it and completion reported what the command did not do.
- CommandParsers.get() reads the spec directly again. The argparse decorators pass it their own
  function, which is not bound, and the public accessor takes a bound method; calling it from
  there also called an override of it during Cmd.__init__().
Freezing the spec stops its fields being reassigned, not a change to the parser which
@with_argparser was given: that is the spec's parser_source itself, and each application copies it
when it builds the command's parser. The changelog entry and the docstring of
get_argparse_command_spec() now say so, and send a reader who wants to change one application's
parser to self.command_parsers.get(func).
@with_argparser records the parser or factory it was given. @with_annotated records a factory
which cmd2 makes from the command's type hints, so its parser_source is not the command's
function. The docstring of get_argparse_command_spec() now names each case.
@tleonhardt
tleonhardt marked this pull request as ready for review December 3, 2026 16:34
@tleonhardt
tleonhardt merged commit 3a5a2ff into main Dec 3, 2026
28 checks passed
@tleonhardt
tleonhardt deleted the public-argparse-command-spec branch December 3, 2026 16:34
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.

1 participant