Skip to content

Commit ef964fb

Browse files
committed
All built-in commands now use a function to create their argument parser.
This simplifies the process for overriding cmd2's default parser class.
1 parent 41a49ff commit ef964fb

File tree

13 files changed

+386
-371
lines changed

13 files changed

+386
-371
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
## 3.0.0 (TBD)
2-
* Breaking Change
2+
* Breaking Changes
33
* Removed macros
4+
* Enhancements
5+
* Simplified the process to set a custom parser for `cmd2's` built-in commands.
6+
See [custom_parser.py](https://github.com/python-cmd2/cmd2/blob/master/examples/custom_parser.py)
7+
example for more details.
48

59
## 2.5.0 (October 23, 2024)
610
* Breaking Change

cmd2/__init__.py

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
# flake8: noqa F401
44
"""This simply imports certain things for backwards compatibility."""
55

6-
import sys
7-
86
import importlib.metadata as importlib_metadata
97

108
try:
@@ -15,53 +13,56 @@
1513

1614
from typing import List
1715

16+
from . import plugin
1817
from .ansi import (
19-
Cursor,
2018
Bg,
21-
Fg,
19+
Cursor,
2220
EightBitBg,
2321
EightBitFg,
22+
Fg,
2423
RgbBg,
2524
RgbFg,
2625
TextStyle,
2726
style,
2827
)
28+
from .argparse_completer import set_default_ap_completer_type
2929
from .argparse_custom import (
3030
Cmd2ArgumentParser,
3131
Cmd2AttributeWrapper,
3232
CompletionItem,
3333
register_argparse_argument_parameter,
3434
set_default_argument_parser_type,
3535
)
36-
37-
# Check if user has defined a module that sets a custom value for argparse_custom.DEFAULT_ARGUMENT_PARSER.
38-
# Do this before loading cmd2.Cmd class so its commands use the custom parser.
39-
import argparse
40-
41-
cmd2_parser_module = getattr(argparse, 'cmd2_parser_module', None)
42-
if cmd2_parser_module is not None:
43-
import importlib
44-
45-
importlib.import_module(cmd2_parser_module)
46-
47-
from .argparse_completer import set_default_ap_completer_type
48-
4936
from .cmd2 import Cmd
50-
from .command_definition import CommandSet, with_default_category
51-
from .constants import COMMAND_NAME, DEFAULT_SHORTCUTS
52-
from .decorators import with_argument_list, with_argparser, with_category, as_subcommand_to
37+
from .command_definition import (
38+
CommandSet,
39+
with_default_category,
40+
)
41+
from .constants import (
42+
COMMAND_NAME,
43+
DEFAULT_SHORTCUTS,
44+
)
45+
from .decorators import (
46+
as_subcommand_to,
47+
with_argparser,
48+
with_argument_list,
49+
with_category,
50+
)
5351
from .exceptions import (
5452
Cmd2ArgparseError,
5553
CommandSetRegistrationError,
5654
CompletionError,
5755
PassThroughException,
5856
SkipPostcommandHooks,
5957
)
60-
from . import plugin
6158
from .parsing import Statement
6259
from .py_bridge import CommandResult
63-
from .utils import categorize, CompletionMode, CustomCompletionSettings, Settable
64-
60+
from .utils import (
61+
CompletionMode,
62+
CustomCompletionSettings,
63+
Settable,
64+
categorize,
65+
)
6566

6667
__all__: List[str] = [
6768
'COMMAND_NAME',
@@ -81,8 +82,8 @@
8182
'Cmd2AttributeWrapper',
8283
'CompletionItem',
8384
'register_argparse_argument_parameter',
84-
'set_default_argument_parser_type',
8585
'set_default_ap_completer_type',
86+
'set_default_argument_parser_type',
8687
# Cmd2
8788
'Cmd',
8889
'CommandResult',
@@ -98,6 +99,7 @@
9899
'Cmd2ArgparseError',
99100
'CommandSetRegistrationError',
100101
'CompletionError',
102+
'PassThroughException',
101103
'SkipPostcommandHooks',
102104
# modules
103105
'plugin',

cmd2/argparse_custom.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,14 +1402,21 @@ def set(self, new_val: Any) -> None:
14021402
self.__attribute = new_val
14031403

14041404

1405-
# The default ArgumentParser class for a cmd2 app
1406-
DEFAULT_ARGUMENT_PARSER: Type[argparse.ArgumentParser] = Cmd2ArgumentParser
1405+
# Parser type used by cmd2's built-in commands.
1406+
# Set it using cmd2.set_default_argument_parser_type().
1407+
DEFAULT_ARGUMENT_PARSER: Type[Cmd2ArgumentParser] = Cmd2ArgumentParser
14071408

14081409

1409-
def set_default_argument_parser_type(parser_type: Type[argparse.ArgumentParser]) -> None:
1410+
def set_default_argument_parser_type(parser_type: Type[Cmd2ArgumentParser]) -> None:
14101411
"""
1411-
Set the default ArgumentParser class for a cmd2 app. This must be called prior to loading cmd2.py if
1412-
you want to override the parser for cmd2's built-in commands. See examples/override_parser.py.
1412+
Set the default ArgumentParser class for cmd2's built-in commands.
1413+
1414+
Since built-in commands rely on customizations made in Cmd2ArgumentParser to
1415+
generate help text, your custom parser class should inherit from Cmd2ArgumentParser.
1416+
1417+
This should be called prior to instantiating your CLI object.
1418+
1419+
See examples/custom_parser.py.
14131420
"""
14141421
global DEFAULT_ARGUMENT_PARSER
14151422
DEFAULT_ARGUMENT_PARSER = parser_type

0 commit comments

Comments
 (0)