summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcatherine devlin <catherine.devlin@gmail.com>2011-08-25 15:24:56 -0400
committercatherine devlin <catherine.devlin@gmail.com>2011-08-25 15:24:56 -0400
commite05f7527cfd9314ef3aa1c6203d9e4c033f81ae1 (patch)
tree07c26f5f35acb0a19d0354402cd729f0619730e9
parentbf78cb82f0f7119058d94330a69455f3cd9980c0 (diff)
downloadcmd2-hg-e05f7527cfd9314ef3aa1c6203d9e4c033f81ae1.tar.gz
custom double-redirector fixed
-rwxr-xr-xcmd2.py8
-rw-r--r--docs/freefeatures.rst16
-rwxr-xr-xexample/example.py1
3 files changed, 23 insertions, 2 deletions
diff --git a/cmd2.py b/cmd2.py
index 332d9b3..e0ebca3 100755
--- a/cmd2.py
+++ b/cmd2.py
@@ -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"),