Repository navigation
Make a command's argparse spec public as Cmd.get_argparse_command_spec() - #1859
Merged
Merged
Conversation
@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 Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
… 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
@with_argparserand@with_annotatedrecord anArgparseCommandSpecon 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 incmd2.argparse_utils, but a command's spec couldonly be read through a private attribute.
The MCP server example in #1855 (issue #1852) needs
preserve_quotesto say how a subcommand istyped: with
preserve_quotes=True, argparse gets"list all"with its quotes, so a subcommandwhose name has quotes in it is reached that way, and one named
list allcannot 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, orNonefor a command which neitherdecorator made, such as one made with
@with_argument_list. A disabled command's stand-incarries its spec, since
disable_command()copies the command's attributes onto it.ArgparseCommandSpecis frozen, so its fields cannot be reassigned. A parser given to@with_argparseris still the spec'sparser_sourceitself, which each application copies whenit 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 sayswhat
parser_sourceis for each decorator: for@with_annotated, a factory which cmd2 made fromthe command's type hints, not the command's function.
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.mdshows how to ask whether a command keeps its quotes.CHANGELOG.md: an entry under Enhancements in 4.5.0.Testing
tests/test_argparse.py: a command made with@with_argparserwith and withoutpreserve_quotes, one made with@with_annotated, one from aCommandSet, a disabled one,commands with no spec (
@with_argument_list, and a plaindo_*method), and the two below.make check,make test(3229 passed, 3 skipped) andmake docs-testpass. No new line ofcmd2/cmd2.pyis left uncovered.After review
A review at
9aac81b2found two points, both real, both fixed indaa6c79fwith a test whichfailed first:
spec.preserve_quotes = Falsethrough one application, another reportedFalsewhile thecommand 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 theirown 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 directlyagain. 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 whata decorator recorded from being changed, but the parser which
@with_argparserwas given is thespec's
parser_sourceitself, and a change to it reaches every application which has not built thecommand's parser yet.
41695f0cnarrows the changelog entry and the getter's docstring to say thatthe 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 thatparser_sourceis what the decorator was given. That is so for@with_argparser, but@with_annotatedrecords a factory which cmd2 makes from the command's type hints.6904ef92names each case, so that a caller inspecting an annotated command does not take
parser_sourcefor the command's function.