src/share/args.bend
ezx@1.11.0.0 · 154 lines · 19 definitions
1
2
3
4
5
6
7
8
9
10
11import Base
12
13
14def nonempty(ss: List<&2, String>) -> List<&2, String>:
15 match ss:
16 case Nil{}:
17 Nil{}
18 case Con{+h, t}:
19 +more = nonempty(t)
20 Bool.pick(List<&2, String>, String.is_empty(h), more, h <> more)
21
22
23def text_of(res: Result<&1, &1, U32 & String, String>) -> String:
24 match res:
25 case Fail{_e}:
26 ""
27 case Done{s}:
28 s
29
30
31
32def copy(xs: List<&1, String>) -> List<&2, String>:
33 match xs:
34 case Nil{}:
35 Nil{}
36 case Con{h, t}:
37 h <> copy(t)
38
39
40
41def split(xs: List<&2, String>) -> String & List<&2, String>:
42 match xs:
43 case Nil{}:
44 ("", Nil{})
45 case Con{h, t}:
46 (h, t)
47
48
49
50def at(xs: List<&2, String>, index: Nat) -> String:
51 match xs index:
52 case Nil{} _m:
53 ""
54 case Con{h, _t} 0n:
55 h
56 case Con{_h, t} 1n+p:
57 at(t, p)
58
59
60def rest(as: List<&2, String>) -> List<&2, String>:
61 match as:
62 case Nil{}:
63 Nil{}
64 case Con{_h, t}:
65 t
66
67
68def all() -> IO(List<&2, String>):
69 do IO<List<&2, String>>:
70 xs : List<&1, String> <- IO.args()
71 return rest(copy(xs))
72
73
74
75def run.line(+entry: String, as: List<&2, String>) -> List<&2, String>:
76 "bend" <> (entry <> rest(as))
77
78
79def drop(xs: List<&2, String>, count: Nat) -> List<&2, String>:
80 match xs count:
81 case Nil{} _m:
82 Nil{}
83 case Con{+h, +t} 0n:
84 h <> t
85 case Con{_h, t} 1n+p:
86 drop(t, p)
87
88
89def undash(xs: List<&2, String>) -> List<&2, String>:
90 match xs:
91 case Nil{}:
92 Nil{}
93 case Con{+h, +t}:
94 Bool.pick(List<&2, String>, String.eq(h, "--"), t, h <> t)
95
96
97
98
99def tool.lead(xs: List<&2, String>) -> List<&2, String>:
100 match xs:
101 case Nil{}:
102 Nil{}
103 case Con{+h, +t}:
104 Bool.pick(List<&2, String>, String.eq(h, "--entry"), rest(t), h <> t)
105
106
107
108def tool.rest(as: List<&2, String>) -> List<&2, String>:
109 undash(rest(tool.lead(drop(as, 2n))))
110
111
112
113def tool.line(+target: String) -> List<&2, String>:
114 Bool.pick(List<&2, String>, String.is_empty(target), ["tool", "run"],
115 ["tool", "run", target])
116
117
118def tool.flagged(+file: String, +target: String) -> List<&2, String>:
119 Bool.pick(List<&2, String>, String.is_empty(target), ["tool", "run", "--entry", file],
120 ["tool", "run", "--entry", file, target])
121
122
123
124def tool.parse(lead: Bool, +as: List<&2, String>) -> List<&2, String>:
125 match lead:
126 case True{}:
127 tool.flagged(at(as, 3n), at(as, 4n))
128 case False{}:
129 tool.line(at(as, 2n))
130
131
132def is.tool.run(+as: List<&2, String>) -> Bool:
133 Bool.and(String.eq(at(as, 0n), "tool"), String.eq(at(as, 1n), "run"))
134
135
136def argv.tool(hit: Bool, +as: List<&2, String>) -> List<&2, String>:
137 match hit:
138 case True{}:
139 tool.parse(String.eq(at(as, 2n), "--entry"), as)
140 case False{}:
141 as
142
143
144def argv.run(hit: Bool, +as: List<&2, String>) -> List<&2, String>:
145 match hit:
146 case True{}:
147 ["run"]
148 case False{}:
149 argv.tool(is.tool.run(as), as)
150
151
152
153def argv.of(+as: List<&2, String>) -> List<&2, String>:
154 argv.run(String.eq(at(as, 0n), "run"), as)