package ez_cmdliner

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file ezcmd.ml

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
(**************************************************************************)
(*                                                                        *)
(*   Typerex Libraries                                                    *)
(*                                                                        *)
(*   Copyright 2011-2017 OCamlPro SAS                                     *)
(*                                                                        *)
(*   All rights reserved.  This file is distributed under the terms of    *)
(*   the GNU Lesser General Public License version 2.1, with the          *)
(*   special exception on linking described in the file LICENSE.          *)
(*                                                                        *)
(**************************************************************************)

module TYPES = struct
  type block =
    [ `S of string
    | `P of string
    | `Pre of string
    | `I of string * string
    | `Noblank
    | `Blocks of block list ]

  type env = Cmdliner.Term.env_info

  type info = string list -> Cmdliner.Arg.info

  module Arg = struct
    type spec =
      (* Same as Arg. But they should only appear at most once on the
         command-line, or Cmdliner will complain. *)
      | Unit of (unit -> unit)
      | Bool of (bool -> unit)
      | Set of bool ref
      | Clear of bool ref
      | String of (string -> unit)
      | Set_string of string ref
      | Int of (int -> unit)
      | Set_int of int ref
      | Float of (float -> unit)
      | Set_float of float ref
      | Symbol of string list * (string -> unit)
      | File of (string -> unit)
      (* Anonymous arguments. `Anon(n,f)` means the anonymous argument
         at position `n`. `Anons f` means all the anonymous arguments. *)
      | Anon of int * (string -> unit)
      | Anons of (string list -> unit)
  end

  type arg_list = (string list * Arg.spec * info) list

  type command = {
    cmd_name : string;
    cmd_action : unit -> unit;
    cmd_args : arg_list;
    cmd_man : Cmdliner.Manpage.block list;
    cmd_doc : string;
  }
end

open TYPES
open TYPES.Arg
open Cmdliner

let info ?docs ?docv ?env doc = Cmdliner.Arg.info ?docs ?docv ?env ~doc

let env = Term.env_info

let rec term_of_list list =
  match list with
  | [] -> Term.(const ())
  | (args, action, info) :: tail -> (
      let x = term_of_list tail in
      let arg_info = info args in
      match action with
      | Unit f ->
          let term = Arg.(value & flag & arg_info) in
          let f () x = if x then f () in
          Term.(const f $ x $ term)
      | Bool f ->
          let term = Arg.(value & flag_all & arg_info) in
          let f () x = List.iter f x in
          Term.(const f $ x $ term)
      | Set r ->
          let term = Arg.(value & flag & arg_info) in
          let f () x = if x then r := true in
          Term.(const f $ x $ term)
      | Clear r ->
          let term = Arg.(value & flag & arg_info) in
          let f () x = if x then r := false in
          Term.(const f $ x $ term)
      | String f ->
          let term = Arg.(value & opt_all string [] & arg_info) in
          let f () x = List.iter f x in
          Term.(const f $ x $ term)
      | Set_string r ->
          let term = Arg.(value & opt (some string) None & arg_info) in
          let f () = function None -> () | Some s -> r := s in
          Term.(const f $ x $ term)
      | Int f ->
          let term = Arg.(value & opt (some int) None & arg_info) in
          let f () = function None -> () | Some s -> f s in
          Term.(const f $ x $ term)
      | Set_int r ->
          let term = Arg.(value & opt (some int) None & arg_info) in
          let f () = function None -> () | Some s -> r := s in
          Term.(const f $ x $ term)
      | Float f ->
          let term = Arg.(value & opt (some float) None & arg_info) in
          let f () = function None -> () | Some s -> f s in
          Term.(const f $ x $ term)
      | Set_float r ->
          let term = Arg.(value & opt (some float) None & arg_info) in
          let f () = function None -> () | Some s -> r := s in
          Term.(const f $ x $ term)
      | Symbol (symbols, f) ->
          let symbol = Arg.enum (List.map (fun s -> (s, s)) symbols) in
          let term = Arg.(value & opt (some symbol) None & arg_info) in
          let f () = function None -> () | Some s -> f s in
          Term.(const f $ x $ term)
      | File f ->
          let term = Arg.(value & opt_all file [] & arg_info) in
          let f () x = List.iter f x in
          Term.(const f $ x $ term)
      | Anon (n, f) ->
          let term = Arg.(value & pos n (some string) None & arg_info) in
          let f () = function None -> () | Some s -> f s in
          Term.(const f $ x $ term)
      | Anons f ->
          let term = Arg.(value & pos_all string [] & arg_info) in
          let f () x = f x in
          Term.(const f $ x $ term) )

