Revision f76c6bbc kamaki/cli/commands/__init__.py

b/kamaki/cli/commands/__init__.py
36 36
    print_list, print_dict, print_json, print_items, ask_user,
37 37
    filter_dicts_by_dict)
38 38
from kamaki.cli.argument import FlagArgument, ValueArgument
39
from kamaki.cli.errors import CLIInvalidArgument
39 40
from sys import stdin, stdout, stderr
40 41

  
41 42
log = get_logger(__name__)
......
242 243
#  feature classes - inherit them to get special features for your commands
243 244

  
244 245

  
246
class OutputFormatArgument(ValueArgument):
247
    """Accepted output formats: json (default)"""
248

  
249
    formats = ('json', )
250

  
251
    def ___init__(self, *args, **kwargs):
252
        super(OutputFormatArgument, self).___init__(*args, **kwargs)
253

  
254
    @property
255
    def value(self):
256
        return self._value
257

  
258
    @value.setter
259
    def value(self, newvalue):
260
        if not newvalue:
261
            self._value = self.default
262
        elif newvalue.lower() in self.formats:
263
            self._value = newvalue.lower
264
        else:
265
            raise CLIInvalidArgument(
266
                'Invalid value %s for argument %s' % (
267
                    newvalue, '/'.join(self.parsed_name)),
268
                details=['Valid output formats: %s' % ', '.join(self.formats)])
269

  
270

  
245 271
class _optional_output_cmd(object):
246 272

  
247 273
    oo_arguments = dict(
248 274
        with_output=FlagArgument('show response headers', ('--with-output')),
249
        json_output=FlagArgument('show headers in json', ('-j', '--json'))
275
        json_output=FlagArgument(
276
            'show headers in json (DEPRECATED from v0.12,'
277
            ' please use --output-format=json instead)', ('-j', '--json'))
250 278
    )
251 279

  
252 280
    def _optional_output(self, r):
......
259 287
class _optional_json(object):
260 288

  
261 289
    oj_arguments = dict(
262
        json_output=FlagArgument('show headers in json', ('-j', '--json'))
290
        output_format=OutputFormatArgument(
291
            'Show output in chosen output format (%s)' % ', '.join(
292
                OutputFormatArgument.formats),
293
            '--output-format'),
294
        json_output=FlagArgument(
295
            'show output in json (DEPRECATED from v0.12,'
296
            ' please use --output-format instead)', ('-j', '--json'))
263 297
    )
264 298

  
265 299
    def _print(self, output, print_method=print_items, **print_method_kwargs):
266
        if self['json_output']:
300
        if self['json_output'] or self['output_format']:
267 301
            print_json(output, out=self._out)
268 302
        else:
269 303
            print_method_kwargs.setdefault('out', self._out)

Also available in: Unified diff