ligo/src/bin/cli.ml

122 lines
4.1 KiB
OCaml
Raw Normal View History

2019-05-12 20:56:22 +00:00
open Cmdliner
open Trace
2019-06-06 17:37:46 +00:00
let error_pp out (e : error) =
let open JSON_string_utils in
let message =
let opt = e |> member "message" |> string in
let msg = Option.unopt ~default:"" opt in
2019-06-06 20:49:36 +00:00
if msg = ""
then ""
else ": " ^ msg in
2019-06-06 17:37:46 +00:00
let error_code =
let error_code = e |> member "error_code" in
match error_code with
| `Null -> ""
| _ -> " (" ^ (J.to_string error_code) ^ ")" in
let title =
let opt = e |> member "title" |> string in
Option.unopt ~default:"" opt in
let data =
let data = e |> member "data" in
match data with
| `Null -> ""
| _ -> " " ^ (J.to_string data) ^ "\n" in
2019-06-06 20:49:36 +00:00
let infos =
let infos = e |> member "infos" in
match infos with
| `Null -> ""
| _ -> " " ^ (J.to_string infos) ^ "\n" in
Format.fprintf out "%s%s%s.\n%s%s" title error_code message data infos
2019-06-06 17:37:46 +00:00
2019-05-12 20:56:22 +00:00
let toplevel x =
match x with
| Trace.Ok ((), annotations) -> ignore annotations; ()
2019-06-06 17:37:46 +00:00
| Error ss -> (
2019-06-03 11:07:24 +00:00
Format.printf "%a%!" error_pp (ss ())
2019-06-06 17:37:46 +00:00
)
2019-05-12 20:56:22 +00:00
let main =
let term = Term.(const print_endline $ const "Ligo needs a command. Do ligo --help") in
(term , Term.info "ligo")
2019-05-29 22:09:40 +00:00
let source =
let open Arg in
let info =
let docv = "SOURCE_FILE" in
let doc = "$(docv) is the path to the .ligo file of the contract." in
info ~docv ~doc [] in
required @@ pos 0 (some string) None info
let entry_point =
let open Arg in
let info =
let docv = "ENTRY_POINT" in
let doc = "$(docv) is entry-point that will be compiled." in
info ~docv ~doc [] in
2019-06-06 09:30:29 +00:00
required @@ pos 1 (some string) (Some "main") info
2019-05-29 22:09:40 +00:00
let expression =
let open Arg in
let docv = "EXPRESSION" in
let doc = "$(docv) is the expression that will be compiled." in
let info = info ~docv ~doc [] in
required @@ pos 2 (some string) None info
let syntax =
let open Arg in
let info =
let docv = "SYNTAX" in
let doc = "$(docv) is the syntax that will be used. Currently supported syntaxes are \"pascaligo\" and \"cameligo\". \"pascaligo\" is the default." in
2019-06-06 09:30:29 +00:00
info ~docv ~doc ["syntax" ; "s"] in
2019-05-29 22:09:40 +00:00
value @@ opt string "pascaligo" info
2019-05-12 20:56:22 +00:00
let compile_file =
2019-05-29 22:09:40 +00:00
let f source entry_point syntax =
2019-05-12 20:56:22 +00:00
toplevel @@
let%bind contract =
2019-06-05 19:16:54 +02:00
trace (simple_info "compiling contract to michelson") @@
2019-06-01 11:51:49 +00:00
Ligo.Run.compile_contract_file source entry_point syntax in
2019-06-06 20:49:36 +00:00
Format.printf "%s\n" contract ;
2019-05-12 20:56:22 +00:00
ok ()
in
let term =
2019-05-29 22:09:40 +00:00
Term.(const f $ source $ entry_point $ syntax) in
2019-06-05 19:17:42 +02:00
let cmdname = "compile-contract" in
let docs = "Subcommand: compile a contract. See `ligo " ^ cmdname ^ " --help' for a list of options specific to this subcommand." in
(term , Term.info ~docs cmdname)
2019-05-12 20:56:22 +00:00
let compile_parameter =
2019-05-29 22:09:40 +00:00
let f source entry_point expression syntax =
2019-05-12 20:56:22 +00:00
toplevel @@
let%bind value =
trace (simple_error "compile-input") @@
2019-06-01 11:51:49 +00:00
Ligo.Run.compile_contract_parameter source entry_point expression syntax in
2019-06-06 20:49:36 +00:00
Format.printf "%s\n" value;
2019-05-12 20:56:22 +00:00
ok ()
in
let term =
2019-05-29 22:09:40 +00:00
Term.(const f $ source $ entry_point $ expression $ syntax) in
2019-06-05 19:17:42 +02:00
let cmdname = "compile-parameter" in
let docs = "Subcommand: compile parameters to a michelson expression. The resulting michelson expression can be passed as an argument in a transaction which calls a contract. See `ligo " ^ cmdname ^ " --help' for a list of options specific to this subcommand." in
(term , Term.info ~docs cmdname)
2019-05-12 20:56:22 +00:00
let compile_storage =
2019-05-29 22:09:40 +00:00
let f source entry_point expression syntax =
2019-05-12 20:56:22 +00:00
toplevel @@
let%bind value =
trace (simple_error "compile-storage") @@
2019-06-01 11:51:49 +00:00
Ligo.Run.compile_contract_storage source entry_point expression syntax in
2019-06-06 20:49:36 +00:00
Format.printf "%s\n" value;
2019-05-12 20:56:22 +00:00
ok ()
in
let term =
2019-05-29 22:09:40 +00:00
Term.(const f $ source $ entry_point $ expression $ syntax) in
2019-06-05 19:17:42 +02:00
let cmdname = "compile-storage" in
let docs = "Subcommand: compile an initial storage in ligo syntax to a michelson expression. The resulting michelson expression can be passed as an argument in a transaction which originates a contract. See `ligo " ^ cmdname ^ " --help' for a list of options specific to this subcommand." in
(term , Term.info ~docs cmdname)
2019-05-12 20:56:22 +00:00
let () = Term.exit @@ Term.eval_choice main [compile_file ; compile_parameter ; compile_storage]