summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2018-02-11 23:23:51 -0500
committerGitHub <noreply@github.com>2018-02-11 23:23:51 -0500
commit4a765e16e64d0b8479921f2e36b4cb8b97db92b1 (patch)
treed5177fbf8ea7a732ae21febbf7e1c006e059ce2e /examples
parent4895d5d8db4e57e2ef9a062473a8536f4f07f213 (diff)
parentf3c6b1b32d614076dc17d2736ae1860d37a36cd5 (diff)
downloadcmd2-git-4a765e16e64d0b8479921f2e36b4cb8b97db92b1.tar.gz
Merge pull request #270 from python-cmd2/persistent_history
Added optional persistent readline history feature
Diffstat (limited to 'examples')
-rwxr-xr-xexamples/persistent_history.py33
-rwxr-xr-x[-rw-r--r--]examples/submenus.py6
2 files changed, 34 insertions, 5 deletions
diff --git a/examples/persistent_history.py b/examples/persistent_history.py
new file mode 100755
index 00000000..e1874212
--- /dev/null
+++ b/examples/persistent_history.py
@@ -0,0 +1,33 @@
+#!/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 readline history from a file.
+
+ :param hist_file: file to load readline history from at start and write it to at end
+ """
+ cmd2.Cmd.__init__(self, persistent_history_file=hist_file, persistent_history_length=500)
+ self.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)
+ app.cmdloop()
diff --git a/examples/submenus.py b/examples/submenus.py
index 52f26e08..1e3da0da 100644..100755
--- a/examples/submenus.py
+++ b/examples/submenus.py
@@ -1,4 +1,5 @@
#!/usr/bin/env python
+# coding=utf-8
"""
Create a CLI with a nested command structure as follows. The commands 'second' and 'third' navigate the CLI to the scope
of the submenu. Nesting of the submenus is done with the cmd2.AddSubmenu() decorator.
@@ -6,9 +7,6 @@ of the submenu. Nesting of the submenus is done with the cmd2.AddSubmenu() decor
(Top Level)----second----->(2nd Level)----third----->(3rd Level)
| | |
---> say ---> say ---> say
-
-
-
"""
from __future__ import print_function
import sys
@@ -71,7 +69,6 @@ class SecondLevel(cmd2.Cmd):
return [s for s in ['qwe', 'asd', 'zxc'] if s.startswith(text)]
-
@cmd2.AddSubmenu(SecondLevel(),
command='second',
aliases=('second_alias',),
@@ -105,7 +102,6 @@ class TopLevel(cmd2.Cmd):
return [s for s in ['qwe', 'asd', 'zxc'] if s.startswith(text)]
-
if __name__ == '__main__':
root = TopLevel()