diff options
-rw-r--r-- | cliff/formatters/shell.py | 9 | ||||
-rw-r--r-- | docs/source/history.rst | 4 | ||||
-rw-r--r-- | docs/source/show_commands.rst | 27 |
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 --------------------------- |