From 39164e4143c6e86fc2e4b83d7a82302db7ba8e59 Mon Sep 17 00:00:00 2001 From: Benjamin Z Date: Thu, 30 Sep 2021 23:23:02 +0700 Subject: [PATCH] Add -o flag to output directly to file (#2452) Co-authored-by: Charles Cooper --- vyper/cli/vyper_compile.py | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/vyper/cli/vyper_compile.py b/vyper/cli/vyper_compile.py index d70d952cb67..4a4c23ecee6 100755 --- a/vyper/cli/vyper_compile.py +++ b/vyper/cli/vyper_compile.py @@ -54,6 +54,17 @@ def _parse_cli_args(): return _parse_args(sys.argv[1:]) +def _cli_helper(f, output_formats, compiled): + if output_formats == ("combined_json",): + print(json.dumps(compiled), file=f) + return + + for contract_data in compiled.values(): + for data in contract_data.values(): + if isinstance(data, (list, dict)): + print(json.dumps(data), file=f) + else: + print(data, file=f) def _parse_args(argv): warnings.simplefilter("always") @@ -109,6 +120,9 @@ def _parse_args(argv): parser.add_argument( "-p", help="Set the root path for contract imports", default=".", dest="root_folder" ) + parser.add_argument( + "-o", help="Set the output path", dest="output_path" + ) args = parser.parse_args(argv) @@ -135,16 +149,13 @@ def _parse_args(argv): args.ovm, ) - if output_formats == ("combined_json",): - print(json.dumps(compiled)) - return - for contract_data in compiled.values(): - for data in contract_data.values(): - if isinstance(data, (list, dict)): - print(json.dumps(data)) - else: - print(data) + if args.output_path: + with open(args.output_path,"w") as f: + _cli_helper(f, output_formats, compiled) + else: + f = sys.stdout + _cli_helper(f, output_formats, compiled) def uniq(seq: Iterable[T]) -> Iterator[T]: