diff options
author | kotfu <kotfu@kotfu.net> | 2019-07-06 17:52:19 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-06 17:52:19 -0600 |
commit | 34975b52e769514b44ee8c13147a22961e68e7a7 (patch) | |
tree | fe93e18257514efc25e8539d5005af34ef4dcd3b /examples | |
parent | e18013e7f6be6721531cde163ec4697eac247270 (diff) | |
parent | 0ae0567c48c0519cbecca8448df0caa32f530906 (diff) | |
download | cmd2-git-34975b52e769514b44ee8c13147a22961e68e7a7.tar.gz |
Merge pull request #712 from python-cmd2/docstructure
New Documentation Structure
Diffstat (limited to 'examples')
-rwxr-xr-x | examples/example.py | 7 | ||||
-rwxr-xr-x | examples/first_app.py | 60 |
2 files changed, 63 insertions, 4 deletions
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)) diff --git a/examples/first_app.py b/examples/first_app.py new file mode 100755 index 00000000..b5bd07e9 --- /dev/null +++ b/examples/first_app.py @@ -0,0 +1,60 @@ +#!/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 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()) |