package cmdlang

  1. Overview
  2. Docs
Declarative Command-line Parsing for OCaml

Install

dune-project
 Dependency

Authors

Maintainers

Sources

cmdlang-0.0.11.tbz
sha256=818cf017d4889b49121f36edf6c77b955105c2b2a79405592c5a51495cfbae4b
sha512=1179129fd7c65cb1767db5b7e2d2069454be8e16c0e9f010754b41b8af25e6e361ef5f0c3204fe7ee94e605cfdc3b406c41680664705dea53b488a3f727bcc2e

doc/src/cmdlang.ast/ast.ml.html

Source file ast.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
(*********************************************************************************)
(*  cmdlang - Declarative command-line parsing for OCaml                         *)
(*  SPDX-FileCopyrightText: 2024-2025 Mathieu Barbin <mathieu.barbin@gmail.com>  *)
(*  SPDX-License-Identifier: MIT                                                 *)
(*********************************************************************************)

type 'a or_error_msg = ('a, [ `Msg of string ]) result
type 'a of_string = string -> 'a or_error_msg
type 'a to_string = 'a -> string

module Nonempty_list = struct
  type 'a t = ( :: ) of 'a * 'a list
end

module Param = struct
  type 'a t =
    | Conv :
        { docv : string option
        ; of_string : 'a of_string
        ; to_string : 'a to_string
        }
        -> 'a t
    | String : string t
    | Int : int t
    | Float : float t
    | Bool : bool t
    | File : string t
    | Enum :
        { docv : string option
        ; choices : (string * 'a) Nonempty_list.t
        ; to_string : 'a -> string
        }
        -> 'a t
    | Comma_separated : 'a t -> 'a list t
end

module Arg = struct
  type 'a t =
    | Return : 'a -> 'a t
    | Map :
        { x : 'a t
        ; f : 'a -> 'b
        }
        -> 'b t
    | Both : 'a t * 'b t -> ('a * 'b) t
    | Apply :
        { f : ('a -> 'b) t
        ; x : 'a t
        }
        -> 'b t
    | Flag :
        { names : string Nonempty_list.t
        ; doc : string
        }
        -> bool t
    | Flag_count :
        { names : string Nonempty_list.t
        ; doc : string
        }
        -> int t
    | Named :
        { names : string Nonempty_list.t
        ; param : 'a Param.t
        ; docv : string option
        ; doc : string
        }
        -> 'a t
    | Named_multi :
        { names : string Nonempty_list.t
        ; param : 'a Param.t
        ; docv : string option
        ; doc : string
        }
        -> 'a list t
    | Named_opt :
        { names : string Nonempty_list.t
        ; param : 'a Param.t
        ; docv : string option
        ; doc : string
        }
        -> 'a option t
    | Named_with_default :
        { names : string Nonempty_list.t
        ; param : 'a Param.t
        ; default : 'a
        ; docv : string option
        ; doc : string
        }
        -> 'a t
    | Pos :
        { pos : int
        ; param : 'a Param.t
        ; docv : string option
        ; doc : string
        }
        -> 'a t
    | Pos_opt :
        { pos : int
        ; param : 'a Param.t
        ; docv : string option
        ; doc : string
        }
        -> 'a option t
    | Pos_with_default :
        { pos : int
        ; param : 'a Param.t
        ; default : 'a
        ; docv : string option
        ; doc : string
        }
        -> 'a t
    | Pos_all :
        { param : 'a Param.t
        ; docv : string option
        ; doc : string
        }
        -> 'a list t
end

module Command = struct
  type 'a t =
    | Make :
        { arg : 'a Arg.t
        ; summary : string
        ; readme : (unit -> string) option
        }
        -> 'a t
    | Group :
        { default : 'a Arg.t option
        ; summary : string
        ; readme : (unit -> string) option
        ; subcommands : (string * 'a t) list
        }
        -> 'a t

  let summary = function
    | Make { summary; _ } -> summary
    | Group { summary; _ } -> summary
  ;;

  let rec map : type a b. a t -> f:(a -> b) -> b t =
    fun a ~f ->
    match a with
    | Make { arg; summary; readme } ->
      Make { arg = Arg.Map { x = arg; f }; summary; readme }
    | Group { default; summary; readme; subcommands } ->
      Group
        { default = default |> Option.map (fun arg -> Arg.Map { x = arg; f })
        ; summary
        ; readme
        ; subcommands = subcommands |> List.map (fun (name, arg) -> name, map arg ~f)
        }
  ;;
end