diff options
-rw-r--r-- | cliff/formatters/shell.py | 7 | ||||
-rw-r--r-- | cliff/tests/test_formatters_shell.py | 16 |
2 files changed, 20 insertions, 3 deletions
diff --git a/cliff/formatters/shell.py b/cliff/formatters/shell.py index 385d48c..fd4f29e 100644 --- a/cliff/formatters/shell.py +++ b/cliff/formatters/shell.py @@ -3,6 +3,8 @@ from .base import SingleFormatter +import six + class ShellFormatter(SingleFormatter): @@ -34,6 +36,7 @@ class ShellFormatter(SingleFormatter): desired_columns = parsed_args.variables for name, value in zip(variable_names, data): if name in desired_columns or not desired_columns: - stdout.write('%s%s="%s"\n' % (parsed_args.prefix, name, - value.replace('"', '\\"'))) + if isinstance(value, six.string_types): + value = value.replace('"', '\\"') + stdout.write('%s%s="%s"\n' % (parsed_args.prefix, name, value)) return diff --git a/cliff/tests/test_formatters_shell.py b/cliff/tests/test_formatters_shell.py index 93452ba..e6813e5 100644 --- a/cliff/tests/test_formatters_shell.py +++ b/cliff/tests/test_formatters_shell.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -from six import StringIO +from six import StringIO, text_type from cliff.formatters import shell @@ -18,3 +18,17 @@ def test_shell_formatter(): sf.emit_one(c, d, output, args) actual = output.getvalue() assert expected == actual + + +def test_shell_formatter_with_non_string_values(): + sf = shell.ShellFormatter() + c = ('a', 'b', 'c', 'd', 'e') + d = (True, False, 100, '"esc"', text_type('"esc"')) + expected = 'a="True"\nb="False"\nc="100"\nd="\\"esc\\""\ne="\\"esc\\""\n' + output = StringIO() + args = mock.Mock() + args.variables = ['a', 'b', 'c', 'd', 'e'] + args.prefix = '' + sf.emit_one(c, d, output, args) + actual = output.getvalue() + assert expected == actual |