package ez_cmdliner
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file v2.ml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730(**************************************************************************) (* *) (* Typerex Libraries *) (* *) (* Copyright 2011-2017 OCamlPro SAS *) (* *) (* All rights reserved. This file is distributed under the terms of *) (* the GNU Lesser General Public License version 2.1, with the *) (* special exception on linking described in the file LICENSE. *) (* *) (**************************************************************************) open EzCompat open Ez_subst.V1 module EZCMD = struct module TYPES = struct type block = [ `S of string (* section title *) | `P of string (* paragraph *) | `Pre of string (* code *) | `I of string * string (* item (description, info) *) | `Noblank | `Blocks of block list ] type env = { env_docs : string option ; env_doc : string option ; env_var : string ; } type info = { arg_docs : string option ; arg_docv : string option ; arg_env : env option ; arg_version : string option ; arg_doc : string ; } module Arg = struct type spec = (* Same as Arg. But they should only appear at most once on the command-line, or Cmdliner will complain. *) | Unit of (unit -> unit) | Bool of (bool -> unit) | Set_bool of bool ref | Set of bool ref | Clear of bool ref | String of (string -> unit) | Set_string of string ref | Int of (int -> unit) | Set_int of int ref | Float of (float -> unit) | Set_float of float ref | Symbol of string list * (string -> unit) | File of (string -> unit) (* Anonymous arguments. `Anon(n,f)` means the anonymous argument at position `n`. `Anons f` means all the anonymous arguments. *) | Anon of int * (string -> unit) | Anons of (string list -> unit) end type arg_list = (string list * Arg.spec * info) list type sub = { sub_name : string ; sub_action : unit -> unit; sub_args : arg_list; sub_man : block list; sub_version : string option ; sub_doc : string; } type command = { cmd_name : string; cmd_action : unit -> unit; cmd_args : arg_list; cmd_man : Cmdliner.Manpage.block list; cmd_doc : string; } end include TYPES.Arg module RAWTYPES = TYPES let raw_env x = x let raw_sub x = x let raw_info x = x open TYPES open TYPES.Arg open Cmdliner let info ?docs ?docv ?env ?version doc = { arg_docs = docs ; arg_docv = docv ; arg_env = env ; arg_version = version ; arg_doc = doc ; } let env ?docs ?doc env_var = { env_docs = docs ; env_doc = doc ; env_var } let rec term_of_list list = match list with | [] -> Term.(const ()) | (args, action, info) :: tail -> ( let x = term_of_list tail in let arg_info = let env = match info.arg_env with | None -> None | Some env -> Some ( Cmd.Env.info ?docs:env.env_docs ?doc:env.env_doc env.env_var) in Cmdliner.Arg.info ?docs:info.arg_docs ?docv:info.arg_docv ?env ~doc:info.arg_doc args in match action with | Unit f -> let term = Arg.(value & flag_all & arg_info) in let f () x = List.iter (fun x -> if x then f ()) x in Term.(const f $ x $ term) | Bool f -> let term = Arg.(value & flag_all & arg_info) in let f () x = List.iter f x in Term.(const f $ x $ term) | Set_bool r -> let term = Arg.(value & opt (some bool) None & arg_info) in let f () = function None -> () | Some s -> r := s in Term.(const f $ x $ term) | Set r -> let term = Arg.(value & flag & arg_info) in let f () x = if x then r := true in Term.(const f $ x $ term) | Clear r -> let term = Arg.(value & flag & arg_info) in let f () x = if x then r := false in Term.(const f $ x $ term) | String f -> let term = Arg.(value & opt_all string [] & arg_info) in let f () x = List.iter f x in Term.(const f $ x $ term) | Set_string r -> let term = Arg.(value & opt (some string) None & arg_info) in let f () = function None -> () | Some s -> r := s in Term.(const f $ x $ term) | Int f -> let term = Arg.(value & opt (some int) None & arg_info) in let f () = function None -> () | Some s -> f s in Term.(const f $ x $ term) | Set_int r -> let term = Arg.(value & opt (some int) None & arg_info) in let f () = function None -> () | Some s -> r := s in Term.(const f $ x $ term) | Float f -> let term = Arg.(value & opt (some float) None & arg_info) in let f () = function None -> () | Some s -> f s in Term.(const f $ x $ term) | Set_float r -> let term = Arg.(value & opt (some float) None & arg_info) in let f () = function None -> () | Some s -> r := s in Term.(const f $ x $ term) | Symbol (symbols, f) -> let symbol = Arg.enum (List.map (fun s -> (s, s)) symbols) in let term = Arg.(value & opt (some symbol) None & arg_info) in let f () = function None -> () | Some s -> f s in Term.(const f $ x $ term) | File f -> let term = Arg.(value & opt_all file [] & arg_info) in let f () x = List.iter f x in Term.(const f $ x $ term) | Anon (n, f) -> let term = Arg.(value & pos n (some string) None & arg_info) in let f () = function None -> () | Some s -> f s in Term.(const f $ x $ term) | Anons f -> let term = Arg.(value & pos_all string [] & arg_info) in let f () x = f x in Term.(const f $ x $ term) ) let cmd_exits = Cmd.Exit.defaults let sub sub_name ~doc ?(args = []) ?(man = []) ?version sub_action = { sub_name ; sub_doc = doc ; sub_action ; sub_args = args ; sub_man = man ; sub_version = version ; } let create_sub ?version sub ~common_args = let man = sub.sub_man in let exits = cmd_exits in let doc = sub.sub_doc in let args = common_args @ sub.sub_args in ( Term.(const sub.sub_action $ term_of_list args), Cmd.info sub.sub_name ?version ~doc ~sdocs:Manpage.s_common_options ~exits ~man ) let help more_topics man_format cmds topic = match topic with | None -> `Help (`Pager, None) (* help about the program. *) | Some topic -> ( let topics = ("topics" :: List.map fst more_topics) @ cmds in let conv, _ = Cmdliner.Arg.enum (List.rev_map (fun s -> (s, s)) topics) in match conv topic with | `Error e -> `Error (false, e) | `Ok t when t = "topics" -> List.iter print_endline topics; `Ok () | `Ok t when List.mem t cmds -> `Help (man_format, Some t) | `Ok t -> let page = ((topic, 7, "", "", ""), `S topic :: List.assoc t more_topics) in `Ok (Cmdliner.Manpage.print man_format Format.std_formatter page) ) let help_cmd ~name ~man ~topics = let topic = let doc = "The topic to get help on. `topics' lists the topics." in Arg.(value & pos 0 (some string) None & info [] ~docv:"TOPIC" ~doc) in let doc = Printf.sprintf "display help about %s and %s commands" name name in let man = [ `S Manpage.s_description; `P "Prints help about darcs commands and other subjects..."; `Blocks man; ] in ( Term.(ret (const (help topics) $ Arg.man_format $ Term.choice_names $ topic)), Cmd.info "help" ~doc ~exits:Cmd.Exit.defaults ~man ) let default_cmd ?version ~doc ~man name = let sdocs = Manpage.s_common_options in let exits = Cmd.Exit.defaults in ( Term.(ret (const (`Help (`Pager, None)))), Cmd.info name ?version ~doc ~sdocs ~exits ~man ) let to_cmd (t, info) = Cmd.v info t let main_with_subcommands ~name ?version ?default ~doc ?(man=[]) ?(topics = []) ?(common_args=[]) ?argv subs = let cmds = List.map (create_sub ?version ~common_args) subs in let default_cmd = match default with | None -> default_cmd name ?version ~doc ~man | Some cmd -> create_sub ?version cmd ~common_args in let cmds = if List.exists (fun cmd -> cmd.sub_name = "help") subs then cmds else cmds @ [ help_cmd ~name ~man ~topics ] in let code = Cmd.eval ~catch:false ?argv ( Cmd.group ~default:(fst default_cmd) ( snd default_cmd ) ( List.map to_cmd cmds ) ) in if code = Cmd.Exit.ok then () else exit code let main ?version ?argv cmd = let cmd = create_sub ?version cmd ~common_args:[] in let code = Cmd.eval ~catch:false ?argv ( to_cmd cmd ) in if code = Cmd.Exit.ok then () else exit code module MANPAGE = Cmdliner.Manpage let translate ?docs arg_list = List.map (fun (arg, spec, doc) -> let len = String.length arg in let arg = if len > 0 && arg.[0] = '-' then if len > 1 && arg.[1] = '-' then String.sub arg 2 (len - 2) else String.sub arg 1 (len - 1) else arg in ([ arg ], spec, info ?docs doc)) arg_list let translate_anon arg_anon = [ ([], Anons (fun list -> List.iter arg_anon list), info "General arguments"); ] let parse ?name ?version ?(man = []) arg_list arg_anon arg_usage = let sub_args = translate arg_list @ translate_anon arg_anon in let sub_name = match name with None -> "COMMAND" | Some sub_name -> sub_name in let cmd = { sub_name; sub_doc = arg_usage; sub_args; sub_man = man; sub_version = None; sub_action = (fun () -> ()); } in main ?version cmd module RST = struct open TYPES let doclang_to_rst ?(map= StringMap.empty) s = let paren map s = match StringMap.find s map with | s -> s | exception Not_found -> match EzString.chop_prefix s ~prefix:"b," with | Some s -> Printf.sprintf "**%s**" s | None -> match EzString.chop_prefix s ~prefix:"i," with | Some s -> Printf.sprintf "*%s*" s | None -> s in EZ_SUBST.string ~paren:paren ~ctxt:map s let man_to_rst ?(map = StringMap.empty) ( man : block list ) = let b = Buffer.create 1000 in let rec iter = function | `S s -> let s = doclang_to_rst ~map s in Printf.bprintf b "\n\n**%s**\n\n" s | `Blocks list -> List.iter iter list | `I (label, txt) -> Printf.bprintf b "\n* %s\n %s\n" ( doclang_to_rst ~map label ) ( doclang_to_rst ~map txt ) | `Noblank -> () | `P par -> Printf.bprintf b "\n%s\n" ( doclang_to_rst ~map par ) | `Pre code -> let code = doclang_to_rst ~map code in Printf.bprintf b "::\n\n %s\n\n" ( String.concat "\n " ( EzString.split code '\n' )) in List.iter iter man; Buffer.contents b let arg_name info name = match info.arg_docv with | None -> name | Some name -> name let arg_name f info = match f with | Arg.String _ -> arg_name info "STRING" | Arg.Bool _ -> arg_name info "BOOL" | Arg.Int _ -> arg_name info "INT" | Arg.Float _ -> arg_name info "FLOAT" | Arg.Set_string _ -> arg_name info "STRING" | Arg.Set_bool _ -> arg_name info "BOOL" | Arg.Set_int _ -> arg_name info "INT" | Arg.Set_float _ -> arg_name info "FLOAT" | Arg.Unit _ | Arg.Set _ | Arg.Clear _ -> "" | Arg.File _ -> arg_name info "FILE" | Arg.Anon (_, _) -> arg_name info "ARGUMENT" | Arg.Anons _ -> arg_name info "ARGUMENTS" | Arg.Symbol (list, _) -> arg_name info (Printf.sprintf "[%s]" ( String.concat "|" list)) let print_options b options = List.iter (fun (option, f, info) -> let arg_name = arg_name f info in let map = StringMap.add "docv" arg_name StringMap.empty in Printf.bprintf b "\n* %s " (match option with | [] -> Printf.sprintf ":code:`%s`" arg_name | _ -> let arg_name = if arg_name = "" then "" else " " ^ arg_name in String.concat " or " @@ List.map (fun s -> if String.length s = 1 then Printf.sprintf ":code:`-%s%s`" s arg_name else Printf.sprintf ":code:`--%s%s`" s arg_name ) option ); Printf.bprintf b " %s%s\n" (match info.arg_version with | None -> "" | Some version -> Printf.sprintf "(since version %s) " version) ( doclang_to_rst ~map info.arg_doc ) ) options let to_rst ?(name=Filename.basename Sys.argv.(0)) commands common_args = let commands = List.map raw_sub commands in let commands = List.sort (fun cmd1 cmd2 -> compare cmd1.sub_name cmd2.sub_name) commands in let b = Buffer.create 10000 in Printf.bprintf b {| Sub-commands and Arguments ========================== |}; begin match common_args with | [] -> () | _ -> Printf.bprintf b "Common arguments to all sub-commands:\n\n"; print_options b ( List.sort compare common_args ); end; Printf.bprintf b {| Overview of sub-commands:: |}; List.iter (fun cmd -> Printf.bprintf b " \n %s%s\n %s\n" cmd.sub_name (match cmd.sub_version with | None -> "" | Some version -> Printf.sprintf " (since version %s)" version) (doclang_to_rst cmd.sub_doc) ) commands; List.iter (fun cmd -> let s = Printf.sprintf "\n\n%s %s%s" name cmd.sub_name (match cmd.sub_version with | None -> "" | Some version -> Printf.sprintf " (since version %s)" version) in Printf.bprintf b "%s\n%s\n\n" s ( String.make ( String.length s) '~' ); Printf.bprintf b "%s\n\n" (doclang_to_rst cmd.sub_doc); Printf.bprintf b "%s" (man_to_rst cmd.sub_man); let options = cmd.sub_args in (* TODO: compare may fail on arguments because they contain closures... *) let options = List.sort compare options in let options = List.map (fun (args, f, info) -> (args, f, raw_info info)) options in Printf.bprintf b "\n**USAGE**\n::\n \n %s %s%s [OPTIONS]\n\n" name cmd.sub_name ( String.concat "" ( List.map (function ( [], f, info ) -> " " ^ arg_name f info | _ -> "") options)) ; Printf.bprintf b "Where options are:\n\n"; print_options b options; ) commands; Buffer.contents b end let to_rst = RST.to_rst module MAKE( M : sig val command : string val version : string val about : string val usage : string (* If this env variable is set, a backtrace will be generated on errors *) val backtrace_var : string option (* manipulate verbosity *) val get_verbosity : unit -> int val set_verbosity : int -> unit (* standard error: no backtrace is printed unless the backtrace_var env variable is defined. *) exception Error of string end ) = struct type cmd_node = { mutable node_cmd : sub option ; mutable node_map : cmd_node StringMap.t ref ; mutable node_commands : sub list ; } let echo () = Printf.eprintf "CMD: \x1B[1;33m %s \x1B[0m \n%!" ( String.concat " " ( List.filter (fun s -> s <> "--echo") ( Array.to_list Sys.argv ))) let backtrace = match M.backtrace_var with | None -> false | Some v -> match Sys.getenv v with | _ -> true | exception _ -> false let backtrace = ref backtrace let increase_verbosity ()= let v = M.get_verbosity () in if v = 1 then begin Printexc.record_backtrace true; backtrace := true; end; M.set_verbosity ( v + 1 ) let main ?(on_error = (fun () -> ()) ) ?(on_exit = (fun () -> ()) ) ?(print_config = (fun () -> ()) ) ?(common_args = []) ?(argv = Sys.argv) subcommands = Printexc.record_backtrace !backtrace; let cmdmap subcommands = let cmdmap = ref StringMap.empty in let rec add_cmd map path cmd = match path with [] -> assert false | name :: path -> let node = match StringMap.find name !map with | exception Not_found -> let node = { node_cmd = None ; node_map = ref StringMap.empty ; node_commands = [] ; } in map := StringMap.add name node !map; node | node -> node in node.node_commands <- cmd :: node.node_commands ; match path with | [] -> assert ( node.node_cmd = None ); node.node_cmd <- Some cmd | _ -> add_cmd node.node_map path cmd in List.iter (fun ( path, cmd ) -> add_cmd cmdmap path cmd ) subcommands ; !cmdmap in let common_args = [ [ "v"; "verbose" ], Unit increase_verbosity, info "Increase verbosity level" ; [ "q"; "quiet" ], Unit (fun () -> M.set_verbosity 0), info "Set verbosity level to 0"; ] @ common_args in let args = Array.to_list argv in let get_commands = function | Some (map, commands) -> (map, commands) | None -> let commands = List.map (fun cmd -> EzString.split cmd.sub_name ' ', cmd ) subcommands in let map = cmdmap commands in let commands = List.map snd commands in (map,commands) in let common_args_map = ref StringMap.empty in List.iter (fun (args, kind, _) -> List.iter (fun arg -> let arg = if String.length arg = 1 then "-" ^ arg else "--" ^ arg in common_args_map := StringMap.add arg kind !common_args_map ) args ) common_args ; let rec iter_initial_args path pre_args ( commands : (cmd_node StringMap.t * sub list ) option ) args = match args with | [] -> begin match path with | [] -> Printf.eprintf "Use '%s --help' for help on commands\n%!" M.command; print_config (); exit 0 | _ -> let _map, commands = get_commands commands in path, List.rev pre_args @ args, commands end | "--echo" :: args -> echo () ; iter_initial_args path pre_args commands args | [ "--version" ] -> Printf.printf "%s\n%!" M.version; exit 0 | [ "--about" ] -> Printf.printf "%s\n%!" M.about; exit 0 | [ "rst" ] | [ "--rst" ] -> let _map, commands = get_commands commands in Printf.printf "%s%!" ( to_rst commands common_args ); exit 0 | ( "-v" | "--verbose" ) :: args -> increase_verbosity (); iter_initial_args path pre_args commands args | ( "-q" | "--quiet" ) :: args -> M.set_verbosity 0; iter_initial_args path pre_args commands args | name :: rem_args -> match StringMap.find name !common_args_map with | kind -> let pre_args = name :: pre_args in let pre_args, rem_args = match kind with | Anon _ | Anons _ -> assert false | Set _ | Unit _ | Bool _ | Set_bool _ | Clear _ | Symbol _ (* TODO: check *) -> pre_args, rem_args | String _ | Set_string _ | Int _ | Set_int _ | Float _ | Set_float _ | File _ -> match rem_args with | [] -> Printf.eprintf "Error: argument %S expects an argument\n%!" name; exit 2 | arg :: rem_args -> arg :: pre_args, rem_args in iter_initial_args path pre_args commands rem_args | exception Not_found -> let map, commands = get_commands commands in match StringMap.find name map with | exception Not_found -> path, List.rev pre_args @ args, commands | node -> iter_initial_args ( name :: path ) pre_args ( Some (! (node.node_map) , node.node_commands )) rem_args in let path, args, ez_commands = iter_initial_args [] [] None (List.tl args ) in let args = match path with | [] -> args | path -> ( String.concat " " (List.rev path) ) :: args in let argv = Array.of_list ( Sys.argv.(0) :: args ) in try begin match args with | [] -> () | _ -> main_with_subcommands ~name:M.command ~version:M.version ~doc:M.usage ~man:[] ~argv ez_commands ~common_args; end; on_exit () with | M.Error s when not !backtrace -> on_error () ; Printf.eprintf "Fatal error: %s\n%!" s; exit 2 | exn -> on_error (); let bt = Printexc.get_backtrace () in let error = Printexc.to_string exn in Printf.eprintf "fatal exception %s\n%s\n%!" error bt; exit 2 end end