Skip to content

Commit

Permalink
Update examples so they work with cmdliner 1.1.x without a deprecatio…
Browse files Browse the repository at this point in the history
…n warning
  • Loading branch information
mjambon committed Feb 23, 2022
1 parent a7127cc commit 8bfd6a8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 48 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Cmdliner is to be used instead of the `Arg` module from the standard library.
This cheatsheet is a compact reference for common patterns.

[Two sample programs](src) are provided for exploration
purposes or as a template for new projects. Clone this git repository
purposes or as templates for new projects. Clone this git repository
and test it as follows (requires `dune` and of course `cmdliner`):
```
$ make
Expand Down
6 changes: 0 additions & 6 deletions src/.merlin

This file was deleted.

25 changes: 11 additions & 14 deletions src/Demo_arg_main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,25 @@ let tag_term =

(*
Combine the values collected for each kind of argument into a single
'conf' object.
'conf' object, which is then passed to our main 'run' function.
Some merging and tweaking can be useful here but most often
we just map each argument to its own record field.
*)
let cmdline_term =
let cmdline_term run =
let combine input_file num_cores user_name tags =
let only_tags =
match tags with
| [] -> []
| _input_file :: tags -> tags
in
{
let conf = {
input_file;
num_cores;
user_name;
tags = only_tags;
}
} in
run conf
in
Term.(const combine
$ input_file_term
Expand Down Expand Up @@ -147,20 +148,17 @@ let man = [
]

(*
Parse the command line into a 'conf' record. Exit early with appropriate
exit codes if there was an error or if '--help' was requested.
Parse the command line into a 'conf' record and pass it to the
main function 'run'.
*)
let parse_command_line () =
let parse_command_line_and_run run =
let info =
Term.info
Cmd.info
~doc
~man
"cmdliner-cheatsheet" (* program name as it will appear in --help *)
in
match Term.eval (cmdline_term, info) with
| `Error _ -> exit 1
| `Version | `Help -> exit 0
| `Ok conf -> conf
Cmd.v info (cmdline_term run) |> Cmd.eval |> exit

let safe_run conf =
try run conf
Expand All @@ -176,7 +174,6 @@ let safe_run conf =

let main () =
Printexc.record_backtrace true;
let conf = parse_command_line () in
safe_run conf
parse_command_line_and_run safe_run

let () = main ()
50 changes: 23 additions & 27 deletions src/Demo_subcmd_main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ type cmd_conf =
| Subcmd1 of subcmd1_conf
| Subcmd2 of subcmd2_conf

(*
Do something with the parsed command line.
*)
let run cmd_conf =
match cmd_conf with
| Subcmd1 conf ->
Expand Down Expand Up @@ -63,9 +66,9 @@ let bar_term =

(*** Putting together subcommand 'subcmd1' ***)

let subcmd1_term =
let subcmd1_term run =
let combine foo =
Subcmd1 { foo }
Subcmd1 { foo } |> run
in
Term.(const combine
$ foo_term
Expand All @@ -78,19 +81,19 @@ let subcmd1_man = [
`P "[multiline overview of subcmd1]";
]

let subcmd1 =
let subcmd1 run =
let info =
Term.info "subcmd1"
Cmd.info "subcmd1"
~doc:subcmd1_doc
~man:subcmd1_man
in
(subcmd1_term, info)
Cmd.v info (subcmd1_term run)

(*** Putting together subcommand 'subcmd2' ***)

let subcmd2_term =
let subcmd2_term run =
let combine bar =
Subcmd2 { bar }
Subcmd2 { bar } |> run
in
Term.(const combine
$ bar_term
Expand All @@ -103,13 +106,13 @@ let subcmd2_man = [
`P "[multiline overview of subcmd2]";
]

let subcmd2 =
let subcmd2 run =
let info =
Term.info "subcmd2"
Cmd.info "subcmd2"
~doc:subcmd2_doc
~man:subcmd2_man
in
(subcmd2_term, info)
Cmd.v info (subcmd2_term run)

(*** Putting together the main command ***)

Expand All @@ -126,19 +129,16 @@ let root_man = [
let root_term =
Term.ret (Term.const (`Help (`Pager, None)))

let root_subcommand =
let info =
Term.info "cmdliner-demo-subcmd"
~doc:root_doc
~man:root_man
in
(root_term, info)
let root_info =
Cmd.info "cmdliner-demo-subcmd"
~doc:root_doc
~man:root_man

(*** Parse the command line and do something with it ***)

let subcommands = [
subcmd1;
subcmd2;
let subcommands run = [
subcmd1 run;
subcmd2 run;
]

(*
Expand All @@ -151,14 +151,10 @@ let subcommands = [
Otherwise, 'conf' is returned to the application.
*)
let parse_command_line () : cmd_conf =
match Term.eval_choice root_subcommand subcommands with
| `Error _ -> exit 1
| `Version | `Help -> exit 0
| `Ok conf -> conf
let parse_command_line_and_run (run : cmd_conf -> unit) =
Cmd.group root_info (subcommands run) |> Cmd.eval |> exit

let main () =
let conf = parse_command_line () in
run conf
parse_command_line_and_run run

let () = main ()

0 comments on commit 8bfd6a8

Please sign in to comment.