|
4 | 4 | import argparse
|
5 | 5 | import json
|
6 | 6 | from operator import itemgetter
|
| 7 | +import pathlib |
| 8 | +import sys |
7 | 9 |
|
8 | 10 | from traitlets import (CaselessStrEnum, Unicode, Tuple, List, Bool, CFloat,
|
9 | 11 | Float, CInt, Int, Instance, Dict, Bytes, Any)
|
@@ -196,16 +198,28 @@ def create_markdown(spec):
|
196 | 198 | parser = argparse.ArgumentParser(description='Description of your program')
|
197 | 199 | parser.add_argument('-f', '--format', choices=['json', 'json-pretty', 'markdown'],
|
198 | 200 | help='Format to generate', default='json')
|
| 201 | + parser.add_argument('output', nargs='?', type=pathlib.Path) |
199 | 202 | args = parser.parse_args()
|
200 | 203 | format = args.format
|
201 | 204 |
|
| 205 | + if args.output: |
| 206 | + args.output.parent.mkdir(exist_ok=True) |
| 207 | + output = open(args.output, mode='w', encoding='utf8') |
| 208 | + else: |
| 209 | + output = sys.stdout |
| 210 | + |
202 | 211 | widgets_to_document = sorted(widgets.Widget._widget_types.items())
|
203 | 212 | spec = create_spec(widgets_to_document)
|
204 |
| - if format == 'json': |
205 |
| - print(json.dumps(spec, sort_keys=True)) |
206 |
| - elif format == 'json-pretty': |
207 |
| - print(json.dumps(spec, sort_keys=True, |
208 |
| - indent=2, separators=(',', ': '))) |
209 |
| - elif format == 'markdown': |
210 |
| - # We go through the json engine to convert tuples to lists, etc. |
211 |
| - print(create_markdown(json.loads(json.dumps(spec)))) |
| 213 | + try: |
| 214 | + if format == 'json': |
| 215 | + json.dump(spec, output, sort_keys=True) |
| 216 | + elif format == 'json-pretty': |
| 217 | + json.dump(spec, output, sort_keys=True, |
| 218 | + indent=2, separators=(',', ': ')) |
| 219 | + elif format == 'markdown': |
| 220 | + # We go through the json engine to convert tuples to lists, etc. |
| 221 | + output.write(create_markdown(json.loads(json.dumps(spec)))) |
| 222 | + output.write('\n') |
| 223 | + finally: |
| 224 | + if args.output: |
| 225 | + output.close() |
0 commit comments