Skip to content

Commit 628a026

Browse files
committed
basic cmdliner interface
1 parent e1371c7 commit 628a026

File tree

6 files changed

+36
-15
lines changed

6 files changed

+36
-15
lines changed

bin/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
(name mustache_cli)
33
(public_name mustache)
44
(modules mustache_cli)
5-
(libraries mustache ezjsonm))
5+
(libraries mustache ezjsonm cmdliner))

bin/mustache_cli.ml

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,21 @@ let run json_filename template_filename =
4040
Mustache.pp_render_error err;
4141
exit 2
4242

43-
let usage () =
44-
print_endline "Usage: mustache-cli json_filename template_filename"
43+
let run_command =
44+
let open Cmdliner in
45+
let doc = "renders Mustache template from JSON data files" in
46+
let json_file =
47+
let doc = "data file in JSON format" in
48+
Arg.(required & pos 0 (some file) None & info [] ~docv:"DATA.json" ~doc)
49+
in
50+
let template_file =
51+
let doc = "mustache template" in
52+
Arg.(required & pos 1 (some file) None & info [] ~docv:"TEMPLATE.mustache" ~doc)
53+
in
54+
Term.(const run $ json_file $ template_file),
55+
Term.info "mustache" ~doc
56+
4557

4658
let () =
47-
match Sys.argv with
48-
| [| _ ; json_filename ; template_filename |]
49-
-> run json_filename template_filename
50-
| _ -> usage ()
59+
let open Cmdliner in
60+
Term.exit @@ Term.eval run_command

bin/test/errors/json-errors.t

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44

55
Empty json file:
66
$ mustache empty.json foo.mustache
7-
Fatal error: exception Ezjsonm.Parse_error(870828711, "JSON.of_buffer expected JSON text (JSON value)")
8-
[2]
7+
mustache: internal error, uncaught exception:
8+
Ezjsonm.Parse_error(870828711, "JSON.of_buffer expected JSON text (JSON value)")
9+
10+
[125]
911

1012
Invalid json file:
1113
$ mustache invalid.json foo.mustache
12-
Fatal error: exception Ezjsonm.Parse_error(870828711, "JSON.of_buffer expected value separator or object end (',' or '}')")
13-
[2]
14+
mustache: internal error, uncaught exception:
15+
Ezjsonm.Parse_error(870828711, "JSON.of_buffer expected value separator or object end (',' or '}')")
16+
17+
[125]

bin/test/errors/sys-errors.t

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33

44
Nonexistent json file:
55
$ mustache nonexistent.json foo.mustache
6-
Fatal error: exception Sys_error("nonexistent.json: No such file or directory")
7-
[2]
6+
mustache: DATA.json argument: no `nonexistent.json' file or directory
7+
Usage: mustache [OPTION]... DATA.json TEMPLATE.mustache
8+
Try `mustache --help' for more information.
9+
[124]
810

911
Nonexistent template file:
1012
$ mustache foo.json nonexistent.mustache
11-
Fatal error: exception Sys_error("nonexistent.mustache: No such file or directory")
12-
[2]
13+
mustache: TEMPLATE.mustache argument: no `nonexistent.mustache' file or
14+
directory
15+
Usage: mustache [OPTION]... DATA.json TEMPLATE.mustache
16+
Try `mustache --help' for more information.
17+
[124]

dune-project

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ Contains the `mustache` command line utility for driving logic-less templates.
2727
ezjsonm
2828
(ounit :with-test)
2929
(menhir (>= 20180703))
30+
(cmdliner (>= 1.0.4))
3031
(ocaml (>= 4.06))))

mustache.opam

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ depends: [
2020
"ezjsonm"
2121
"ounit" {with-test}
2222
"menhir" {>= "20180703"}
23+
"cmdliner" {>= "1.0.4"}
2324
"ocaml" {>= "4.06"}
2425
"odoc" {with-doc}
2526
]

0 commit comments

Comments
 (0)