summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDoug Hellmann <doug.hellmann@dreamhost.com>2012-04-28 19:35:33 -0400
committerDoug Hellmann <doug.hellmann@dreamhost.com>2012-04-28 19:35:33 -0400
commitf658e388965568f6eeca8f7552ab014d09be7880 (patch)
tree3c48cf80197744d9fd25b559dff57081be8cbd7d
parent21ffad9b2c370b6398148d9cf8492ad795286b7c (diff)
downloadcliff-tablib-f658e388965568f6eeca8f7552ab014d09be7880.tar.gz
add --prefix option for shell formatter; add docs for shell formatter
-rw-r--r--cliff/formatters/shell.py9
-rw-r--r--docs/source/history.rst4
-rw-r--r--docs/source/show_commands.rst27
3 files changed, 36 insertions, 4 deletions
diff --git a/cliff/formatters/shell.py b/cliff/formatters/shell.py
index a490550..23f2368 100644
--- a/cliff/formatters/shell.py
+++ b/cliff/formatters/shell.py
@@ -19,6 +19,13 @@ class ShellFormatter(SingleFormatter):
metavar='VARIABLE',
help='specify the variable(s) to include, can be repeated',
)
+ group.add_argument(
+ '--prefix',
+ action='store',
+ default='',
+ dest='prefix',
+ help='add a prefix to all variable names',
+ )
def emit_one(self, column_names, data, stdout, parsed_args):
variable_names = [c.lower().replace(' ', '_')
@@ -27,5 +34,5 @@ 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"\n' % (name, value))
+ stdout.write('%s%s="%s"\n' % (parsed_args.prefix, name, value))
return
diff --git a/docs/source/history.rst b/docs/source/history.rst
index 5df2893..303b9cd 100644
--- a/docs/source/history.rst
+++ b/docs/source/history.rst
@@ -2,6 +2,10 @@
Release History
=================
+dev
+
+ - Add shell formatter for single objects.
+
0.3
- Add ShowOne base class for commands that show details about single
diff --git a/docs/source/show_commands.rst b/docs/source/show_commands.rst
index c3b8914..5ad72de 100644
--- a/docs/source/show_commands.rst
+++ b/docs/source/show_commands.rst
@@ -27,10 +27,10 @@ commands. :class:`ShowOne` adds a command line switch to let the user
specify the formatter they want, so you don't have to do any extra
work in your application.
-PrettyTable
------------
+table
+-----
-The ``PrettyTable`` formatter uses PrettyTable_ to produce output
+The ``table`` formatter uses PrettyTable_ to produce output
formatted for human consumption.
.. _PrettyTable: http://code.google.com/p/prettytable/
@@ -48,6 +48,27 @@ formatted for human consumption.
| Modified Time | 1335569964.0 |
+---------------+--------------+
+shell
+-----
+
+The ``shell`` formatter produces output that can be parsed directly by
+a typical UNIX shell as variable assignments. This avoids extra
+parsing overhead in shell scripts.
+
+::
+
+ (.venv)$ cliffdemo file -f shell setup.py
+ name="setup.py"
+ size="5916"
+ uid="527"
+ gid="501"
+ modified_time="1335655655.0"
+
+ (.venv)$ eval "$(cliffdemo file -f shell --prefix example_ setup.py)"
+ (.venv)$ echo $example_size
+ 5916
+
+
Creating Your Own Formatter
---------------------------