Mercurial > python-cmd2
changeset 351:8a5bc9f5c28e
cmd2 docs
author | catherine@Drou |
---|---|
date | Tue, 16 Feb 2010 23:07:04 -0500 |
parents | 1c91655d05f8 |
children | 798c7f32a960 |
files | docs/pycon2010/akkad.png docs/pycon2010/ease.png docs/pycon2010/graph.py docs/pycon2010/pirate2.py docs/pycon2010/pirate3.py docs/pycon2010/pirate4.py docs/pycon2010/pirate5.py docs/pycon2010/pirate6.py docs/pycon2010/pirate7.py docs/pycon2010/pirate8.py docs/pycon2010/pycon2010.rst docs/pycon2010/schematic.png docs/pycon2010/schematic.py docs/pycon2010/strategy.png |
diffstat | 14 files changed, 245 insertions(+), 67 deletions(-) [+] |
line wrap: on
line diff
--- a/docs/pycon2010/graph.py Tue Feb 16 20:51:47 2010 -0500 +++ b/docs/pycon2010/graph.py Tue Feb 16 23:07:04 2010 -0500 @@ -4,6 +4,8 @@ def label(txt): write(txt, font=('Arial', 20, 'italic')) +hideturtle() +width(6) def line(len, _label): start = pos() @@ -31,9 +33,9 @@ tech(-390, 100, 'AJAX') tech(-300, -10, 'webapp') tech(190, -380, 'CLU') -tech(90, -320, 'TUI') +tech(60, -320, 'TUI') tech(100, -210, 'cmd') -tech(80, -100, 'cmd2') +tech(80, -80, 'cmd2') while True: pass \ No newline at end of file
--- a/docs/pycon2010/pirate2.py Tue Feb 16 20:51:47 2010 -0500 +++ b/docs/pycon2010/pirate2.py Tue Feb 16 23:07:04 2010 -0500 @@ -2,9 +2,9 @@ # using ``do_`` methods class Pirate(Cmd): - gold = 10 + gold = 3 def do_loot(self, arg): - 'Seize booty frrrom a passing ship.' + 'Seize booty from a passing ship.' self.gold += 1 print('Now we gots {0} doubloons'.format(self.gold)) def do_drink(self, arg):
--- a/docs/pycon2010/pirate3.py Tue Feb 16 20:51:47 2010 -0500 +++ b/docs/pycon2010/pirate3.py Tue Feb 16 23:07:04 2010 -0500 @@ -1,16 +1,20 @@ from cmd import Cmd -# using a hook +# using hook class Pirate(Cmd): gold = 3 def do_loot(self, arg): - 'Drown your sorrrows in rrrum.' + 'Seize booty from a passing ship.' self.gold += 1 def do_drink(self, arg): 'Drown your sorrrows in rrrum.' self.gold -= 1 - def postcmd(self, stop, line): - print('Now we gots {0} doubloons'.format(self.gold)) + def precmd(self, line): + self.initial_gold = self.gold + return line + def postcmd(self, stop, line): + if self.gold != self.initial_gold: + print('Now we gots {0} doubloons'.format(self.gold)) pirate = Pirate() pirate.cmdloop() \ No newline at end of file
--- a/docs/pycon2010/pirate4.py Tue Feb 16 20:51:47 2010 -0500 +++ b/docs/pycon2010/pirate4.py Tue Feb 16 23:07:04 2010 -0500 @@ -4,7 +4,7 @@ class Pirate(Cmd): gold = 3 def do_loot(self, arg): - 'Drown your sorrrows in rrrum.' + 'Seize booty from a passing ship.' self.gold += 1 def do_drink(self, arg): '''Drown your sorrrows in rrrum. @@ -16,8 +16,12 @@ if arg: print('''What's "{0}"? I'll take rrrum.'''.format(arg)) self.gold -= 1 - def postcmd(self, stop, line): - print('Now we gots {0} doubloons'.format(self.gold)) + def precmd(self, line): + self.initial_gold = self.gold + return line + def postcmd(self, stop, line): + if self.gold != self.initial_gold: + print('Now we gots {0} doubloons'.format(self.gold)) pirate = Pirate() pirate.cmdloop() \ No newline at end of file
--- a/docs/pycon2010/pirate5.py Tue Feb 16 20:51:47 2010 -0500 +++ b/docs/pycon2010/pirate5.py Tue Feb 16 23:07:04 2010 -0500 @@ -4,7 +4,7 @@ class Pirate(Cmd): gold = 3 def do_loot(self, arg): - 'Drown your sorrrows in rrrum.' + 'Seize booty from a passing ship.' self.gold += 1 def do_drink(self, arg): '''Drown your sorrrows in rrrum. @@ -16,10 +16,14 @@ if arg: print('''What's "{0}"? I'll take rrrum.'''.format(arg)) self.gold -= 1 - def postcmd(self, stop, line): - print('Now we gots {0} doubloons'.format(self.gold)) + def precmd(self, line): + self.initial_gold = self.gold + return line + def postcmd(self, stop, line): + if self.gold != self.initial_gold: + print('Now we gots {0} doubloons'.format(self.gold)) if self.gold < 0: - print("Off to debtorrr's prrrison. Game overrr.") + print("Off to debtorrr's prison. Game overrr.") return True return stop def do_quit(self, arg):
--- a/docs/pycon2010/pirate6.py Tue Feb 16 20:51:47 2010 -0500 +++ b/docs/pycon2010/pirate6.py Tue Feb 16 23:07:04 2010 -0500 @@ -1,4 +1,4 @@ -from cmd2 import Cmd +from cmd import Cmd # prompts and defaults class Pirate(Cmd): @@ -7,7 +7,7 @@ def default(self, line): print('What mean ye by "{0}"?'.format(line)) def do_loot(self, arg): - 'Drown your sorrrows in rrrum.' + 'Seize booty from a passing ship.' self.gold += 1 def do_drink(self, arg): '''Drown your sorrrows in rrrum. @@ -19,10 +19,14 @@ if arg: print('''What's "{0}"? I'll take rrrum.'''.format(arg)) self.gold -= 1 - def postcmd(self, stop, line): - print('Now we gots {0} doubloons'.format(self.gold)) + def precmd(self, line): + self.initial_gold = self.gold + return line + def postcmd(self, stop, line): + if self.gold != self.initial_gold: + print('Now we gots {0} doubloons'.format(self.gold)) if self.gold < 0: - print("Off to debtorrr's prrrison. Game overrr.") + print("Off to debtorrr's prison. Game overrr.") return True return stop def do_quit(self, arg):
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/docs/pycon2010/pirate7.py Tue Feb 16 23:07:04 2010 -0500 @@ -0,0 +1,42 @@ +from cmd2 import Cmd +# prompts and defaults + +class Pirate(Cmd): + gold = 3 + prompt = 'arrr> ' + def default(self, line): + print('What mean ye by "{0}"?'.format(line)) + def do_loot(self, arg): + 'Seize booty from a passing ship.' + self.gold += 1 + def do_drink(self, arg): + '''Drown your sorrrows in rrrum. + + drink [n] - drink [n] barrel[s] o' rum.''' + try: + self.gold -= int(arg) + except: + if arg: + print('''What's "{0}"? I'll take rrrum.'''.format(arg)) + self.gold -= 1 + def precmd(self, line): + self.initial_gold = self.gold + return line + def postcmd(self, stop, line): + if self.gold != self.initial_gold: + print('Now we gots {0} doubloons'.format(self.gold)) + if self.gold < 0: + print("Off to debtorrr's prison. Game overrr.") + return True + return stop + def do_quit(self, arg): + print("Quiterrr!") + return True + default_to_shell = True + multilineCommands = ['sing'] + terminators = Cmd.terminators + ['...'] + def do_sing(self, arg): + print(self.colorize(arg, 'blue')) + +pirate = Pirate() +pirate.cmdloop() \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/docs/pycon2010/pirate8.py Tue Feb 16 23:07:04 2010 -0500 @@ -0,0 +1,51 @@ +from cmd2 import Cmd, options, make_option +# prompts and defaults + +class Pirate(Cmd): + gold = 3 + prompt = 'arrr> ' + def default(self, line): + print('What mean ye by "{0}"?'.format(line)) + def do_loot(self, arg): + 'Seize booty from a passing ship.' + self.gold += 1 + def do_drink(self, arg): + '''Drown your sorrrows in rrrum. + + drink [n] - drink [n] barrel[s] o' rum.''' + try: + self.gold -= int(arg) + except: + if arg: + print('''What's "{0}"? I'll take rrrum.'''.format(arg)) + self.gold -= 1 + def precmd(self, line): + self.initial_gold = self.gold + return line + def postcmd(self, stop, line): + if self.gold != self.initial_gold: + print('Now we gots {0} doubloons'.format(self.gold)) + if self.gold < 0: + print("Off to debtorrr's prison. Game overrr.") + return True + return stop + def do_quit(self, arg): + print("Quiterrr!") + return True + default_to_shell = True + multilineCommands = ['sing'] + terminators = Cmd.terminators + ['...'] + def do_sing(self, arg): + print(self.colorize(arg, 'blue')) + @options([make_option('--ho', type='int', help="How often to chant 'ho'", default=2), + make_option('-c', '--commas', action="store_true", help="Interspers commas")]) + def do_yo(self, arg, opts): + chant = ['yo'] + ['ho'] * opts.ho + if opts.commas: + separator = ', ' + else: + separator = ' ' + print('{0} and a bottle of {1}'.format(separator.join(chant), arg)) + +pirate = Pirate() +pirate.cmdloop() \ No newline at end of file
--- a/docs/pycon2010/pycon2010.rst Tue Feb 16 20:51:47 2010 -0500 +++ b/docs/pycon2010/pycon2010.rst Tue Feb 16 23:07:04 2010 -0500 @@ -1,6 +1,15 @@ -===== -Title -===== +================================================ +Easy command-line interpreters with cmd and cmd2 +================================================ + +:author: Catherine Devlin +:date: 2010-02-20 + +Quit scribbling +=============== + +Slides are *already* posted at +catherinedevlin.pythoneers.com Web 2.0 ======= @@ -14,6 +23,9 @@ .. image:: sargon.jpg :height: 300px +.. image:: akkad.png + :height: 300px + Sargon the Great Founder of Akkadian Empire @@ -29,8 +41,8 @@ Unlike the Akkadian Empire, the CLI will never die. -Defining -======== +Defining CLI +============ - "Line-oriented command interpreter" - "Command-line interface" @@ -78,14 +90,14 @@ Use ``curses``, ``urwid`` -Tradeoff -======== +Priorities +========== -.. image:: ease.png +.. image:: strategy.png :height: 300px -pirate.py -========= +A ``cmd`` app: pirate.py +======================== :: @@ -97,16 +109,18 @@ pirate = Pirate() pirate.cmdloop() -Nothing here... but history and help +.. Nothing here... but history and help .. ctrl-r for bash-style history Fundamental prrrinciple ======================= -.. class: huge +.. class:: huge - ``foo a b c`` -> + :: + + (Cmd) foo a b c ``self.do_foo('a b c')`` @@ -116,7 +130,7 @@ :: class Pirate(Cmd): - gold = 10 + gold = 3 def do_loot(self, arg): 'Seize booty frrrom a passing ship.' self.gold += 1 @@ -139,16 +153,18 @@ :: - class Pirate(Cmd): - gold = 3 - def do_loot(self, arg): - 'Drown your sorrrows in rrrum.' - self.gold += 1 - def do_drink(self, arg): - 'Drown your sorrrows in rrrum.' - self.gold -= 1 - def postcmd(self, stop, line): - print('Now we gots {0} doubloons'.format(self.gold)) + def do_loot(self, arg): + 'Seize booty from a passing ship.' + self.gold += 1 + def do_drink(self, arg): + 'Drown your sorrrows in rrrum.' + self.gold -= 1 + def precmd(self, line): + self.initial_gold = self.gold + return line + def postcmd(self, stop, line): + if self.gold != self.initial_gold: + print('Now we gots {0} doubloons'.format(self.gold)) Arguments: pirate4.py ===================== @@ -172,16 +188,16 @@ :: - def postcmd(self, stop, line): - print('Now we gots {0} doubloons' - .format(self.gold)) + def postcmd(self, stop, line): + if self.gold != self.initial_gold: + print('Now we gots {0} doubloons'.format(self.gold)) if self.gold < 0: - print("Off to debtorrr's prrrison. Game overrr.") + print("Off to debtorrr's prison. Game overrr.") return True return stop def do_quit(self, arg): print("Quiterrr!") - return True + return True prompts and defaults: pirate6.py ================================ @@ -192,11 +208,12 @@ def default(self, line): print('What mean ye by "{0}"?' .format(line)) - + cmd2 ==== -Third-party module in PyPI +.. image:: schematic.png + :height: 300px Absolutely free =============== @@ -223,23 +240,41 @@ * Default to shell * Color output * Shortcuts + * Multiline commands + * Environment variables -But wait, there's more -====================== +Now how much would you pay? +=========================== + + * options / flags + * Quiet (suppress feedback) + * BASH-style ``select`` + * Parsing: terminators, suffixes + +Minor changes: pirate7.py +========================= + +:: - * Case-insensitive commands - * Abbreviated commands - * Quitting - * Timing - * Echo - * Debug - * Color output + default_to_shell = True + multilineCommands = ['sing'] + terminators = Cmd.terminators + ['...'] + def do_sing(self, arg): + print(self.colorize(arg, 'blue')) + +Options: pirate8.py +=================== + +:: - -More -==== + @options([make_option('--ho', type='int', help="How often to chant 'ho'", default=2), + make_option('-c', '--commas', action="store_true", help="Interspers commas")]) + def do_yo(self, arg, opts): + chant = ['yo'] + ['ho'] * opts.ho + if opts.commas: + separator = ', ' + else: + separator = ' ' + print('{0} and a bottle of {1}'.format(separator.join(chant), arg)) - * Case-insensitivity - * One-character shortcuts - * Default to shell - \ No newline at end of file + \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/docs/pycon2010/schematic.py Tue Feb 16 23:07:04 2010 -0500 @@ -0,0 +1,32 @@ +from turtle import * +hideturtle() +width(6) +pensize = 10 +pu() +goto(0,-400) + +def rectangle(x, y, _label): + pu() + seth(0) + backward(x / 2) + fontsize = 40 + pd() + for i in range(2): + forward(x) + left(90) + forward(y) + left(90) + pu() + forward(x / 2) + left(90) + forward(y / 2 - fontsize) + pd() + write(_label, align='center', font=('Arial', fontsize, 'bold')) + +rectangle(800, 80, 'cmd') +pu() +forward(80) +rectangle(200, 400, 'cmd2') + +while True: + pass