summaryrefslogtreecommitdiff
path: root/cliff
diff options
context:
space:
mode:
authorDoug Hellmann <doug.hellmann@dreamhost.com>2012-04-28 19:26:07 -0400
committerDoug Hellmann <doug.hellmann@dreamhost.com>2012-04-28 19:26:07 -0400
commit8ba10466ffafd6f9c9fb15a42bcd83b92c3c655a (patch)
treee3b15e3dd8853e640387e17667f0c91eb06ed275 /cliff
parent16c1759f81ff3f7670722587c2c1d05b4fdb4ba8 (diff)
downloadcliff-tablib-8ba10466ffafd6f9c9fb15a42bcd83b92c3c655a.tar.gz
add shell output formatter for single items
Diffstat (limited to 'cliff')
-rw-r--r--cliff/formatters/shell.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/cliff/formatters/shell.py b/cliff/formatters/shell.py
new file mode 100644
index 0000000..a490550
--- /dev/null
+++ b/cliff/formatters/shell.py
@@ -0,0 +1,31 @@
+"""Output formatters using shell syntax.
+"""
+
+from .base import SingleFormatter
+
+
+class ShellFormatter(SingleFormatter):
+
+ def add_argument_group(self, parser):
+ group = parser.add_argument_group(
+ title='shell formatter',
+ description='Print values in a format a UNIX shell can parse (variable="value")',
+ )
+ group.add_argument(
+ '--variable',
+ action='append',
+ default=[],
+ dest='variables',
+ metavar='VARIABLE',
+ help='specify the variable(s) to include, can be repeated',
+ )
+
+ def emit_one(self, column_names, data, stdout, parsed_args):
+ variable_names = [c.lower().replace(' ', '_')
+ for c in column_names
+ ]
+ 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"\n' % (name, value))
+ return