Skip to content

Commit 2f525ab

Browse files
committed
Update cmdliner to groups fork
Signed-off-by: Rudi Grinberg <[email protected]>
1 parent bdf8b90 commit 2f525ab

23 files changed

+183
-38
lines changed

vendor/cmdliner/src/cmdliner.ml

Lines changed: 96 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(*---------------------------------------------------------------------------
22
Copyright (c) 2011 Daniel C. Bünzli. All rights reserved.
33
Distributed under the ISC license, see terms at the end of the file.
4-
cmdliner v1.0.4-3-ga5ff0e8
4+
cmdliner v1.0.4-12-gfb7fa13
55
---------------------------------------------------------------------------*)
66

77
module Manpage = Cmdliner_manpage
@@ -216,7 +216,7 @@ module Term = struct
216216
?err:(err_ppf = Format.err_formatter)
217217
?(catch = true) ?(env = env_default) ?(argv = Sys.argv) ((al, f), ti) =
218218
let term = Cmdliner_info.term_add_args ti al in
219-
let ei = Cmdliner_info.eval ~term ~main:term ~choices:[] ~env in
219+
let ei = Cmdliner_info.eval ~env (Simple term) in
220220
let args = remove_exec argv in
221221
let ei, res = term_eval ~catch ei f args in
222222
do_result help_ppf err_ppf ei res
@@ -257,20 +257,111 @@ module Term = struct
257257
let main = fst main_f in
258258
match choose_term main_f choices_f (remove_exec argv) with
259259
| Error err ->
260-
let ei = Cmdliner_info.eval ~term:main ~main ~choices ~env in
260+
let ei = Cmdliner_info.eval ~env
261+
(Sub_command { term = main ; path = [main] ; main
262+
; sibling_terms = choices})
263+
in
261264
Cmdliner_msg.pp_err_usage err_ppf ei ~err_lines:false ~err;
262265
`Error `Parse
263266
| Ok ((chosen, f), args) ->
264-
let ei = Cmdliner_info.eval ~term:chosen ~main ~choices ~env in
267+
let ei = Cmdliner_info.eval ~env
268+
(Sub_command { term = chosen ; path = [main; chosen] ; main;
269+
sibling_terms = choices }) in
270+
let ei, res = term_eval ~catch ei f args in
271+
do_result help_ppf err_ppf ei res
272+
273+
module Group = struct
274+
type 'a node =
275+
| Term of 'a Cmdliner_term.t
276+
| Group of 'a t list
277+
278+
and 'a t = 'a node * info
279+
280+
let term_add_args (al, f) info =
281+
Cmdliner_info.term_add_args info al
282+
283+
let rec add_args (node, info) =
284+
match node with
285+
| Term (al, f) -> (Term (al, f), term_add_args (al, f) info)
286+
| Group subs -> (Group (List.map add_args subs), info)
287+
288+
let (>>=) res f =
289+
match res with
290+
| Error e -> Error e
291+
| Ok x -> f x
292+
293+
let parse_arg_cmd = function
294+
| [] -> Error `No_args
295+
| cmd :: args ->
296+
if String.length cmd >= 1 && cmd.[0] = '-' then
297+
Error `No_args
298+
else
299+
Ok (cmd, args)
300+
301+
let cmd_name (_, info) = Cmdliner_info.term_name info
302+
303+
let one_of (cmd, choices, path, args) =
304+
match List.find (fun t -> cmd_name t = cmd) choices with
305+
| exception Not_found -> Error (`Invalid_command (cmd, path, choices))
306+
| (choice, info) -> Ok ((choice, info), choices, info :: path, args)
307+
308+
let try_one_of choices path args =
309+
match parse_arg_cmd args with
310+
| Ok (cmd, args) -> one_of (cmd, choices, path, args)
311+
| Error `No_args -> Error (`No_args (path, choices))
312+
313+
let rec try_choose_term choices path args =
314+
try_one_of choices path args >>= choose_term
315+
316+
and choose_term ((t, info), choices, path, args) =
317+
match t with
318+
| Term t -> Ok ((t, info), choices, path, args)
319+
| Group subs -> try_choose_term subs path args
320+
321+
let choose_term main choices args =
322+
match parse_arg_cmd args with
323+
| Error `No_args -> Ok (main, choices, [], args)
324+
| Ok (cmd, args) -> one_of (cmd, choices, [snd main], args) >>= choose_term
325+
326+
let eval
327+
?help:(help_ppf = Format.std_formatter)
328+
?err:(err_ppf = Format.err_formatter)
329+
?(catch = true) ?(env = env_default) ?(argv = Sys.argv) main choices =
330+
let choices_f = List.map add_args choices in
331+
let to_term_f ((al, f), ti) = Cmdliner_info.term_add_args ti al, f in
332+
let main_args = fst main in
333+
let main_f = to_term_f main in
334+
let main = fst main_f in
335+
match choose_term (main_args, (fst main_f)) choices_f (remove_exec argv) with
336+
| Error (`No_args (path, choices)) ->
337+
let sibling_terms = List.map snd choices in
338+
let ei = Cmdliner_info.eval ~env
339+
(Sub_command { term = main ; path ; main ; sibling_terms}) in
340+
let _, _, ei = add_stdopts ei in
341+
Cmdliner_docgen.pp_man ~errs:err_ppf `Auto help_ppf ei;
342+
`Help
343+
| Error (`Invalid_command (maybe, path, _choices)) ->
344+
let err = Cmdliner_base.err_unknown ~kind:"command" maybe ~hints:[] in
345+
let sibling_terms = List.map snd choices in
346+
let ei = Cmdliner_info.eval ~env
347+
(Sub_command { term = main ; path ; main ; sibling_terms})
348+
in
349+
Cmdliner_msg.pp_err_usage err_ppf ei ~err_lines:false ~err;
350+
`Error `Parse
351+
| Ok (((_, f), info), sibling_terms, path, args) ->
352+
let sibling_terms = List.map snd sibling_terms in
353+
let ei = Cmdliner_info.eval ~env
354+
(Sub_command { main ; term = info ; path ; sibling_terms }) in
265355
let ei, res = term_eval ~catch ei f args in
266356
do_result help_ppf err_ppf ei res
357+
end
267358

268359
let eval_peek_opts
269360
?(version_opt = false) ?(env = env_default) ?(argv = Sys.argv)
270361
((args, f) : 'a t) =
271362
let version = if version_opt then Some "dummy" else None in
272363
let term = Cmdliner_info.term ~args ?version "dummy" in
273-
let ei = Cmdliner_info.eval ~term ~main:term ~choices:[] ~env in
364+
let ei = Cmdliner_info.eval ~env (Simple term) in
274365
(term_eval_peek_opts ei f (remove_exec argv) :> 'a option * 'a result)
275366

276367
(* Exits *)

vendor/cmdliner/src/cmdliner.mli

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(*---------------------------------------------------------------------------
22
Copyright (c) 2011 Daniel C. Bünzli. All rights reserved.
33
Distributed under the ISC license, see terms at the end of the file.
4-
cmdliner v1.0.4-3-ga5ff0e8
4+
cmdliner v1.0.4-12-gfb7fa13
55
---------------------------------------------------------------------------*)
66

77
(** Declarative definition of command line interfaces.
@@ -22,7 +22,7 @@
2222
use. Open the module to use it, it defines only three modules in
2323
your scope.
2424
25-
{e v1.0.4-3-ga5ff0e8 — {{:http://erratique.ch/software/cmdliner }homepage}} *)
25+
{e v1.0.4-12-gfb7fa13 — {{:http://erratique.ch/software/cmdliner }homepage}} *)
2626

2727
(** {1:top Interface} *)
2828

@@ -382,6 +382,21 @@ module Term : sig
382382
is unspecified the "main" term [t] is evaluated. [i] defines the
383383
name and man page of the program. *)
384384

385+
module Group : sig
386+
type 'a term
387+
388+
type 'a node =
389+
| Term of 'a term
390+
| Group of 'a t list
391+
392+
and 'a t = 'a node * info
393+
394+
val eval :
395+
?help:Format.formatter -> ?err:Format.formatter -> ?catch:bool ->
396+
?env:(string -> string option) -> ?argv:string array ->
397+
'a term * info -> 'a t list -> 'a result
398+
end with type 'a term := 'a t
399+
385400
val eval_peek_opts :
386401
?version_opt:bool -> ?env:(string -> string option) ->
387402
?argv:string array -> 'a t -> 'a option * 'a result
@@ -1255,7 +1270,7 @@ let cmd =
12551270
`S Manpage.s_see_also; `P "$(b,rmdir)(1), $(b,unlink)(2)" ]
12561271
in
12571272
Term.(const rm $ prompt $ recursive $ files),
1258-
Term.info "rm" ~version:"v1.0.4-3-ga5ff0e8" ~doc ~exits:Term.default_exits ~man
1273+
Term.info "rm" ~version:"v1.0.4-12-gfb7fa13" ~doc ~exits:Term.default_exits ~man
12591274
12601275
let () = Term.(exit @@ eval cmd)
12611276
]}
@@ -1325,7 +1340,7 @@ let cmd =
13251340
`P "Email them to <hehey at example.org>."; ]
13261341
in
13271342
Term.(ret (const cp $ verbose $ recurse $ force $ srcs $ dest)),
1328-
Term.info "cp" ~version:"v1.0.4-3-ga5ff0e8" ~doc ~exits ~man ~man_xrefs
1343+
Term.info "cp" ~version:"v1.0.4-12-gfb7fa13" ~doc ~exits ~man ~man_xrefs
13291344
13301345
let () = Term.(exit @@ eval cmd)
13311346
]}
@@ -1598,7 +1613,7 @@ let default_cmd =
15981613
let exits = Term.default_exits in
15991614
let man = help_secs in
16001615
Term.(ret (const (fun _ -> `Help (`Pager, None)) $ copts_t)),
1601-
Term.info "darcs" ~version:"v1.0.4-3-ga5ff0e8" ~doc ~sdocs ~exits ~man
1616+
Term.info "darcs" ~version:"v1.0.4-12-gfb7fa13" ~doc ~sdocs ~exits ~man
16021617
16031618
let cmds = [initialize_cmd; record_cmd; help_cmd]
16041619

vendor/cmdliner/src/cmdliner_arg.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(*---------------------------------------------------------------------------
22
Copyright (c) 2011 Daniel C. Bünzli. All rights reserved.
33
Distributed under the ISC license, see terms at the end of the file.
4-
cmdliner v1.0.4-3-ga5ff0e8
4+
cmdliner v1.0.4-12-gfb7fa13
55
---------------------------------------------------------------------------*)
66

77
let rev_compare n0 n1 = compare n1 n0

vendor/cmdliner/src/cmdliner_arg.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(*---------------------------------------------------------------------------
22
Copyright (c) 2011 Daniel C. Bünzli. All rights reserved.
33
Distributed under the ISC license, see terms at the end of the file.
4-
cmdliner v1.0.4-3-ga5ff0e8
4+
cmdliner v1.0.4-12-gfb7fa13
55
---------------------------------------------------------------------------*)
66

77
(** Command line arguments as terms. *)

vendor/cmdliner/src/cmdliner_base.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(*---------------------------------------------------------------------------
22
Copyright (c) 2011 Daniel C. Bünzli. All rights reserved.
33
Distributed under the ISC license, see terms at the end of the file.
4-
cmdliner v1.0.4-3-ga5ff0e8
4+
cmdliner v1.0.4-12-gfb7fa13
55
---------------------------------------------------------------------------*)
66

77
(* Invalid argument strings *)

vendor/cmdliner/src/cmdliner_base.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(*---------------------------------------------------------------------------
22
Copyright (c) 2011 Daniel C. Bünzli. All rights reserved.
33
Distributed under the ISC license, see terms at the end of the file.
4-
cmdliner v1.0.4-3-ga5ff0e8
4+
cmdliner v1.0.4-12-gfb7fa13
55
---------------------------------------------------------------------------*)
66

77
(** A few helpful base definitions. *)

vendor/cmdliner/src/cmdliner_cline.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(*---------------------------------------------------------------------------
22
Copyright (c) 2011 Daniel C. Bünzli. All rights reserved.
33
Distributed under the ISC license, see terms at the end of the file.
4-
cmdliner v1.0.4-3-ga5ff0e8
4+
cmdliner v1.0.4-12-gfb7fa13
55
---------------------------------------------------------------------------*)
66

77
(* A command line stores pre-parsed information about the command

vendor/cmdliner/src/cmdliner_cline.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(*---------------------------------------------------------------------------
22
Copyright (c) 2011 Daniel C. Bünzli. All rights reserved.
33
Distributed under the ISC license, see terms at the end of the file.
4-
cmdliner v1.0.4-3-ga5ff0e8
4+
cmdliner v1.0.4-12-gfb7fa13
55
---------------------------------------------------------------------------*)
66

77
(** Command lines. *)

vendor/cmdliner/src/cmdliner_docgen.ml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(*---------------------------------------------------------------------------
22
Copyright (c) 2011 Daniel C. Bünzli. All rights reserved.
33
Distributed under the ISC license, see terms at the end of the file.
4-
cmdliner v1.0.4-3-ga5ff0e8
4+
cmdliner v1.0.4-12-gfb7fa13
55
---------------------------------------------------------------------------*)
66

77
let rev_compare n0 n1 = compare n1 n0
@@ -61,9 +61,11 @@ let term_info_subst ei = function
6161
let invocation ?(sep = ' ') ei = match Cmdliner_info.eval_kind ei with
6262
| `Simple | `Multiple_main -> term_name (Cmdliner_info.eval_main ei)
6363
| `Multiple_sub ->
64-
strf "%s%c%s"
65-
Cmdliner_info.(term_name @@ eval_main ei) sep
66-
Cmdliner_info.(term_name @@ eval_term ei)
64+
let sep = String.make 1 sep in
65+
Cmdliner_info.eval_parents_invocation_order ei
66+
|> List.map Cmdliner_info.term_name
67+
|> String.concat sep
68+
|> strf "%s"
6769

6870
let plain_invocation ei = invocation ei
6971
let invocation ?sep ei = esc @@ invocation ?sep ei

vendor/cmdliner/src/cmdliner_docgen.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(*---------------------------------------------------------------------------
22
Copyright (c) 2011 Daniel C. Bünzli. All rights reserved.
33
Distributed under the ISC license, see terms at the end of the file.
4-
cmdliner v1.0.4-3-ga5ff0e8
4+
cmdliner v1.0.4-12-gfb7fa13
55
---------------------------------------------------------------------------*)
66

77
val plain_invocation : Cmdliner_info.eval -> string

0 commit comments

Comments
 (0)