diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2017-02-26 17:19:15 -0500 |
---|---|---|
committer | Todd Leonhardt <todd.leonhardt@gmail.com> | 2017-02-26 17:19:15 -0500 |
commit | 552c02561e8e79b9b2e658453f269fba68fab9a5 (patch) | |
tree | e74587bd07f912a7063cdbb2338a7c395d1aad32 /examples/example.py | |
parent | 04632df7659a7fa68d75770d862e4edd32acff2b (diff) | |
download | cmd2-git-552c02561e8e79b9b2e658453f269fba68fab9a5.tar.gz |
Changes to make our project as welcoming as possible for new contributors.
Changes include:
1) Added CONTRIBUTING.md with detailed instructions for how to contribute, which should be especially useful to those new to open source in general or GitHub in particular
2) Added CODE_OF_CONDUCT.md which sets ground rules for participants’ behavior and helps to facilitate a friendly, welcoming environment
3) Renamed the "example" directory to "examples" in the hope that one day soon there may be more than a single example ;-)
Diffstat (limited to 'examples/example.py')
-rwxr-xr-x | examples/example.py | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/examples/example.py b/examples/example.py new file mode 100755 index 00000000..f39be5de --- /dev/null +++ b/examples/example.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python +# coding=utf-8 +"""A sample application for cmd2. + +Thanks to cmd2's built-in transtript testing capability, it also serves as a test suite for example.py when used with + the exampleSession.txt transcript. + +Running `python example.py -t exampleSession.txt` will run all the commands in the transcript against example.py, +verifying that the output produced matches the transcript. +""" + +from cmd2 import Cmd, make_option, options + + +class CmdLineApp(Cmd): + """ Example cmd2 application. """ + multilineCommands = ['orate'] + Cmd.shortcuts.update({'&': 'speak'}) + maxrepeats = 3 + Cmd.settable.append('maxrepeats') + + # Setting this true makes it run a shell command if a cmd2/cmd command doesn't exist + # default_to_shell = True + + def __init__(self): + # Set use_ipython to True to enable the "ipy" command which embeds and interactive IPython shell + Cmd.__init__(self, use_ipython=False) + + @options([make_option('-p', '--piglatin', action="store_true", help="atinLay"), + make_option('-s', '--shout', action="store_true", help="N00B EMULATION MODE"), + make_option('-r', '--repeat', type="int", help="output [n] times") + ]) + def do_speak(self, arg, opts=None): + """Repeats what you tell me to.""" + arg = ''.join(arg) + if opts.piglatin: + arg = '%s%say' % (arg[1:], arg[0]) + if opts.shout: + arg = arg.upper() + repetitions = opts.repeat or 1 + for i in range(min(repetitions, self.maxrepeats)): + self.stdout.write(arg) + self.stdout.write('\n') + # self.stdout.write is better than "print", because Cmd can be + # initialized with a non-standard output destination + + do_say = do_speak # now "say" is a synonym for "speak" + do_orate = do_speak # another synonym, but this one takes multi-line input + + +if __name__ == '__main__': + c = CmdLineApp() + c.cmdloop() |