From 9e64f600b65d99ea5a83051372372512be698b1b Mon Sep 17 00:00:00 2001 From: kotfu Date: Fri, 5 Jul 2019 15:05:51 -0600 Subject: Added a walkthrough of constructing a simple application to the overview For #709 --- examples/example.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'examples') diff --git a/examples/example.py b/examples/example.py index 24be5d5d..a62c258b 100755 --- a/examples/example.py +++ b/examples/example.py @@ -26,10 +26,9 @@ class CmdLineApp(cmd2.Cmd): MUMBLE_LAST = ['right?'] def __init__(self): - shortcuts = dict(cmd2.DEFAULT_SHORTCUTS) + shortcuts = cmd2.DEFAULT_SHORTCUTS shortcuts.update({'&': 'speak'}) - # Set use_ipython to True to enable the "ipy" command which embeds and interactive IPython shell - super().__init__(use_ipython=False, multiline_commands=['orate'], shortcuts=shortcuts) + super().__init__(multiline_commands=['orate'], shortcuts=shortcuts) # Make maxrepeats settable at runtime self.maxrepeats = 3 @@ -52,7 +51,7 @@ class CmdLineApp(cmd2.Cmd): word = word.upper() words.append(word) repetitions = args.repeat or 1 - for i in range(min(repetitions, self.maxrepeats)): + for _ in range(min(repetitions, self.maxrepeats)): # .poutput handles newlines, and accommodates output redirection too self.poutput(' '.join(words)) -- cgit v1.2.1 From 369f7eb1df01cc46e0212aa131536e99c1fa68ba Mon Sep 17 00:00:00 2001 From: kotfu Date: Sat, 6 Jul 2019 12:51:55 -0600 Subject: Work on Getting Started section of documentation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - change the walk through from example.py to a new first_app.py (based on example.py) - remove the feature tour section, we’ll use the first application to demo the key features --- examples/first_app.py | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100755 examples/first_app.py (limited to 'examples') diff --git a/examples/first_app.py b/examples/first_app.py new file mode 100755 index 00000000..4c2d977c --- /dev/null +++ b/examples/first_app.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python +# coding=utf-8 +""" +A simple application using cmd2 which demonstrates 8 key features: + + * Settings + * Commands + * Argument Parsing + * Generating Output + * Help + * Shortcuts + * Multiline Commands + * History +""" +import argparse +import random + +import cmd2 + + +class FirstApp(cmd2.Cmd): + """A simple cmd2 application.""" + + def __init__(self): + shortcuts = cmd2.DEFAULT_SHORTCUTS + shortcuts.update({'&': 'speak'}) + super().__init__(multiline_commands=['orate'], shortcuts=shortcuts) + + # Make maxrepeats settable at runtime + self.maxrepeats = 3 + self.settable['maxrepeats'] = 'max repetitions for speak command' + + speak_parser = argparse.ArgumentParser() + speak_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay') + speak_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE') + speak_parser.add_argument('-r', '--repeat', type=int, help='output [n] times') + speak_parser.add_argument('words', nargs='+', help='words to say') + + @cmd2.with_argparser(speak_parser) + def do_speak(self, args): + """Repeats what you tell me to.""" + words = [] + for word in args.words: + if args.piglatin: + word = '%s%say' % (word[1:], word[0]) + if args.shout: + word = word.upper() + words.append(word) + repetitions = args.repeat or 1 + for _ in range(min(repetitions, self.maxrepeats)): + # .poutput handles newlines, and accommodates output redirection too + self.poutput(' '.join(words)) + + # orate is a synonym for speak which takes multiline input + do_orate = do_speak + + +if __name__ == '__main__': + import sys + c = FirstApp() + sys.exit(c.cmdloop()) -- cgit v1.2.1 From 9523c0dc56b4b7c208ea69adeb05c802d9aa10b2 Mon Sep 17 00:00:00 2001 From: kotfu Date: Sat, 6 Jul 2019 14:08:17 -0600 Subject: Fix build error for unused import --- examples/first_app.py | 1 - 1 file changed, 1 deletion(-) (limited to 'examples') diff --git a/examples/first_app.py b/examples/first_app.py index 4c2d977c..b5bd07e9 100755 --- a/examples/first_app.py +++ b/examples/first_app.py @@ -13,7 +13,6 @@ A simple application using cmd2 which demonstrates 8 key features: * History """ import argparse -import random import cmd2 -- cgit v1.2.1