mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-30 18:09:59 +03:00
generateschema: Add OUTPUT positional argument
It's helpful to be able to write this stuff to a file rather than stdout. Allow that, albeit it in an opt-in manner. Signed-off-by: Stephen Finucane <stephen@that.guru>
This commit is contained in:
parent
9d001cd84c
commit
306ddd19f5
|
@ -1,3 +1,6 @@
|
|||
import argparse
|
||||
import sys
|
||||
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
from rest_framework.compat import coreapi
|
||||
|
@ -15,6 +18,9 @@ class Command(BaseCommand):
|
|||
parser.add_argument('--url', dest="url", default=None, type=str)
|
||||
parser.add_argument('--description', dest="description", default=None, type=str)
|
||||
parser.add_argument('--format', dest="format", choices=['openapi', 'openapi-json', 'corejson'], default='openapi', type=str)
|
||||
parser.add_argument(
|
||||
'output',
|
||||
nargs='?', type=argparse.FileType('w'), default=sys.stdout)
|
||||
|
||||
def handle(self, *args, **options):
|
||||
assert coreapi is not None, 'coreapi must be installed.'
|
||||
|
@ -29,7 +35,7 @@ class Command(BaseCommand):
|
|||
|
||||
renderer = self.get_renderer(options['format'])
|
||||
output = renderer.render(schema, renderer_context={})
|
||||
self.stdout.write(output.decode('utf-8'))
|
||||
options['output'].write(output.decode('utf-8'))
|
||||
|
||||
def get_renderer(self, format):
|
||||
return {
|
||||
|
|
Loading…
Reference in New Issue
Block a user