99 import yaml
1010except ImportError :
1111 yaml = None
12+ try :
13+ import jsonlines
14+ except ImportError :
15+ jsonlines = None
1216
1317from . import converters
1418
@@ -185,6 +189,12 @@ def write_results(results, output):
185189 ensure_ascii = False )
186190 if output == 'yaml' :
187191 results = yaml .safe_dump (results )
192+ if output == 'ndjson' :
193+ # NDJSON support seems like a hack. There has to be a better way
194+ writer = jsonlines .Writer (sys .stdout )
195+ writer .write_all (results )
196+ writer .close ()
197+ return
188198
189199 sys .stdout .write (results )
190200 if not results .endswith ('\n ' ):
@@ -200,7 +210,7 @@ def do_it(args):
200210 method = 'GET'
201211
202212 usage = ('usage: cli4 '
203- + '[-V|--version] [-h|--help] [-v|--verbose] [-q|--quiet] [-j|--json] [-y|--yaml] '
213+ + '[-V|--version] [-h|--help] [-v|--verbose] [-q|--quiet] [-j|--json] [-y|--yaml] [-n|ndjson] '
204214 + '[-r|--raw] '
205215 + '[-d|--dump] '
206216 + '[--get|--patch|--post|--put|--delete] '
@@ -212,7 +222,7 @@ def do_it(args):
212222 'VhvqjyrdGPOUD' ,
213223 [
214224 'version' ,
215- 'help' , 'verbose' , 'quiet' , 'json' , 'yaml' ,
225+ 'help' , 'verbose' , 'quiet' , 'json' , 'yaml' , 'ndjson' ,
216226 'raw' ,
217227 'dump' ,
218228 'get' , 'patch' , 'post' , 'put' , 'delete'
@@ -234,6 +244,10 @@ def do_it(args):
234244 if yaml is None :
235245 exit ('cli4: install yaml support' )
236246 output = 'yaml'
247+ elif opt in ('-n' , '--ndjson' ):
248+ if jsonlines is None :
249+ exit ('cli4: install jsonlines support' )
250+ output = 'ndjson'
237251 elif opt in ('-r' , '--raw' ):
238252 raw = True
239253 elif opt in ('-d' , '--dump' ):
0 commit comments