summaryrefslogtreecommitdiff
path: root/README.md
diff options
context:
space:
mode:
Diffstat (limited to 'README.md')
-rwxr-xr-xREADME.md250
1 files changed, 131 insertions, 119 deletions
diff --git a/README.md b/README.md
index 164fc388..76c443fd 100755
--- a/README.md
+++ b/README.md
@@ -70,10 +70,13 @@ Instructions for implementing each feature follow.
- Parsing commands with `optparse` options (flags)
- @options([make_option('-m', '--myoption', action="store_true", help="all about my option")])
- def do_myfunc(self, arg, opts):
- if opts.myoption:
- ...
+ ```python
+ @options([make_option('-m', '--myoption', action="store_true", help="all about my option")])
+ def do_myfunc(self, arg, opts):
+ if opts.myoption:
+ #TODO: Do something useful
+ pass
+ ```
See Python standard library's ``optparse`` documentation: http://docs.python.org/lib/optparse-defining-options.html
@@ -91,126 +94,135 @@ A couple tutorials on using cmd2 exist:
Example Application
-------------------
-Example cmd2 application (**examples/example.py**) ::
-
- '''A sample application for cmd2.'''
-
- from cmd2 import Cmd, make_option, options
-
- class CmdLineApp(Cmd):
- 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
-
- @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()
+Example cmd2 application (**examples/example.py**):
+
+```python
+'''A sample application for cmd2.'''
+
+from cmd2 import Cmd, make_option, options
+
+class CmdLineApp(Cmd):
+ 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
+
+ @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()
+```
The following is a sample session running example.py.
Thanks to Cmd2's built-in transcript testing capability, it also serves as a test
suite for example.py when saved as *exampleSession.txt*.
-Running::
-
- python example.py -t exampleSession.txt
+Running
+```bash
+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.
-example/exampleSession.txt::
-
- (Cmd) help
-
- Documented commands (type help <topic>):
- ========================================
- _relative_load edit help list orate py run say shell show
- cmdenvironment eof history load pause quit save set shortcuts speak
-
- (Cmd) help say
- Repeats what you tell me to.
- Usage: speak [options] arg
-
- Options:
- -h, --help show this help message and exit
- -p, --piglatin atinLay
- -s, --shout N00B EMULATION MODE
- -r REPEAT, --repeat=REPEAT
- output [n] times
-
- (Cmd) say goodnight, Gracie
- goodnight, Gracie
- (Cmd) say -ps --repeat=5 goodnight, Gracie
- OODNIGHT, GRACIEGAY
- OODNIGHT, GRACIEGAY
- OODNIGHT, GRACIEGAY
- (Cmd) set maxrepeats 5
- maxrepeats - was: 3
- now: 5
- (Cmd) say -ps --repeat=5 goodnight, Gracie
- OODNIGHT, GRACIEGAY
- OODNIGHT, GRACIEGAY
- OODNIGHT, GRACIEGAY
- OODNIGHT, GRACIEGAY
- OODNIGHT, GRACIEGAY
- (Cmd) hi
- -------------------------[1]
- help
- -------------------------[2]
- help say
- -------------------------[3]
- say goodnight, Gracie
- -------------------------[4]
- say -ps --repeat=5 goodnight, Gracie
- -------------------------[5]
- set maxrepeats 5
- -------------------------[6]
- say -ps --repeat=5 goodnight, Gracie
- (Cmd) run 4
- say -ps --repeat=5 goodnight, Gracie
-
- OODNIGHT, GRACIEGAY
- OODNIGHT, GRACIEGAY
- OODNIGHT, GRACIEGAY
- OODNIGHT, GRACIEGAY
- OODNIGHT, GRACIEGAY
- (Cmd) orate Four score and
- > seven releases ago
- > our BDFL
- > blah blah blah
- Four score and
- seven releases ago
- our BDFL
- blah blah blah
- (Cmd) & look, a shortcut!
- look, a shortcut!
- (Cmd) show color
- colors: True
- (Cmd) set prompt "---> "
- prompt - was: (Cmd)
- now: --->
- ---> say goodbye
- goodbye
+example/exampleSession.txt:
+
+```text
+(Cmd) help
+
+Documented commands (type help <topic>):
+========================================
+_relative_load edit help list orate py run say shell show
+cmdenvironment eof history load pause quit save set shortcuts speak
+
+(Cmd) help say
+Repeats what you tell me to.
+Usage: speak [options] arg
+
+Options:
+ -h, --help show this help message and exit
+ -p, --piglatin atinLay
+ -s, --shout N00B EMULATION MODE
+ -r REPEAT, --repeat=REPEAT
+ output [n] times
+
+(Cmd) say goodnight, Gracie
+goodnight, Gracie
+(Cmd) say -ps --repeat=5 goodnight, Gracie
+OODNIGHT, GRACIEGAY
+OODNIGHT, GRACIEGAY
+OODNIGHT, GRACIEGAY
+(Cmd) set maxrepeats 5
+maxrepeats - was: 3
+now: 5
+(Cmd) say -ps --repeat=5 goodnight, Gracie
+OODNIGHT, GRACIEGAY
+OODNIGHT, GRACIEGAY
+OODNIGHT, GRACIEGAY
+OODNIGHT, GRACIEGAY
+OODNIGHT, GRACIEGAY
+(Cmd) hi
+-------------------------[1]
+help
+-------------------------[2]
+help say
+-------------------------[3]
+say goodnight, Gracie
+-------------------------[4]
+say -ps --repeat=5 goodnight, Gracie
+-------------------------[5]
+set maxrepeats 5
+-------------------------[6]
+say -ps --repeat=5 goodnight, Gracie
+(Cmd) run 4
+say -ps --repeat=5 goodnight, Gracie
+
+OODNIGHT, GRACIEGAY
+OODNIGHT, GRACIEGAY
+OODNIGHT, GRACIEGAY
+OODNIGHT, GRACIEGAY
+OODNIGHT, GRACIEGAY
+(Cmd) orate Four score and
+> seven releases ago
+> our BDFL
+> blah blah blah
+Four score and
+seven releases ago
+our BDFL
+blah blah blah
+(Cmd) & look, a shortcut!
+look, a shortcut!
+(Cmd) show color
+colors: /(True|False)/
+(Cmd) set prompt "---> "
+prompt - was: (Cmd)
+now: --->
+---> say goodbye
+goodbye
+```
+
+Note how a regular expression `/(True|False)/` is used near the end for output of the **show color** command since
+colored text is currently not available for cmd2 on Windows. Regular expressions can be used anywhere within a
+transcript file simply by embedding them within two forward slashes, `/`.