Skip to content

Commit

Permalink
Add support for changing CLI output format per API
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycrosley committed Mar 21, 2019
1 parent 334b5f4 commit 37f957e
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions hug/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@
from wsgiref.simple_server import make_server

import falcon
from falcon import HTTP_METHODS

import hug.defaults
import hug.output_format
from falcon import HTTP_METHODS
from hug import introspect
from hug._async import asyncio, ensure_future
from hug._version import current
Expand Down Expand Up @@ -371,7 +372,7 @@ def error_serializer(request, response, error):

class CLIInterfaceAPI(InterfaceAPI):
"""Defines the CLI interface specific API"""
__slots__ = ('commands', 'error_exit_codes',)
__slots__ = ('commands', 'error_exit_codes', '_output_format')

def __init__(self, api, version='', error_exit_codes=False):
super().__init__(api)
Expand Down Expand Up @@ -407,6 +408,14 @@ def extend(self, cli_api, command_prefix="", sub_command="", **kwargs):
for name, command in cli_api.commands.items():
self.commands["{}{}".format(command_prefix, name)] = command

@property
def output_format(self):
return getattr(self, '_output_format', hug.defaults.cli_output_format)

@output_format.setter
def output_format(self, formatter):
self._output_format = formatter

def __str__(self):
return "{0}\n\nAvailable Commands:{1}\n".format(self.api.doc or self.api.name,
"\n\n\t- " + "\n\t- ".join(self.commands.keys()))
Expand Down

0 comments on commit 37f957e

Please sign in to comment.