From ec2c1571aff98decdc3d1a80d0b5487d4ea6e176 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi?= Date: Fri, 3 Jul 2020 12:46:52 +0200 Subject: [PATCH] CLI: --output option for compile-contract --- src/bin/cli.ml | 14 +++++++++--- src/bin/cli_helpers.ml | 36 +++++++++++++++++------------- src/bin/cli_helpers.mli | 4 ++-- src/bin/expect_tests/help_tests.ml | 4 ++++ 4 files changed, 37 insertions(+), 21 deletions(-) diff --git a/src/bin/cli.ml b/src/bin/cli.ml index 62bb17c79..8ffb538f5 100644 --- a/src/bin/cli.ml +++ b/src/bin/cli.ml @@ -124,6 +124,14 @@ let display_format = human_readable info +let output_file = + let open Arg in + let info = + let docv = "OUTPUT_FILE" in + let doc = "$(docv) if used, prints the output into the specified file instead of stdout" in + info ~docv ~doc ["output" ; "output-file"] in + value @@ opt (some string) None info + let michelson_code_format = let open Arg in let info = @@ -150,15 +158,15 @@ module Decompile = Ligo.Decompile module Run = Ligo.Run.Of_michelson let compile_file = - let f source_file entry_point syntax display_format disable_typecheck michelson_format = - return_result ~display_format (Tezos_utils.Michelson.michelson_format michelson_format) @@ + let f source_file entry_point syntax display_format disable_typecheck michelson_format output_file = + return_result ~output_file ~display_format (Tezos_utils.Michelson.michelson_format michelson_format) @@ let%bind typed,_ = Compile.Utils.type_file source_file syntax (Contract entry_point) in let%bind mini_c = Compile.Of_typed.compile typed in let%bind michelson = Compile.Of_mini_c.aggregate_and_compile_contract mini_c entry_point in Compile.Of_michelson.build_contract ~disable_typecheck michelson in let term = - Term.(const f $ source_file 0 $ entry_point 1 $ syntax $ display_format $ disable_michelson_typechecking $ michelson_code_format) in + Term.(const f $ source_file 0 $ entry_point 1 $ syntax $ display_format $ disable_michelson_typechecking $ michelson_code_format $ output_file) in let cmdname = "compile-contract" in let doc = "Subcommand: Compile a contract." in (Term.ret term , Term.info ~doc cmdname) diff --git a/src/bin/cli_helpers.ml b/src/bin/cli_helpers.ml index 46a948220..1b6ded91e 100644 --- a/src/bin/cli_helpers.ml +++ b/src/bin/cli_helpers.ml @@ -4,21 +4,25 @@ open Main.Display let return_good v = `Ok v let return_bad v = `Error (false, Format.asprintf "@[error@ %s@]" v) -let toplevel : display_format:ex_display_format -> displayable -> ('value, Main_errors.Types.all) result -> unit Term.ret = - fun ~display_format disp value -> - let (Ex_display_format t) = display_format in - let as_str : string = - match t with - | Human_readable -> convert ~display_format:t disp ; - | Dev -> convert ~display_format:t disp ; - | Json -> Yojson.to_string @@ convert ~display_format:t disp - in - match value with - | Ok _ -> return_good @@ - Format.fprintf Format.std_formatter "%s\n" as_str - | Error _ -> return_bad as_str +let toplevel : ?output_file:string option -> display_format:ex_display_format -> displayable -> ('value, Main_errors.Types.all) result -> unit Term.ret = + fun ?(output_file=None) ~display_format disp value -> + let (Ex_display_format t) = display_format in + let as_str : string = + match t with + | Human_readable -> convert ~display_format:t disp ; + | Dev -> convert ~display_format:t disp ; + | Json -> Yojson.to_string @@ convert ~display_format:t disp + in + match value with + | Ok _ -> + let fmt = match output_file with + | Some file_path -> Format.formatter_of_out_channel @@ open_out file_path + | None -> Format.std_formatter + in + return_good @@ Format.fprintf fmt "%s\n" as_str + | Error _ -> return_bad as_str -let return_result : display_format:ex_display_format -> 'value format -> ('value, Main_errors.Types.all) result -> unit Term.ret = - fun ~display_format value_format value -> +let return_result : ?output_file:string option -> display_format:ex_display_format -> 'value format -> ('value, Main_errors.Types.all) result -> unit Term.ret = + fun ?(output_file=None) ~display_format value_format value -> let format = bind_format value_format Main.Formatter.error_format in - toplevel ~display_format (Displayable {value ; format}) value \ No newline at end of file + toplevel ~output_file ~display_format (Displayable {value ; format}) value \ No newline at end of file diff --git a/src/bin/cli_helpers.mli b/src/bin/cli_helpers.mli index a863f45a9..5dcb5966e 100644 --- a/src/bin/cli_helpers.mli +++ b/src/bin/cli_helpers.mli @@ -1,5 +1,5 @@ open Cmdliner open Display -val toplevel : display_format:ex_display_format -> displayable -> ('value, Main_errors.Types.all) result -> unit Term.ret -val return_result : display_format:ex_display_format -> 'value format -> ('value, Main_errors.Types.all) result -> unit Term.ret \ No newline at end of file +val toplevel : ?output_file:string option -> display_format:ex_display_format -> displayable -> ('value, Main_errors.Types.all) result -> unit Term.ret +val return_result : ?output_file:string option -> display_format:ex_display_format -> 'value format -> ('value, Main_errors.Types.all) result -> unit Term.ret \ No newline at end of file diff --git a/src/bin/expect_tests/help_tests.ml b/src/bin/expect_tests/help_tests.ml index abb06515d..6dcbb08e9 100644 --- a/src/bin/expect_tests/help_tests.ml +++ b/src/bin/expect_tests/help_tests.ml @@ -246,6 +246,10 @@ let%expect_test _ = compile-contract for the resulting Michelson. Available formats are 'text' (default), 'json' and 'hex'. + --output-file=OUTPUT_FILE, --output=OUTPUT_FILE + OUTPUT_FILE if used, prints the output into the specified file + instead of stdout + -s SYNTAX, --syntax=SYNTAX (absent=auto) SYNTAX is the syntax that will be used. Currently supported syntaxes are "pascaligo", "cameligo" and "reasonligo". By default,