| 1 |
|
| 2 |
"""This example demonstrates how to enable persistent readline history in your cmd2 application. |
| 3 |
|
| 4 |
This will allow end users of your cmd2-based application to use the arrow keys and Ctrl+r in a manner which persists |
| 5 |
across invocations of your cmd2 application. This can make it much easier for them to use your application. |
| 6 |
""" |
| 7 |
|
| 8 |
import cmd2 |
| 9 |
|
| 10 |
|
| 11 |
class Cmd2PersistentHistory(cmd2.Cmd): |
| 12 |
"""Basic example of how to enable persistent readline history within your cmd2 app.""" |
| 13 |
|
| 14 |
def __init__(self, hist_file) -> None: |
| 15 |
"""Configure the app to load persistent history from a file (both readline and cmd2 history command affected). |
| 16 |
|
| 17 |
:param hist_file: file to load history from at start and write it to at end |
| 18 |
""" |
| 19 |
super().__init__(persistent_history_file=hist_file, persistent_history_length=500, allow_cli_args=False) |
| 20 |
self.prompt = "ph> " |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
if __name__ == "__main__": |
| 26 |
import sys |
| 27 |
|
| 28 |
history_file = "~/.persistent_history.cmd2" |
| 29 |
if len(sys.argv) > 1: |
| 30 |
history_file = sys.argv[1] |
| 31 |
|
| 32 |
app = Cmd2PersistentHistory(hist_file=history_file) |
| 33 |
sys.exit(app.cmdloop()) |