Skip to content

Commit dc1115b

Browse files
authored
Merge pull request #59 from gasche/cmdliner
Cmdliner
2 parents e1371c7 + 7faf4cc commit dc1115b

File tree

6 files changed

+90
-15
lines changed

6 files changed

+90
-15
lines changed

bin/dune

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,13 @@
22
(name mustache_cli)
33
(public_name mustache)
44
(modules mustache_cli)
5-
(libraries mustache ezjsonm))
5+
(libraries mustache ezjsonm cmdliner))
6+
7+
(rule
8+
(with-stdout-to mustache.1
9+
(run mustache --help=groff)))
10+
11+
(install
12+
(section man)
13+
(package mustache)
14+
(files mustache.1))

bin/mustache_cli.ml

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,66 @@ 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 man = [
47+
`S Manpage.s_description;
48+
`P "$(tname) is a command-line tool coming with the $(i,ocaml-mustache) library,
49+
an OCaml implementation of the Mustache template format.
50+
$(tname) takes a data file and a template file as command-line parameters;
51+
it renders the populated template on standard output.";
52+
53+
`P "Mustache is a simple and popular template format,
54+
with library implementations in many programming languages.
55+
It is named from its {{..}} delimiters.";
56+
57+
`I ("Mustache website",
58+
"https://mustache.github.io/");
59+
`I ("Mustache templates documentation",
60+
"https://mustache.github.io/mustache.5.html");
61+
`I ("ocaml-mustache website:",
62+
"https://github.com/rgrinberg/ocaml-mustache");
63+
64+
`P "The $(i,ocaml-mustache) implementation is tested against
65+
the Mustache specification testsuite.
66+
All features are supported, except for lambdas and setting delimiter tags.";
67+
`S Manpage.s_examples;
68+
`Pre
69+
{|
70+
\$ cat data.json
71+
{ "name": "OCaml",
72+
"qualities": [{"name": "simple"}, {"name": "fun"}] }
73+
74+
\$ cat hello.mustache
75+
Hello {{name}}!
76+
Mustache is:
77+
{{#qualities}}
78+
- {{name}}
79+
{{/qualities}}
80+
81+
\$ $(tname) data.json hello.mustache
82+
Hello OCaml!
83+
Mustache is:
84+
- simple
85+
- fun
86+
|};
87+
`S Manpage.s_bugs;
88+
`P "Report bugs on https://github.com/rgrinberg/ocaml-mustache/issues";
89+
]
90+
in
91+
let json_file =
92+
let doc = "data file in JSON format" in
93+
Arg.(required & pos 0 (some file) None & info [] ~docv:"DATA.json" ~doc)
94+
in
95+
let template_file =
96+
let doc = "mustache template" in
97+
Arg.(required & pos 1 (some file) None & info [] ~docv:"TEMPLATE.mustache" ~doc)
98+
in
99+
Term.(const run $ json_file $ template_file),
100+
Term.info "mustache" ~doc ~man
101+
45102

46103
let () =
47-
match Sys.argv with
48-
| [| _ ; json_filename ; template_filename |]
49-
-> run json_filename template_filename
50-
| _ -> usage ()
104+
let open Cmdliner in
105+
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)