summaryrefslogtreecommitdiff
path: root/cmd2.py
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2017-02-16 00:05:06 -0500
committerTodd Leonhardt <todd.leonhardt@gmail.com>2017-02-16 00:05:06 -0500
commitd55a618999194ca2076d75a5568184bf9efd3789 (patch)
tree50825ff1c06f29c4463258bda89ab35a29d40429 /cmd2.py
parent9b8bc42a7a6f04d8e83830ba4f87ffe3aa010eb4 (diff)
downloadcmd2-git-d55a618999194ca2076d75a5568184bf9efd3789.tar.gz
Added member boolean flag to disable output redirection and piping.
This addresses Issue #15. Also added a unit test for this new feature and display of it's status in the cmdenvironment command.
Diffstat (limited to 'cmd2.py')
-rwxr-xr-xcmd2.py22
1 files changed, 12 insertions, 10 deletions
diff --git a/cmd2.py b/cmd2.py
index 8540252c..9127269b 100755
--- a/cmd2.py
+++ b/cmd2.py
@@ -502,7 +502,8 @@ class Cmd(cmd.Cmd):
# Attributes which are NOT dynamically settable at runtime
_STOP_AND_EXIT = True # distinguish end of script file from actual exit
_STOP_SCRIPT_NO_EXIT = -999
- allow_cli_args = True
+ allow_cli_args = True # Should arguments passed on the command-line be processed as commands?
+ allow_redirection = True # Should output redirection and pipes be allowed
blankLinesAllowed = False
colorcodes = {'bold': {True: '\x1b[1m', False: '\x1b[22m'},
'cyan': {True: '\x1b[36m', False: '\x1b[39m'},
@@ -636,13 +637,12 @@ class Cmd(cmd.Cmd):
def do_cmdenvironment(self, args):
"""Summary report of interactive parameters."""
self.stdout.write("""
- Commands are %(casesensitive)scase-sensitive.
- Commands may be terminated with: %(terminators)s
- Settable parameters: %(settable)s\n""" %
- {'casesensitive': (self.case_insensitive and 'not ') or '',
- 'terminators': str(self.terminators),
- 'settable': ' '.join(self.settable)
- })
+ Commands are case-sensitive: {}
+ Commands may be terminated with: {}
+ Command-line arguments allowed: {}
+ Output redirection and pipes allowed: {}
+ Settable parameters: {}\n""".format(not self.case_insensitive, str(self.terminators), self.allow_cli_args,
+ self.allow_redirection, ' '.join(self.settable)))
def do_help(self, arg):
"""List available commands with "help" or detailed help with "help cmd"."""
@@ -990,7 +990,8 @@ class Cmd(cmd.Cmd):
if statement.parsed.command not in self.excludeFromHistory:
self.history.append(statement.parsed.raw)
try:
- self.redirect_output(statement)
+ if self.allow_redirection:
+ self.redirect_output(statement)
timestart = datetime.datetime.now()
statement = self.precmd(statement)
stop = self.onecmd(statement)
@@ -998,7 +999,8 @@ class Cmd(cmd.Cmd):
if self.timing:
self.pfeedback('Elapsed: %s' % str(datetime.datetime.now() - timestart))
finally:
- self.restore_output(statement)
+ if self.allow_redirection:
+ self.restore_output(statement)
except EmptyStatement:
return 0
except ValueError as ex: