make better cli

This commit is contained in:
Galfour 2019-04-23 15:17:28 +00:00
parent 097c5ca0fb
commit ba9b8c4a68
2 changed files with 66 additions and 40 deletions

View File

@ -1,3 +1,4 @@
open Cmdliner
open Trace open Trace
let toplevel x = let toplevel x =
@ -6,44 +7,68 @@ let toplevel x =
| Errors ss -> | Errors ss ->
Format.printf "Errors: %a\n%!" errors_pp @@ List.map (fun f -> f()) ss Format.printf "Errors: %a\n%!" errors_pp @@ List.map (fun f -> f()) ss
let main () = let main =
let l = Array.length Sys.argv in let term = Term.(const print_endline $ const "Ligo needs a command. Do ligo --help") in
let%bind () = (term , Term.info "ligo")
if l < 2
then simple_fail "Pass a command" let compile_file =
else ok () in let f source entry_point =
let command = Sys.argv.(1) in toplevel @@
match command with
| "compile" -> (
let sub_command = Sys.argv.(2) in
match sub_command with
| "file" -> (
let%bind () =
trace_strong (simple_error "bad number of args") @@
Assert.assert_equal_int 5 l in
let source = Sys.argv.(3) in
let entry_point = Sys.argv.(4) in
let%bind contract = let%bind contract =
trace (simple_error "compile michelson") @@ trace (simple_error "compile michelson") @@
Ligo.Contract.compile_contract_file source entry_point in Ligo.Contract.compile_contract_file source entry_point in
Format.printf "Contract:\n%s\n" contract ; Format.printf "Contract:\n%s\n" contract ;
ok () ok ()
) in
| "expression" -> ( let term =
let%bind () = let source =
trace_strong (simple_error "bad number of args") @@ let open Arg in
Assert.assert_equal_int 6 l in let info =
let source = Sys.argv.(3) in let docv = "SOURCE_FILE" in
let entry_point = Sys.argv.(4) in let doc = "$(docv) is the path to the .ligo file of the contract." in
let expression = Sys.argv.(5) in info ~docv ~doc [] in
required @@ pos 0 (some string) None info in
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
value @@ pos 1 string "main" info in
Term.(const f $ source $ entry_point) in
let docs = "Compile contracts." in
(term , Term.info ~docs "compile-contract")
let compile_expression =
let f source entry_point expression =
toplevel @@
let%bind value = let%bind value =
trace (simple_error "compile expression") @@ trace (simple_error "compile-input") @@
Ligo.Contract.compile_contract_parameter source entry_point expression in Ligo.Contract.compile_contract_parameter source entry_point expression in
Format.printf "Input:\n%s\n" value; Format.printf "Input:\n%s\n" value;
ok () ok ()
) in
| _ -> simple_fail "Bad sub-command" let term =
) let source =
| _ -> simple_fail "Bad command" let open Arg in
let docv = "SOURCE_FILE" in
let doc = "$(docv) is the path to the .ligo file of the contract." in
let info = info ~docv ~doc [] in
required @@ pos 0 (some string) None info in
let entry_point =
let open Arg in
let docv = "ENTRY_POINT" in
let doc = "$(docv) is the entry-point of the contract." in
let info = info ~docv ~doc [] in
required @@ pos 1 (some string) None info in
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 in
Term.(const f $ source $ entry_point $ expression) in
let docs = "Compile contracts parameters." in
(term , Term.info ~docs "compile-parameter")
let () = toplevel @@ main () let () = Term.exit @@ Term.eval_choice main [compile_file ; compile_expression]

View File

@ -3,6 +3,7 @@
(public_name ligo) (public_name ligo)
(libraries (libraries
tezos-utils tezos-utils
cmdliner
ligo ligo
) )
(package ligo) (package ligo)