CLI: --output option for compile-contract

This commit is contained in:
Rémi 2020-07-03 12:46:52 +02:00
parent ab773a56a4
commit ec2c1571af
4 changed files with 37 additions and 21 deletions

View File

@ -124,6 +124,14 @@ let display_format =
human_readable human_readable
info 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 michelson_code_format =
let open Arg in let open Arg in
let info = let info =
@ -150,15 +158,15 @@ module Decompile = Ligo.Decompile
module Run = Ligo.Run.Of_michelson module Run = Ligo.Run.Of_michelson
let compile_file = let compile_file =
let f source_file entry_point syntax display_format disable_typecheck michelson_format = let f source_file entry_point syntax display_format disable_typecheck michelson_format output_file =
return_result ~display_format (Tezos_utils.Michelson.michelson_format michelson_format) @@ 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 typed,_ = Compile.Utils.type_file source_file syntax (Contract entry_point) in
let%bind mini_c = Compile.Of_typed.compile typed 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 let%bind michelson = Compile.Of_mini_c.aggregate_and_compile_contract mini_c entry_point in
Compile.Of_michelson.build_contract ~disable_typecheck michelson Compile.Of_michelson.build_contract ~disable_typecheck michelson
in in
let term = 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 cmdname = "compile-contract" in
let doc = "Subcommand: Compile a contract." in let doc = "Subcommand: Compile a contract." in
(Term.ret term , Term.info ~doc cmdname) (Term.ret term , Term.info ~doc cmdname)

View File

@ -4,8 +4,8 @@ open Main.Display
let return_good v = `Ok v let return_good v = `Ok v
let return_bad v = `Error (false, Format.asprintf "@[<hv>error@ %s@]" v) let return_bad v = `Error (false, Format.asprintf "@[<hv>error@ %s@]" v)
let toplevel : display_format:ex_display_format -> displayable -> ('value, Main_errors.Types.all) result -> unit Term.ret = let toplevel : ?output_file:string option -> display_format:ex_display_format -> displayable -> ('value, Main_errors.Types.all) result -> unit Term.ret =
fun ~display_format disp value -> fun ?(output_file=None) ~display_format disp value ->
let (Ex_display_format t) = display_format in let (Ex_display_format t) = display_format in
let as_str : string = let as_str : string =
match t with match t with
@ -14,11 +14,15 @@ let toplevel : display_format:ex_display_format -> displayable -> ('value, Main_
| Json -> Yojson.to_string @@ convert ~display_format:t disp | Json -> Yojson.to_string @@ convert ~display_format:t disp
in in
match value with match value with
| Ok _ -> return_good @@ | Ok _ ->
Format.fprintf Format.std_formatter "%s\n" as_str 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 | Error _ -> return_bad as_str
let return_result : display_format:ex_display_format -> 'value format -> ('value, Main_errors.Types.all) result -> unit Term.ret = let return_result : ?output_file:string option -> display_format:ex_display_format -> 'value format -> ('value, Main_errors.Types.all) result -> unit Term.ret =
fun ~display_format value_format value -> fun ?(output_file=None) ~display_format value_format value ->
let format = bind_format value_format Main.Formatter.error_format in let format = bind_format value_format Main.Formatter.error_format in
toplevel ~display_format (Displayable {value ; format}) value toplevel ~output_file ~display_format (Displayable {value ; format}) value

View File

@ -1,5 +1,5 @@
open Cmdliner open Cmdliner
open Display open Display
val toplevel : display_format:ex_display_format -> displayable -> ('value, Main_errors.Types.all) result -> unit Term.ret val toplevel : ?output_file:string option -> 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 val return_result : ?output_file:string option -> display_format:ex_display_format -> 'value format -> ('value, Main_errors.Types.all) result -> unit Term.ret

View File

@ -246,6 +246,10 @@ let%expect_test _ =
compile-contract for the resulting Michelson. Available formats compile-contract for the resulting Michelson. Available formats
are 'text' (default), 'json' and 'hex'. 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) -s SYNTAX, --syntax=SYNTAX (absent=auto)
SYNTAX is the syntax that will be used. Currently supported SYNTAX is the syntax that will be used. Currently supported
syntaxes are "pascaligo", "cameligo" and "reasonligo". By default, syntaxes are "pascaligo", "cameligo" and "reasonligo". By default,