let cmd_exits = Term.default_exits

let create_sub ?version sub =
  let man = sub.cmd_man in
  let exits = cmd_exits in
  let doc = sub.cmd_doc in
  ( Term.(const sub.cmd_action $ term_of_list sub.cmd_args),
    Term.info sub.cmd_name ?version ~doc ~sdocs:Manpage.s_common_options ~exits
      ~man )

let help more_topics man_format cmds topic =
  match topic with
  | None -> `Help (`Pager, None) (* help about the program. *)
  | Some topic -> (
      let topics = ("topics" :: List.map fst more_topics) @ cmds in
      let conv, _ = Cmdliner.Arg.enum (List.rev_map (fun s -> (s, s)) topics) in
      match conv topic with
      | `Error e -> `Error (false, e)
      | `Ok t when t = "topics" ->
          List.iter print_endline topics;
          `Ok ()
      | `Ok t when List.mem t cmds -> `Help (man_format, Some t)
      | `Ok t ->
          let page =
            ((topic, 7, "", "", ""), `S topic :: List.assoc t more_topics)
          in
          `Ok (Cmdliner.Manpage.print man_format Format.std_formatter page) )

let help_cmd ~name ~man ~topics =
  let topic =
    let doc = "The topic to get help on. `topics' lists the topics." in
    Arg.(value & pos 0 (some string) None & info [] ~docv:"TOPIC" ~doc)
  in
  let doc = Printf.sprintf "display help about %s and %s commands" name name in
  let man =
    [
      `S Manpage.s_description;
      `P "Prints help about darcs commands and other subjects...";
      `Blocks man;
    ]
  in
  ( Term.(ret (const (help topics) $ Arg.man_format $ Term.choice_names $ topic)),
    Term.info "help" ~doc ~exits:Term.default_exits ~man )

let default_cmd ~name ?version ~doc ~man =
  let sdocs = Manpage.s_common_options in
  let exits = Term.default_exits in
  ( Term.(ret (const (`Help (`Pager, None)))),
    Term.info name ?version ~doc ~sdocs ~exits ~man )

let main_with_subcommands ~name ?version ?default ~doc ~man ?(topics = []) subs
    =
  let cmds = List.map (create_sub ?version) subs in
  let default_cmd =
    match default with
    | None -> default_cmd ~name ?version ~doc ~man
    | Some cmd -> create_sub ?version cmd
  in
  let cmds =
    if List.exists (fun cmd -> cmd.cmd_name = "help") subs then cmds
    else cmds @ [ help_cmd ~name ~man ~topics ]
  in
  match Term.eval_choice ~catch:false default_cmd cmds with
  | `Ok () -> ()
  | t -> Term.exit t

let main ?version cmd =
  let cmd = create_sub ?version cmd in
  match Term.eval ~catch:false cmd with
  | `Ok () -> ()
  | `Error `Parse -> print_endline "toto"
  | t -> Term.exit t

module MANPAGE = Cmdliner.Manpage

let translate ?docs arg_list =
  List.map
    (fun (arg, spec, doc) ->
      let len = String.length arg in
      let arg =
        if len > 0 && arg.[0] = '-' then
          if len > 1 && arg.[1] = '-' then String.sub arg 2 (len - 2)
          else String.sub arg 1 (len - 1)
        else arg
      in
      ([ arg ], spec, info ?docs doc))
    arg_list

let translate_anon arg_anon =
  [
    ([], Anons (fun list -> List.iter arg_anon list), info "General arguments");
  ]

let parse ?name ?version ?(man = []) arg_list arg_anon arg_usage =
  let cmd_args = translate arg_list @ translate_anon arg_anon in
  let cmd_name =
    match name with None -> "COMMAND" | Some cmd_name -> cmd_name
  in
  let cmd =
    {
      cmd_name;
      cmd_doc = arg_usage;
      cmd_args;
      cmd_man = man;
      cmd_action = (fun () -> ());
    }
  in
  main ?version cmd