summaryrefslogtreecommitdiff
path: root/examples/basic.py
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2019-11-08 19:17:53 -0500
committerGitHub <noreply@github.com>2019-11-08 19:17:53 -0500
commitd5916797620225391379ea40ae466bd7f496ff52 (patch)
tree6452dc4a3f1f45e31cfae16c8acccf3dd7746f14 /examples/basic.py
parenta18eef6f6aa89280b5f9b92d4e681f22a438ff8b (diff)
parent36796c2c6be67085f3d9000086de7052f08f433c (diff)
downloadcmd2-git-d5916797620225391379ea40ae466bd7f496ff52.tar.gz
Merge pull request #799 from python-cmd2/doc_updates
Doc updates
Diffstat (limited to 'examples/basic.py')
-rwxr-xr-xexamples/basic.py43
1 files changed, 43 insertions, 0 deletions
diff --git a/examples/basic.py b/examples/basic.py
new file mode 100755
index 00000000..75672a6b
--- /dev/null
+++ b/examples/basic.py
@@ -0,0 +1,43 @@
+#!/usr/bin/env python3
+# coding=utf-8
+"""A simple example demonstrating the following:
+ 1) How to add a command
+ 2) How to add help for that command
+ 3) Persistent history
+ 4) How to run an initialization script at startup
+ 5) How to add custom command aliases using the alias command
+ 6) Shell-like capabilities
+"""
+import cmd2
+from cmd2 import style
+
+
+class BasicApp(cmd2.Cmd):
+ CUSTOM_CATEGORY = 'My Custom Commands'
+
+ def __init__(self):
+ super().__init__(multiline_commands=['echo'], persistent_history_file='cmd2_history.dat',
+ startup_script='scripts/startup.txt', use_ipython=True)
+
+ self.intro = style('Welcome to PyOhio 2019 and cmd2!', fg='red', bg='white', bold=True) + ' 😀'
+
+ # Allow access to your application in py and ipy via self
+ self.locals_in_py = True
+
+ # Set the default category name
+ self.default_category = 'cmd2 Built-in Commands'
+
+ @cmd2.with_category(CUSTOM_CATEGORY)
+ def do_intro(self, _):
+ """Display the intro banner"""
+ self.poutput(self.intro)
+
+ @cmd2.with_category(CUSTOM_CATEGORY)
+ def do_echo(self, arg):
+ """Example of a multiline command"""
+ self.poutput(arg)
+
+
+if __name__ == '__main__':
+ app = BasicApp()
+ app.cmdloop()