summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2018-03-07 19:45:30 -0500
committerTodd Leonhardt <todd.leonhardt@gmail.com>2018-03-07 19:45:30 -0500
commit5cc434b0150e38252bc6eaba91825805db08e5a4 (patch)
tree44501c7be6449ee326058526725933e5feceb52d
parentc18b2a830576db859a37732584a077699039bb33 (diff)
downloadcmd2-git-5cc434b0150e38252bc6eaba91825805db08e5a4.tar.gz
Added broken_pipe_warning attribute of cmd2.Cmd.
It defaults to an empty string. But if a user would like a warning to be printed on a broken pipe error when using poutput() or ppaged(), then they simply set this to the warning they would like printed to sys.stderr.
-rwxr-xr-xcmd2.py19
1 files changed, 11 insertions, 8 deletions
diff --git a/cmd2.py b/cmd2.py
index 900ddcfa..27d00d5f 100755
--- a/cmd2.py
+++ b/cmd2.py
@@ -1136,6 +1136,9 @@ class Cmd(cmd.Cmd):
# Used to keep track of whether we are redirecting or piping output
self.redirecting = False
+ # If this string is non-empty, then this warning message will print if a broken pipe error occurs while printing
+ self.broken_pipe_warning = ''
+
# ----- Methods related to presenting output to the user -----
@property
@@ -1174,10 +1177,10 @@ class Cmd(cmd.Cmd):
self.stdout.write(end)
except BROKEN_PIPE_ERROR:
# This occurs if a command's output is being piped to another process and that process closes before the
- # command is finished. We intentionally don't print a warning message here since we know that stdout
- # will be restored by the _restore_output() method. If you would like your application to print a
- # warning message, then override this method.
- pass
+ # command is finished. If you would like your application to print a warning message, then set the
+ # broken_pipe_warning attribute to the message you want printed.
+ if self.broken_pipe_warning:
+ sys.stderr.write(self.broken_pipe_warning)
def perror(self, errmsg, exception_type=None, traceback_war=True):
""" Print error message to sys.stderr and if debug is true, print an exception Traceback if one exists.
@@ -1254,10 +1257,10 @@ class Cmd(cmd.Cmd):
self.stdout.write(msg_str)
except BROKEN_PIPE_ERROR:
# This occurs if a command's output is being piped to another process and that process closes before the
- # command is finished. We intentionally don't print a warning message here since we know that stdout
- # will be restored by the _restore_output() method. If you would like your application to print a
- # warning message, then override this method.
- pass
+ # command is finished. If you would like your application to print a warning message, then set the
+ # broken_pipe_warning attribute to the message you want printed.
+ if self.broken_pipe_warning:
+ sys.stderr.write(self.broken_pipe_warning)
def colorize(self, val, color):
"""Given a string (``val``), returns that string wrapped in UNIX-style