use cmdliner
This commit is contained in:
parent
9bee347c17
commit
82f99de39c
2 changed files with 51 additions and 42 deletions
2
app/dune
2
app/dune
|
@ -1,4 +1,4 @@
|
||||||
(executable
|
(executable
|
||||||
(name main)
|
(name main)
|
||||||
(public_name opam_graph)
|
(public_name opam_graph)
|
||||||
(libraries cmdliner logs logs.fmt opam-graph))
|
(libraries cmdliner logs logs.fmt fmt.cli fmt.tty logs.cli opam-graph))
|
||||||
|
|
91
app/main.ml
91
app/main.ml
|
@ -11,45 +11,54 @@ let read_file file =
|
||||||
invalid_arg ("Error reading file: " ^ file)
|
invalid_arg ("Error reading file: " ^ file)
|
||||||
with _ -> invalid_arg ("Error opening file " ^ file)
|
with _ -> invalid_arg ("Error opening file " ^ file)
|
||||||
|
|
||||||
let () =
|
let jump () transitive file output_format =
|
||||||
match Sys.argv with
|
let switch = read_file file in
|
||||||
| [| _ ; output_format; file |] -> (
|
let data = OpamFile.SwitchExport.read_from_string switch in
|
||||||
let switch = read_file file in
|
match output_format with
|
||||||
let data = OpamFile.SwitchExport.read_from_string switch in
|
| `Text ->
|
||||||
match output_format with
|
let graph = Opam_graph.dependencies ~transitive data in
|
||||||
| "text" ->
|
Format.printf "%a" Opam_graph.pp_graph graph
|
||||||
let graph = Opam_graph.dependencies data in
|
| `Dot ->
|
||||||
Format.printf "%a" Opam_graph.pp_graph graph
|
let graph = Opam_graph.dependencies ~transitive data in
|
||||||
| "text_transitive" ->
|
let dot = Opam_graph.Render.Dot.of_graph graph in
|
||||||
let graph = Opam_graph.dependencies ~transitive:true data in
|
Format.printf "%a" Opam_graph.Render.Dot.pp dot
|
||||||
Format.printf "%a" Opam_graph.pp_graph graph
|
| `Dot_ui ->
|
||||||
| "dot" ->
|
let graph = Opam_graph.Ui.dependencies ~transitive data in
|
||||||
let graph = Opam_graph.dependencies data in
|
let dot = Opam_graph.Render.Dot.of_assoc graph in
|
||||||
let dot = Opam_graph.Render.Dot.of_graph graph in
|
Format.printf "%a" Opam_graph.Render.Dot.pp dot
|
||||||
Format.printf "%a" Opam_graph.Render.Dot.pp dot
|
| `Html ->
|
||||||
| "dot_ui" ->
|
let graph = Opam_graph.Ui.dependencies ~transitive data in
|
||||||
let transitive = false in
|
let html = Opam_graph.Render.Html.of_assoc graph in
|
||||||
let graph = Opam_graph.Ui.dependencies ~transitive data in
|
Format.printf "%a" Opam_graph.Render.Html.pp html
|
||||||
let dot = Opam_graph.Render.Dot.of_assoc graph in
|
|
||||||
Format.printf "%a" Opam_graph.Render.Dot.pp dot
|
|
||||||
| "dot_ui_transitive" ->
|
|
||||||
let transitive = true in
|
|
||||||
let graph = Opam_graph.Ui.dependencies ~transitive data in
|
|
||||||
let dot = Opam_graph.Render.Dot.of_assoc graph in
|
|
||||||
Format.printf "%a" Opam_graph.Render.Dot.pp dot
|
|
||||||
| "html_ui" ->
|
|
||||||
let transitive = false in
|
|
||||||
let graph = Opam_graph.Ui.dependencies ~transitive data in
|
|
||||||
let html = Opam_graph.Render.Html.of_assoc graph in
|
|
||||||
Format.printf "%a" Opam_graph.Render.Html.pp html
|
|
||||||
| "html_ui_transitive" ->
|
|
||||||
let transitive = true in
|
|
||||||
let graph = Opam_graph.Ui.dependencies ~transitive data in
|
|
||||||
let html = Opam_graph.Render.Html.of_assoc graph in
|
|
||||||
Format.printf "%a" Opam_graph.Render.Html.pp html
|
|
||||||
| _ -> failwith "Unsupported output format"
|
|
||||||
)
|
|
||||||
| _ ->
|
|
||||||
print_endline "expecting exactly one argument";
|
|
||||||
exit 1
|
|
||||||
|
|
||||||
|
let setup_log style_renderer level =
|
||||||
|
Fmt_tty.setup_std_outputs ?style_renderer ();
|
||||||
|
Logs.set_level level;
|
||||||
|
Logs.set_reporter (Logs_fmt.reporter ~dst:Format.std_formatter ())
|
||||||
|
|
||||||
|
open Cmdliner
|
||||||
|
|
||||||
|
let setup_log =
|
||||||
|
Term.(const setup_log
|
||||||
|
$ Fmt_cli.style_renderer ()
|
||||||
|
$ Logs_cli.level ())
|
||||||
|
|
||||||
|
let transitive =
|
||||||
|
let doc = "Transitive dependencies" in
|
||||||
|
Arg.(value & flag & info [ "transitive" ] ~doc)
|
||||||
|
|
||||||
|
let formats = [ "html", `Html ; "dot", `Dot ; "dot-ui", `Dot_ui ; "text", `Text ]
|
||||||
|
|
||||||
|
let output_format =
|
||||||
|
let doc = "Output format" in
|
||||||
|
Arg.(value & opt (Arg.enum formats) `Text & info [ "output-format" ] ~doc)
|
||||||
|
|
||||||
|
let file =
|
||||||
|
let doc = "The opam switch export to graph" in
|
||||||
|
Arg.(required & pos 0 (some file) None & info [ ] ~doc ~docv:"FILE")
|
||||||
|
|
||||||
|
let cmd =
|
||||||
|
Term.(const jump $ setup_log $ transitive $ file $ output_format),
|
||||||
|
Term.info "opam_graph" ~version:"%%VERSION%%"
|
||||||
|
|
||||||
|
let () = match Term.eval cmd with `Ok () -> exit 0 | _ -> exit 1
|
||||||
|
|
Loading…
Reference in a new issue