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