diff options
author | catherine devlin <catherine.devlin@gmail.com> | 2011-08-25 15:24:56 -0400 |
---|---|---|
committer | catherine devlin <catherine.devlin@gmail.com> | 2011-08-25 15:24:56 -0400 |
commit | e05f7527cfd9314ef3aa1c6203d9e4c033f81ae1 (patch) | |
tree | 07c26f5f35acb0a19d0354402cd729f0619730e9 | |
parent | bf78cb82f0f7119058d94330a69455f3cd9980c0 (diff) | |
download | cmd2-hg-e05f7527cfd9314ef3aa1c6203d9e4c033f81ae1.tar.gz |
custom double-redirector fixed
-rwxr-xr-x | cmd2.py | 8 | ||||
-rw-r--r-- | docs/freefeatures.rst | 16 | ||||
-rwxr-xr-x | example/example.py | 1 |
3 files changed, 23 insertions, 2 deletions
@@ -379,6 +379,7 @@ class Cmd(cmd.Cmd): 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 @@ class Cmd(cmd.Cmd): - 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 @@ class Cmd(cmd.Cmd): 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: diff --git a/docs/freefeatures.rst b/docs/freefeatures.rst index fef29ae..8795261 100644 --- a/docs/freefeatures.rst +++ b/docs/freefeatures.rst @@ -76,6 +76,22 @@ As in a Unix shell, output of a command can be redirected: 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/ diff --git a/example/example.py b/example/example.py index 32f20f9..88cefd4 100755 --- a/example/example.py +++ b/example/example.py @@ -7,6 +7,7 @@ class CmdLineApp(Cmd): 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"), |