|
5 | 5 | """ |
6 | 6 |
|
7 | 7 | import argparse |
| 8 | +import io |
8 | 9 | import json |
9 | 10 | import sys |
10 | 11 | from collections import OrderedDict |
@@ -138,20 +139,33 @@ def main(argv=None): |
138 | 139 | args = _parser().parse_args(argv) |
139 | 140 |
|
140 | 141 | format2_path = args.input_path |
141 | | - output_path = args.output_path or (format2_path + ".gxwf.yml") |
| 142 | + output_path = args.output or args.output_path |
142 | 143 | with open(format2_path) as f: |
143 | 144 | native_workflow_dict = json.load(f) |
144 | 145 |
|
145 | 146 | as_dict = from_galaxy_native(native_workflow_dict, compact=args.compact) |
146 | | - with open(output_path, "w") as f: |
147 | | - ordered_dump(as_dict, f) |
| 147 | + |
| 148 | + if args.json_output: |
| 149 | + output_text = json.dumps(as_dict, indent=4) + "\n" |
| 150 | + else: |
| 151 | + stream = io.StringIO() |
| 152 | + ordered_dump(as_dict, stream) |
| 153 | + output_text = stream.getvalue() |
| 154 | + |
| 155 | + if output_path: |
| 156 | + with open(output_path, "w") as f: |
| 157 | + f.write(output_text) |
| 158 | + else: |
| 159 | + sys.stdout.write(output_text) |
148 | 160 |
|
149 | 161 |
|
150 | 162 | def _parser(): |
151 | 163 | parser = argparse.ArgumentParser(description=SCRIPT_DESCRIPTION) |
152 | 164 | parser.add_argument("input_path", metavar="INPUT", type=str, help="input workflow path (.ga)") |
153 | | - parser.add_argument("output_path", metavar="OUTPUT", type=str, nargs="?", help="output workflow path (.gxfw.yml)") |
154 | | - parser.add_argument("--compact", action="store_true", help="Generate compact workflow without position information") |
| 165 | + parser.add_argument("output_path", metavar="OUTPUT", type=str, nargs="?", help="output workflow path (.gxwf.yml)") |
| 166 | + parser.add_argument("--output", "-o", help="output file (default: stdout)") |
| 167 | + parser.add_argument("--compact", action="store_true", help="generate compact workflow without position information") |
| 168 | + parser.add_argument("--json", dest="json_output", action="store_true", help="output JSON instead of YAML") |
155 | 169 | return parser |
156 | 170 |
|
157 | 171 |
|
|
0 commit comments