Debian Codegraph @

docs/upgrades.md

raw symbols SCIP
1 # cmd2 Major Versions Upgrades
2
3 ## Upgrading to cmd2 4.x from 3.x
4
5 The biggest change from 3.x to 4.x is the migration from the GNU Readline library to
6 [prompt-toolkit](https://github.com/prompt-toolkit/python-prompt-toolkit) for the Read-Eval-Print
7 Loop (REPL). This change provides a pure Python replacement for readline that is fully
8 cross-platform compatible and offers significant enhancements in terms of features and
9 extensibility.
10
11 ### prompt-toolkit Migration
12
13 `cmd2` now utilizes `prompt-toolkit` for all input handling, history navigation, and tab completion.
14 This removes the dependency on the GNU Readline library (and the `gnureadline` package on macOS).
15
16 #### Key Benefits of prompt-toolkit
17
18 - **Cross-platform**: Works identically on Windows, macOS, and Linux without external dependencies.
19 - **Asynchronous Output**: Better handling of asynchronous printing to the terminal without
20 interfering with the user's input line.
21 - **Enhanced UI**: Support for advanced UI elements like bottom toolbars and floating menus.
22 - **Multiline Editing**: Improved support for editing commands that span multiple lines.
23
24 #### Breaking Changes and Incompatibilities
25
26 While we have strived to maintain compatibility, there are some differences:
27
28 - **Key Bindings**: Key bindings are now managed by `prompt-toolkit`. While it defaults to
29 Emacs-style bindings (similar to readline), customization now uses the `prompt-toolkit`
30 [KeyBindings](https://python-prompt-toolkit.readthedocs.io/en/stable/pages/advanced_topics/key_bindings.html)
31 API.
32 - **Input Hooks**: Readline-specific input hooks are no longer supported.
33
34 ### Bottom Toolbar
35
36 `cmd2` now supports an optional, persistent bottom toolbar. This can be used to display information
37 such as the application name, current state, or even a real-time clock.
38
39 - **Enablement**: Set `enable_bottom_toolbar=True` in the [cmd2.Cmd.__init__][] constructor.
40 - **Customization**: Override the [cmd2.Cmd.get_bottom_toolbar][] method to return the content you
41 wish to display.
42
43 See the
44 [getting_started.py](https://github.com/python-cmd2/cmd2/blob/main/examples/getting_started.py)
45 example for a demonstration of how to implement a background thread that refreshes the toolbar
46 periodically.
47
48 ### Custom Completion Menu Colors
49
50 `cmd2` now leverages `prompt-toolkit` for its tab completion menu and provides the ability to
51 customize its appearance using the `cmd2` theme.
52
53 - **Customization**: Override the `Cmd2Style.COMPLETION_MENU_CURRENT` and
54 `Cmd2Style.COMPLETION_MENU_META` styles using `cmd2.theme.update_theme()`. See
55 [Customizing Completion Menu Colors](features/theme.md#customizing-completion-menu-colors) for
56 more details.
57
58 ### Deleted Modules
59
60 Removed `rl_utils.py` and `terminal_utils.py` since `prompt-toolkit` provides this functionality.
61
62 ## Upgrading to cmd2 3.x from 2.x
63
64 For details about all of the changes in the 3.0.0 release, please refer to
65 [CHANGELOG.md](https://github.com/python-cmd2/cmd2/blob/main/CHANGELOG.md).
66
67 The biggest change from 2.x to 3.x is that `cmd2` now has a dependency on
68 [rich](https://github.com/Textualize/rich). Accordingly, `cmd2` now relies on `rich` for beautiful
69 text styling and formatting within the terminal. As such, a good chunk of custom code has been
70 removed from `cmd2` and other things have either moved or altered to be based on `rich`.
71
72 The major things users should be aware of when upgrading to 3.x are detailed in subsections below.
73
74 ### Deleted Modules
75
76 #### ansi
77
78 The functionality within the `cmd2.ansi` module has either been removed or changed to be based on
79 `rich` and moved to one of the new modules: [cmd2.string_utils][], [cmd2.styles][], or
80 [cmd2.terminal_utils][].
81
82 To ease the migration path from `cmd2` 2.x to 3.x, we have created the `cmd2-ansi` module which is a
83 backport of the `cmd2.ansi` module present in `cmd2` 2.7.0 in a standalone fashion. Relevant links:
84
85 - PyPI: [cmd2-ansi](https://pypi.org/project/cmd2-ansi/)
86 - GitHub: [cmd2-ansi](https://github.com/python-cmd2/cmd2-ansi)
87
88 To use this backport:
89
90 ```Python
91 from cmd2_ansi import ansi
92 ```
93
94 #### table_creator
95
96 The `cmd2.table_creator` module no longer exists. Please see rich's documentation on
97 [Tables](https://rich.readthedocs.io/en/latest/tables.html) for more information. The
98 [rich_tables.py](https://github.com/python-cmd2/cmd2/blob/main/examples/rich_tables.py) example
99 demonstrates how to use `rich` tables in a `cmd2` application.
100
101 `rich` tables offer a degree of power and flexibility that are superior to what `cmd2` previously
102 offered. We apologize for this backwards incompatibility, but the APIs were fundamentally different
103 and we could not figure out a way to create a backwards-compatibility wrapper.
104
105 To ease the migration path from `cmd2` 2.x to 3.x, we have created the `cmd2-table` module which is
106 a backport of the `cmd2.table_creator` module present in `cmd2` 2.7.0 in a standalone fashion.
107 Relevant links:
108
109 - PyPI: [cmd2-table](https://pypi.org/project/cmd2-table/)
110 - GitHub: [cmd2-table](https://github.com/python-cmd2/cmd2-table)
111
112 To use this backport:
113
114 ```Python
115 from cmd2_table import table_creator
116 ```
117
118 ### Added modules
119
120 #### colors
121
122 The new [cmd2.colors][] module provides the convenient [cmd2.colors.Color][] `StrEnum` class for
123 `rich` color names. This allows you to use tab-completable constants in your code instead of magic
124 strings to represent the precise color you want.
125
126 See the
127 [getting_started.py](https://github.com/python-cmd2/cmd2/blob/main/examples/getting_started.py) for
128 a basic example of using the `Color` class to choose colors for stylizing your output.
129 Alternatively, see the [color.py](https://github.com/python-cmd2/cmd2/blob/main/examples/color.py)
130 example for a visual demonstration of all supported colors.
131
132 #### rich_utils
133
134 The new [cmd2.rich_utils][] module provides common utility classes and functions for supporting the
135 use of `rich` within `cmd2` applications. Most of what is here is not intended to be user-facing.
136
137 The one thing many `cmd2` application developers will likely be interested in using is the
138 [cmd2.theme.update_theme][] function. See the
139 [rich_theme.py](https://github.com/python-cmd2/cmd2/blob/main/examples/rich_theme.py) example for a
140 demonstration for how to set a theme (color scheme) for your app.
141
142 #### styles
143
144 Default styles for how something like an error message should be displayed are now located in the
145 new [cmd2.styles][] module and they are now based on `rich` styles.
146
147 Previously `cmd2` default styles were in the `cmd2.ansi` module.
148
149 See
150 [argparse_completion.py](https://github.com/python-cmd2/cmd2/blob/main/examples/argparse_completion.py)
151 for an example on how you can leverage these default styles in your `cmd2` application to maintain a
152 consistent look and feel.
153
154 #### string_utils
155
156 Various string utility functions have been moved from the `cmd2.ansi` module to the new
157 [cmd2.string_utils][] module.
158
159 This includes functions for styling, aligning, and quoting/un-quoting text. See the
160 [getting_started.py](https://github.com/python-cmd2/cmd2/blob/main/examples/getting_started.py)
161 example for a demonstration of how to use the common [cmd2.string_utils.stylize][] function.
162
163 #### terminal_utils
164
165 Support for terminal control escape sequences for things like setting the window title and
166 asynchronous alerts has been moved from `cmd2.ansi` to the new [cmd2.terminal_utils][] module.
167
168 This isn't really intended to be used by end users, but is used by higher-level functionality that
169 is intended to be used by end users such as [cmd2.Cmd.set_window_title][] and
170 [cmd2.Cmd.async_alert][].
171
172 See [async_printing.py](https://github.com/python-cmd2/cmd2/blob/main/examples/async_printing.py)
173 for an example of how to use this functionality in a `cmd2` application.
174
175 ### Argparse HelpFormatter classes
176
177 `cmd2` now has 5 different Argparse HelpFormatter classes, all of which are based on the
178 `RichHelpFormatter` class from [rich-argparse](https://github.com/hamdanal/rich-argparse).
179
180 - [Cmd2HelpFormatter][cmd2.argparse_custom.Cmd2HelpFormatter]
181 - [ArgumentDefaultsCmd2HelpFormatter][cmd2.argparse_custom.ArgumentDefaultsCmd2HelpFormatter]
182 - [MetavarTypeCmd2HelpFormatter][cmd2.argparse_custom.MetavarTypeCmd2HelpFormatter]
183 - [RawDescriptionCmd2HelpFormatter][cmd2.argparse_custom.RawDescriptionCmd2HelpFormatter]
184 - [RawTextCmd2HelpFormatter][cmd2.argparse_custom.RawTextCmd2HelpFormatter]
185
186 Previously the default `Cmd2HelpFormatter` class inherited from `argparse.RawTextHelpFormatter`,
187 however it now inherits from `argparse.HelpFormatter`. If you want RawText behavior, then pass
188 `formatter_class=RawTextCmd2HelpFormatter` to your parser.
189
190 The benefit is that your `cmd2` applications now have more aesthetically pleasing help which
191 includes color to make it quicker and easier to visually parse help text. This works for all
192 supported versions of Python.
193
194 ### Other Changes
195
196 - The `auto_load_commands` argument to `cmd2.Cmd.__init__` now defaults to `False`
197 - Replaced `Settable.get_value()` and `Settable.set_value()` methods with a more Pythonic `value`
198 property
199 - Removed redundant setting of a parser's `prog` value in the `with_argparser()` decorator, as this
200 is now handled centrally in `Cmd._build_parser()`