File: example_chorus.ml

package info (click to toggle)
cmdliner 2.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 704 kB
  • sloc: ml: 7,287; sh: 146; makefile: 108
file content (38 lines) | stat: -rw-r--r-- 1,173 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
(*---------------------------------------------------------------------------
   Copyright (c) 2011 The cmdliner programmers. All rights reserved.
   SPDX-License-Identifier: CC0-1.0
  ---------------------------------------------------------------------------*)

(* Implementation of the command *)

let chorus ~count msg = for i = 1 to count do print_endline msg done

(* Command line interface *)

open Cmdliner
open Cmdliner.Term.Syntax

let count =
  let doc = "Repeat the message $(docv) times." in
  Arg.(value & opt int 10 & info ["c"; "count"] ~doc ~docv:"COUNT")

let msg =
  let env =
    let doc = "Overrides the default message to print." in
    Cmd.Env.info "CHORUS_MSG" ~doc
  in
  let doc = "The message to print." in
  Arg.(value & pos 0 string "Revolt!" & info [] ~env ~doc ~docv:"MSG")

let chorus_cmd =
  let doc = "Print a customizable message repeatedly" in
  let man = [
    `S Manpage.s_bugs;
    `P "Email bug reports to <bugs@example.org>." ]
  in
  Cmd.make (Cmd.info "chorus" ~version:"v2.1.0" ~doc ~man) @@
  let+ count and+ msg in
  chorus ~count msg

let main () = Cmd.eval chorus_cmd
let () = if !Sys.interactive then () else exit (main ())