Skip to content

poutput() can not print a zero. #222

@kotfu

Description

@kotfu

Say you built an app which subclasses cmd2. Say you wanted to have an 'exit_code' command in your app which printed out the exit code of the last command processed. Say you wanted to output an exit code of zero, using the poutput() function. It won't print it.

Here's the poutput() function:

cmd2/cmd2.py

Lines 588 to 599 in 5d5675b

if msg:
try:
msg_str = '{}'.format(msg)
self.stdout.write(msg_str)
if not msg_str.endswith(end):
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

If msg evaluates to false, then nothing is output. An empty string, and a zero both evaluate to false.

Recommended fix:

if msg is not None:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions