Skip to content

Commit 20918ff

Browse files
committed
Added additional test for Cmd._build_parser().
1 parent a8b4dba commit 20918ff

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
## 2.6.2 (TBD, 2025)
22

33
- Enhancements
4+
45
- Added explicit support for free-threaded versions of Python, starting with version 3.14
56

7+
- Bug Fixes
8+
- Restored code to set a parser's `prog` value in the `with_argparser` decorator. This is to
9+
preserve backward compatibility in the `cmd2` 2.0 family. This functionality will be removed
10+
in `cmd2` 3.0.0.
11+
612
## 2.6.1 (June 8, 2025)
713

814
- Bug Fixes

cmd2/decorators.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,10 @@ def cmd_wrapper(*args: Any, **kwargs: dict[str, Any]) -> Optional[bool]:
395395

396396
if isinstance(parser, argparse.ArgumentParser):
397397
# Set parser's prog value for backward compatibility within the cmd2 2.0 family.
398-
# This will be removed in cmd2 3.0 since we never reference this parser's prog value.
399-
# We only set prog on the deep copy of this parser created in Cmd._build_parser().
398+
# This will be removed in cmd2 3.0 since we never reference this parser object's prog value.
399+
# Since it's possible for the same parser object to be passed into multiple with_argparser()
400+
# calls, we only set prog on the deep copies of this parser based on the specific do_xxxx
401+
# instance method they are associated with.
400402
_set_parser_prog(parser, command_name)
401403

402404
# Set some custom attributes for this command

tests/test_argparse.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,12 @@ def test_preservelist(argparse_app) -> None:
248248
assert out[0] == "['foo', '\"bar baz\"']"
249249

250250

251+
def test_invalid_parser_builder(argparse_app):
252+
parser_builder = None
253+
with pytest.raises(TypeError):
254+
argparse_app._build_parser(argparse_app, parser_builder, "fake_prog")
255+
256+
251257
def _build_has_subcmd_parser() -> cmd2.Cmd2ArgumentParser:
252258
has_subcmds_parser = cmd2.Cmd2ArgumentParser(description="Tests as_subcmd_to decorator")
253259
has_subcmds_parser.add_subparsers(dest='subcommand', metavar='SUBCOMMAND', required=True)

0 commit comments

Comments
 (0)