Mercurial > python-cmd2
changeset 435:bfbe4241bd6b
custom double-redirector fixed
author | catherine.devlin@gmail.com |
---|---|
date | Thu, 25 Aug 2011 15:24:56 -0400 |
parents | 742fd308f0c9 |
children | c4c35f002aef |
files | cmd2.py docs/freefeatures.rst example/example.py |
diffstat | 3 files changed, 23 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/cmd2.py Sun Aug 21 02:39:43 2011 -0400 +++ b/cmd2.py Thu Aug 25 15:24:56 2011 -0400 @@ -379,6 +379,7 @@ debug = False locals_in_py = True kept_state = None + redirector = '>' # for sending output to file settable = stubbornDict(''' prompt colors Colorized output (*nix only) @@ -665,7 +666,10 @@ - args: if "quoted strings /* seem to " start comments? - command: what ''' - outputParser = (pyparsing.Literal('>>') | (pyparsing.WordStart() + '>') | pyparsing.Regex('[^=]>'))('output') + #outputParser = (pyparsing.Literal('>>') | (pyparsing.WordStart() + '>') | pyparsing.Regex('[^=]>'))('output') + outputParser = (pyparsing.Literal(self.redirector *2) | \ + (pyparsing.WordStart() + self.redirector) | \ + pyparsing.Regex('[^=]' + self.redirector))('output') terminatorParser = pyparsing.Or([(hasattr(t, 'parseString') and t) or pyparsing.Literal(t) for t in self.terminators])('terminator') stringEnd = pyparsing.stringEnd ^ '\nEOF' @@ -815,7 +819,7 @@ self.kept_sys = Statekeeper(sys, ('stdout',)) if statement.parsed.outputTo: mode = 'w' - if statement.parsed.output == '>>': + if statement.parsed.output == 2 * self.redirector: mode = 'a' sys.stdout = self.stdout = open(os.path.expanduser(statement.parsed.outputTo), mode) else:
--- a/docs/freefeatures.rst Sun Aug 21 02:39:43 2011 -0400 +++ b/docs/freefeatures.rst Thu Aug 25 15:24:56 2011 -0400 @@ -76,6 +76,22 @@ to paste buffer requires software to be installed on the operating system, pywin32_ on Windows or xclip_ on \*nix. +If your application depends on mathematical syntax, ``>`` may be a bad +choice for redirecting output - it will prevent you from using the +greater-than sign in your actual user commands. You can override your +app's value of ``self.redirector`` to use a different string for output redirection:: + + class MyApp(cmd2.Cmd): + redirector = '->' + +:: + + (Cmd) say line1 -> out.txt + (Cmd) say line2 ->-> out.txt + (Cmd) !cat out.txt + line1 + line2 + .. _pywin32: http://sourceforge.net/projects/pywin32/ .. _xclip: http://www.cyberciti.biz/faq/xclip-linux-insert-files-command-output-intoclipboard/
--- a/example/example.py Sun Aug 21 02:39:43 2011 -0400 +++ b/example/example.py Thu Aug 25 15:24:56 2011 -0400 @@ -7,6 +7,7 @@ multilineCommands = ['orate'] Cmd.shortcuts.update({'&': 'speak'}) maxrepeats = 3 + redirector = '->' Cmd.settable.append('maxrepeats Max number of `--repeat`s allowed') @options([make_option('-p', '--piglatin', action="store_true", help="atinLay"),