Something went wrong. Try again.
How a command-line program ends
Something went wrong. Try again.
1.6 kB · 63 lines
OCaml
at main
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364(* One command with one mode per ending [Cli_exit.run] decides. *)
open Cmdliner
(* Not [Failure], so [boom] stays an exception nothing expected and the two arms of [~refusal] cannot be the same arm. *)exception Refused of string
type mode = Lines | Copy | Boom | Deny | Refuse
let modes = [ ("lines", Lines); ("copy", Copy); ("boom", Boom); ("deny", Deny); ("refuse", Refuse); ]
let mode = let doc = "What to do: $(docv) is one of lines, copy, boom, deny or refuse." in Arg.(required & pos 0 (some (enum modes)) None & info [] ~docv:"MODE" ~doc)
let count = let doc = "Write $(docv) numbered lines." in Arg.(value & opt int 1000 & info [ "count" ] ~docv:"N" ~doc)
let lines n = for i = 1 to n do Fmt.pr "line %d@." i done
let copy n = Eio_main.run @@ fun env -> Cli_exit.writing_output @@ fun () -> let stdout = Eio.Stdenv.stdout env in for i = 1 to n do Eio.Flow.copy_string (Fmt.str "line %d\n" i) stdout done
let run mode n = match mode with | Lines -> lines n; `Ok () | Copy -> copy n; `Ok () | Boom -> failwith "nothing expected this" | Deny -> raise (Refused "the count is not one this command counts to") | Refuse -> `Error (false, "the image is not one this command reads")
let () = let doc = "Every ending cli-exit decides, one per mode." in let info = Cmd.info "probe" ~version:"1.0" ~doc ~sdocs:Cli_exit.sdocs ~man:(Cli_exit.standard_options ~version:true) in Cli_exit.run ~refusal:(function Refused message -> Some message | _ -> None) (Cmd.v info Term.(ret (const run $ mode $ count)))