-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oclis-contract.ncl
159 lines (158 loc) · 3.3 KB
/
oclis-contract.ncl
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
# Record contract for an Oclis specifcation
let normString : String -> String =
std.function.compose
(std.string.replace_regex "\\s+" " ")
std.string.trim
in
let TrimmedString : _ -> String -> String = fun _label => normString
in
let NotRequiredIfList = fun label args =>
args
|> std.array.map
(
fun arg =>
if arg."type" == 'List-Text then
if std.record.has_field "optional" arg && arg."optional" == false then
std.contract.blame_with_message
(
"Incorrect values for argument "
++ arg.name
++ ": Arguments of type "
++ std.string.from_enum arg.type
++ " must always be optional"
)
label
else
arg
else
arg
)
in
let EitherNameOrShortName = fun label options =>
options
|> std.array.map
(
fun option =>
let nameIsSet =
std.record.has_field "name" option
&& option.name != ""
in
let shortNameIsSet =
std.record.has_field "shortName" option
&& option.shortName != ""
in
if !nameIsSet && !shortNameIsSet then
std.contract.blame_with_message
"Option must have either a name or a shortName"
label
else
option
)
in
let Argument = {
name | String,
description | String,
type
| [|
'Text,
'Int,
'Float,
'Bool,
'List-Text,
'List-Int,
'List-Float,
'List-Bool,
|],
"optional"
| optional
| Bool,
"default"
| optional
| String
| Number
| Bool
| Array String
| Array Number
| Array Bool,
}
in
let Option = {
name
| doc "The name of the option (without the leading --)"
| optional
| TrimmedString,
shortName
| doc "The short name of the option"
| optional
| std.string.Character,
description
| doc "The description of the option"
| TrimmedString,
argument
| doc "The argument of the option"
| optional
| Argument
| EitherNameOrShortName,
"optional"
| optional
| Bool,
"default"
| optional
| String
| Number
| Bool
| Array String
| Array Number
| Array Bool,
}
in
let Command = {
name
| doc "The name of the command"
| TrimmedString,
description
| doc "The description of the command"
| TrimmedString,
options
| doc "The list of options"
| optional
| Array Option,
arguments
| doc "The list of arguments"
| optional
| Array Argument
| NotRequiredIfList,
funcName
| doc "The function to be called"
| optional
| TrimmedString,
}
in
{
"_⛔️_"
| doc "A warning for the exported file to not edit it manually"
| default
=
normString
m%"
DO NOT EDIT THIS FILE MANUALLY!
It was generated from the `oclis.ncl` file.
"%,
name
| doc "The name of the CLI app"
| TrimmedString,
description
| doc "The description of the CLI app"
| TrimmedString,
version
| doc "The version of the CLI app"
| TrimmedString,
funcName
| doc "The function to be called"
| optional
| TrimmedString,
commands
| doc "The list of available sub-commands"
| optional
| Array Command,
}