aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/cli.lux
blob: d171dc89947635218d96fe1e33b48372be55075f (plain)
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
(;module:
  [lux #- not]
  (lux (control functor
                applicative
                monad)
       (data (coll (list #as list #open ("List/" Monoid<List> Monad<List>)))
             (text #as text #open ("Text/" Monoid<Text>))
             error
             (sum #as sum))
       [io]
       [macro #+ with-gensyms Functor<Lux> Monad<Lux>]
       (macro [ast]
              ["s" syntax #+ syntax: Syntax])))

## [Types]
(type: #export (CLI a)
  {#;doc "A command-line interface parser."}
  (-> (List Text) (Error [(List Text) a])))

## [Utils]
(def: (run' opt inputs)
  (All [a] (-> (CLI a) (List Text) (Error [(List Text) a])))
  (opt inputs))

## [Structures]
(struct: #export _ (Functor CLI)
  (def: (map f ma inputs)
    (case (ma inputs)
      (#;Left msg)              (#;Left msg)
      (#;Right [inputs' datum]) (#;Right [inputs' (f datum)]))))

(struct: #export _ (Applicative CLI)
  (def: functor Functor<CLI>)

  (def: (wrap a inputs)
    (#;Right [inputs a]))

  (def: (apply ff fa inputs)
    (case (ff inputs)
      (#;Right [inputs' f])
      (case (fa inputs')
        (#;Right [inputs'' a])
        (#;Right [inputs'' (f a)])

        (#;Left msg)
        (#;Left msg))

      (#;Left msg)
      (#;Left msg))
    ))

(struct: #export _ (Monad CLI)
  (def: applicative Applicative<CLI>)

  (def: (join mma inputs)
    (case (mma inputs)
      (#;Left msg)           (#;Left msg)
      (#;Right [inputs' ma]) (ma inputs'))))

## [Combinators]
(def: #export any
  {#;doc "Just returns the next input without applying any logic."}
  (CLI Text)
  (function [inputs]
    (case inputs
      (#;Cons arg inputs')
      (#;Right [inputs' arg])
      
      _
      (#;Left "Cannot parse empty arguments."))))

(def: #export (parse parser)
  {#;doc "Parses the next input with a parsing function."}
  (All [a] (-> (-> Text (Error a)) (CLI a)))
  (function [inputs]
    (case inputs
      (#;Cons arg inputs')
      (case (parser arg)
        (#;Right value)
        (#;Right [inputs' value])

        (#;Left parser-error)
        (#;Left parser-error))
      
      _
      (#;Left "Cannot parse empty arguments."))))

(def: #export (option names)
  {#;doc "Checks that a given option (with multiple possible names) has a value."}
  (-> (List Text) (CLI Text))
  (function [inputs]
    (let [[pre post] (list;split-with (. ;not (list;member? text;Eq<Text> names)) inputs)]
      (case post
        #;Nil
        (#;Left ($_ Text/append "Missing option (" (text;join-with " " names) ")"))

        (^ (list& _ value post'))
        (#;Right [(List/append pre post') value])

        _
        (#;Left ($_ Text/append "Option lacks value (" (text;join-with " " names) ")"))
        ))))

(def: #export (flag names)
  {#;doc "Checks that a given flag (with multiple possible names) is set."}
  (-> (List Text) (CLI Bool))
  (function [inputs]
    (let [[pre post] (list;split-with (. ;not (list;member? text;Eq<Text> names)) inputs)]
      (case post
        #;Nil
        (#;Right [pre false])

        (#;Cons _ post')
        (#;Right [(List/append pre post') true])))))

(def: #export end
  {#;doc "Ensures there are no more inputs."}
  (CLI Unit)
  (function [inputs]
    (case inputs
      #;Nil (#;Right [inputs []])
      _     (#;Left (Text/append "Unknown parameters: " (text;join-with " " inputs))))))

(def: #export (after param subject)
  (All [p s] (-> (CLI p) (CLI s) (CLI s)))
  (do Monad<CLI>
    [_ param]
    subject))

(def: #export (before param subject)
  (All [p s] (-> (CLI p) (CLI s) (CLI s)))
  (do Monad<CLI>
    [output subject
     _ param]
    (wrap output)))

(def: #export (assert message test)
  {#;doc "Fails with the given message if the test is false."}
  (-> Text Bool (CLI Unit))
  (function [inputs]
    (if test
      (#;Right [inputs []])
      (#;Left message))))

(def: #export (opt opt)
  {#;doc "Optionality combinator."}
  (All [a]
    (-> (CLI a) (CLI (Maybe a))))
  (function [inputs]
    (case (opt inputs)
      (#;Left _)            (#;Right [inputs #;None])
      (#;Right [inputs' x]) (#;Right [inputs' (#;Some x)]))))

(def: #export (seq optL optR)
  {#;doc "Sequencing combinator."}
  (All [a b] (-> (CLI a) (CLI b) (CLI [a b])))
  (do Monad<CLI>
    [l optL
     r optR]
    (wrap [l r])))

(def: #export (alt optL optR)
  {#;doc "Heterogeneous alternative combinator."}
  (All [a b] (-> (CLI a) (CLI b) (CLI (| a b))))
  (function [inputs]
    (case (optL inputs)
      (#;Left msg)
      (case (optR inputs)
        (#;Left _)
        (#;Left msg)

        (#;Right [inputs' r])
        (#;Right [inputs' (sum;right r)]))

      (#;Right [inputs' l])
      (#;Right [inputs' (sum;left l)]))))

(def: #export (not opt)
  {#;doc "The opposite of the given CLI."}
  (All [a] (-> (CLI a) (CLI Unit)))
  (function [inputs]
    (case (opt inputs)
      (#;Left msg)
      (#;Right [inputs []])
      
      _
      (#;Left "Expected to fail; yet succeeded."))))

(def: #export (some opt)
  {#;doc "0-or-more combinator."}
  (All [a]
    (-> (CLI a) (CLI (List a))))
  (function [inputs]
    (case (opt inputs)
      (#;Left _)            (#;Right [inputs (list)])
      (#;Right [inputs' x]) (run' (do Monad<CLI>
                                    [xs (some opt)]
                                    (wrap (list& x xs)))
                                  inputs'))))

(def: #export (many opt)
  {#;doc "1-or-more combinator."}
  (All [a]
    (-> (CLI a) (CLI (List a))))
  (do Monad<CLI>
    [x opt
     xs (some opt)]
    (wrap (list& x xs))))

(def: #export (either pl pr)
  {#;doc "Homogeneous alternative combinator."}
  (All [a]
    (-> (CLI a) (CLI a) (CLI a)))
  (function [inputs]
    (case (pl inputs)
      (#;Left _) (pr inputs)
      output     output)))

(def: #export (run opt inputs)
  (All [a] (-> (CLI a) (List Text) (Error a)))
  (case (opt inputs)
    (#;Left msg)
    (#;Left msg)

    (#;Right [_ value])
    (#;Right value)))

## [Syntax]
(type: Program-Args
  (#Raw-Program-Args Text)
  (#Parsed-Program-Args (List [AST AST])))

(def: program-args^
  (Syntax Program-Args)
  (s;alt s;local-symbol
         (s;form (s;some (s;either (do s;Monad<Syntax>
                                     [name s;local-symbol]
                                     (wrap [(ast;symbol ["" name]) (` any)]))
                                   (s;tuple (s;seq s;any s;any)))))))

(syntax: #export (program: [args program-args^] body)
  {#;doc (doc "Defines the entry-point to a program (similar to the \"main\" function/method in other programming languages)."
              "Can take a list of all the input parameters to the program, or can destructure them using CLI-option combinators from the lux/cli module."
              (program: all-args
                (do Monad<IO>
                  [foo init-program
                   bar (do-something all-args)]
                  (wrap [])))

              (program: (name)
                (io (log! (Text/append "Hello, " name))))

              (program: ([config config^])
                (do Monad<IO>
                  [data (init-program config)]
                  (do-something data))))}
  (case args
    (#Raw-Program-Args args)
    (wrap (list (` (;_lux_program (~ (ast;symbol ["" args]))
                                  (~ body)))))
    
    (#Parsed-Program-Args args)
    (with-gensyms [g!args g!_ g!output g!message]
      (wrap (list (` (;_lux_program (~ g!args)
                                    (case ((: (;;CLI (io;IO Unit))
                                              (do ;;Monad<CLI>
                                                [(~@ (|> args
                                                         (List/map (function [[binding parser]]
                                                                     (list binding parser)))
                                                         List/join))
                                                 (~ g!_) ;;end]
                                                ((~' wrap) (~ body))))
                                           (~ g!args))
                                      (#;Right [(~ g!_) (~ g!output)])
                                      (~ g!output)

                                      (#;Left (~ g!message))
                                      (error! (~ g!message))
                                      )))
                  )))
    ))