Skip to content

Commit

Permalink
Merge pull request #217 from n-osborne/default-term
Browse files Browse the repository at this point in the history
Move to use a default `Cmdliner.term` rather than the `usage` function
  • Loading branch information
n-osborne authored Apr 15, 2024
2 parents 3feceab + 2d691a4 commit b737385
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Unreleased

- Make `--help` and `--version` work even without any plugins installed
[\#217](https://github.com/ocaml-gospel/ortac/pull/217)
- Improve test-failure message
[\#202](https://github.com/ocaml-gospel/ortac/pull/202) and
[\#204](https://github.com/ocaml-gospel/ortac/pull/204) and
Expand Down
44 changes: 25 additions & 19 deletions bin/cli.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,31 @@ let () =

open Cmdliner

let usage () =
Format.(fprintf err_formatter)
"@[ortac: required plugin is missing, please install at least one@ \
(qcheck-stm,@ monolith@ or@ wrapper).@]@.";
exit Cmd.Exit.cli_error
let default_cmd cmds =
let errmsg, usage =
match cmds with
| [] ->
(* We cannot be in the case where ORTAC_ONLY_PLUGIN was set, since
trying to load only one missing plugin would have raised an exception
earlier, so we know no plugin is available *)
(* Displaying usage in that case is useless *)
("error: no plugin is available, please install at least one", false)
| _ -> ("error: missing command", true)
in
Term.(term_result ~usage (const (Result.error (`Msg errmsg))))

let () =
match
let doc = "Run ORTAC."
and version =
Printf.sprintf "ortac version: %s"
(match Build_info.V1.version () with
| None -> "n/a"
| Some v -> Build_info.V1.Version.to_string v)
in
let info = Cmd.info "ortac" ~doc ~version
and cmds =
Registration.fold (fun acc cmd -> cmd :: acc) [] Registration.plugins
with
| [] -> usage ()
| cmds ->
let doc = "Run ORTAC." in
let version =
Printf.sprintf "ortac version: %s"
(match Build_info.V1.version () with
| None -> "n/a"
| Some v -> Build_info.V1.Version.to_string v)
in
let info = Cmd.info "ortac" ~doc ~version in
let group = Cmd.group info cmds in
Stdlib.exit (Cmd.eval group)
in
let default = default_cmd cmds in
let group = Cmd.group info ~default cmds in
Stdlib.exit (Cmd.eval group)

0 comments on commit b737385

Please sign in to comment.