summaryrefslogtreecommitdiff
path: root/cliff
diff options
context:
space:
mode:
authorDoug Hellmann <doug.hellmann@gmail.com>2012-04-22 16:26:02 -0700
committerDoug Hellmann <doug.hellmann@gmail.com>2012-04-22 16:26:02 -0700
commit0f9c634b8dd3b7c88d8aa5539e91c5c919a1de77 (patch)
treef1294a68205bb2bcff9d6e105910aaa71a4ff4ba /cliff
parent6b4add15ab5a3675da90b5ba4f4e7997be100f60 (diff)
downloadcliff-0f9c634b8dd3b7c88d8aa5539e91c5c919a1de77.tar.gz
add a csv formatter for list apps
Diffstat (limited to 'cliff')
-rw-r--r--cliff/formatters/commaseparated.py33
-rw-r--r--cliff/lister.py4
2 files changed, 36 insertions, 1 deletions
diff --git a/cliff/formatters/commaseparated.py b/cliff/formatters/commaseparated.py
new file mode 100644
index 0000000..f061b59
--- /dev/null
+++ b/cliff/formatters/commaseparated.py
@@ -0,0 +1,33 @@
+"""Output formatters using csv format.
+"""
+
+import csv
+
+from .base import ListFormatter
+
+
+class CSVLister(ListFormatter):
+
+ QUOTE_MODES = {
+ 'all': csv.QUOTE_ALL,
+ 'minimal': csv.QUOTE_MINIMAL,
+ 'nonnumeric': csv.QUOTE_NONNUMERIC,
+ 'none': csv.QUOTE_NONE,
+ }
+
+ def add_argument_group(self, parser):
+ group = parser.add_argument_group('CSV Formatter')
+ group.add_argument(
+ '--quote',
+ choices=sorted(self.QUOTE_MODES.keys()),
+ dest='quote_mode',
+ default='nonnumeric',
+ help='when to include quotes, defaults to nonnumeric',
+ )
+
+ def emit_list(self, column_names, data, stdout, parsed_args):
+ writer = csv.writer(stdout, quoting=self.QUOTE_MODES[parsed_args.quote_mode])
+ writer.writerow(column_names)
+ for row in data:
+ writer.writerow(row)
+ return
diff --git a/cliff/lister.py b/cliff/lister.py
index 01f25ef..15ac83a 100644
--- a/cliff/lister.py
+++ b/cliff/lister.py
@@ -27,7 +27,9 @@ class Lister(Command):
description='List output formatter options',
)
formatter_choices = sorted(self.formatters.keys())
- formatter_default = formatter_choices[0]
+ formatter_default = 'table'
+ if formatter_default not in formatter_choices:
+ formatter_default = formatter_choices[0]
formatter_group.add_argument(
'-f', '--format',
dest='formatter',