@@ -9,7 +9,7 @@ pub fn main() {
9
9
}
10
10
11
11
type Options {
12
- Options ( foo : String , bar : Int , baz : Bool , qux : Float )
12
+ Options ( foo : String , bar : Int , baz : Bool , qux : Float , names : List ( String ) )
13
13
}
14
14
15
15
pub fn decode_test ( ) {
@@ -55,6 +55,10 @@ pub fn decode_test() {
55
55
|> clad . decode ( [ "-q" , "2.5" ] )
56
56
|> should . equal ( Ok ( 2.5 ) )
57
57
58
+ clad . list ( "foo" , "f" , dynamic . string , clad . decoded )
59
+ |> clad . decode ( [ "-f" , "hello" , "--foo" , "world" ] )
60
+ |> should . equal ( Ok ( [ "world" , "hello" ] ) )
61
+
58
62
let decoder = {
59
63
use foo <- clad . string ( long_name : "foo" , short_name : "f" )
60
64
use bar <- clad . int ( long_name : "bar" , short_name : "b" )
@@ -64,38 +68,45 @@ pub fn decode_test() {
64
68
short_name : "q" ,
65
69
default : 0.0 ,
66
70
)
67
- clad . decoded ( Options ( foo : , bar : , baz : , qux : ) )
71
+ use names <- clad . list (
72
+ long_name : "name" ,
73
+ short_name : "n" ,
74
+ of : dynamic . string ,
75
+ )
76
+ clad . decoded ( Options ( foo : , bar : , baz : , qux : , names : ) )
68
77
}
69
78
70
79
// all fields set
71
- let args = [ "--foo" , "hello" , "-b" , "1" , "--baz" , "-q" , "2.5" ]
80
+ let args = [
81
+ "--foo" , "hello" , "-b" , "1" , "--baz" , "-q" , "2.5" , "-n" , "Lucy" , "-n" , "Joe" ,
82
+ ]
72
83
clad . decode ( decoder , args )
73
- |> should . equal ( Ok ( Options ( "hello" , 1 , True , 2.5 ) ) )
84
+ |> should . equal ( Ok ( Options ( "hello" , 1 , True , 2.5 , [ "Lucy" , "Joe" ] ) ) )
74
85
75
86
// using '='
76
- let args = [ "--foo=hello" , "-b=1" , "--baz" , "-q" , "2.5" ]
87
+ let args = [ "--foo=hello" , "-b=1" , "--baz" , "-q" , "2.5" , "-n" , "Lucy" ]
77
88
clad . decode ( decoder , args )
78
- |> should . equal ( Ok ( Options ( "hello" , 1 , True , 2.5 ) ) )
89
+ |> should . equal ( Ok ( Options ( "hello" , 1 , True , 2.5 , [ "Lucy" ] ) ) )
79
90
80
91
// missing field with default value
81
- let args = [ "--foo" , "hello" , "--bar" , "1" , "--baz" ]
92
+ let args = [ "--foo" , "hello" , "--bar" , "1" , "--baz" , "--name" , "Lucy" ]
82
93
clad . decode ( decoder , args )
83
- |> should . equal ( Ok ( Options ( "hello" , 1 , True , 0.0 ) ) )
94
+ |> should . equal ( Ok ( Options ( "hello" , 1 , True , 0.0 , [ "Lucy" ] ) ) )
84
95
85
96
// missing flag field
86
- let args = [ "--foo" , "hello" , "--bar" , "1" ]
97
+ let args = [ "--foo" , "hello" , "--bar" , "1" , "-n" , "Lucy" ]
87
98
clad . decode ( decoder , args )
88
- |> should . equal ( Ok ( Options ( "hello" , 1 , False , 0.0 ) ) )
99
+ |> should . equal ( Ok ( Options ( "hello" , 1 , False , 0.0 , [ "Lucy" ] ) ) )
89
100
90
101
// explicit setting flag to 'true'
91
- let args = [ "--foo" , "hello" , "--bar" , "1" , "-z" , "true" ]
102
+ let args = [ "--foo" , "hello" , "--bar" , "1" , "-z" , "true" , "-n" , "Lucy" ]
92
103
clad . decode ( decoder , args )
93
- |> should . equal ( Ok ( Options ( "hello" , 1 , True , 0.0 ) ) )
104
+ |> should . equal ( Ok ( Options ( "hello" , 1 , True , 0.0 , [ "Lucy" ] ) ) )
94
105
95
106
// explicit setting flag to 'false'
96
- let args = [ "--foo" , "hello" , "--bar" , "1" , "-z" , "false" ]
107
+ let args = [ "--foo" , "hello" , "--bar" , "1" , "-z" , "false" , "-n" , "Lucy" ]
97
108
clad . decode ( decoder , args )
98
- |> should . equal ( Ok ( Options ( "hello" , 1 , False , 0.0 ) ) )
109
+ |> should . equal ( Ok ( Options ( "hello" , 1 , False , 0.0 , [ "Lucy" ] ) ) )
99
110
}
100
111
101
112
pub fn decode_errors_test ( ) {
@@ -111,6 +122,19 @@ pub fn decode_errors_test() {
111
122
|> clad . decode ( [ "--foo" , "1" ] )
112
123
|> should . equal ( Error ( [ DecodeError ( "String" , "Int" , [ "--foo" ] ) ] ) )
113
124
125
+ clad . string ( long_name : "foo" , short_name : "f" , then : clad . decoded )
126
+ |> clad . decode ( [ "-f" , "hello" , "-f" , "world" ] )
127
+ |> should . equal ( Error ( [ DecodeError ( "String" , "List" , [ "--foo" ] ) ] ) )
128
+
129
+ clad . list (
130
+ long_name : "foo" ,
131
+ short_name : "f" ,
132
+ of : dynamic . string ,
133
+ then : clad . decoded ,
134
+ )
135
+ |> clad . decode ( [ "-f" , "1" , "-f" , "world" ] )
136
+ |> should . equal ( Error ( [ DecodeError ( "String" , "Int" , [ "-f" , "*" ] ) ] ) )
137
+
114
138
let decoder = {
115
139
use foo <- clad . string ( long_name : "foo" , short_name : "f" )
116
140
use bar <- clad . int ( long_name : "bar" , short_name : "b" )
@@ -120,7 +144,8 @@ pub fn decode_errors_test() {
120
144
short_name : "q" ,
121
145
default : 0.0 ,
122
146
)
123
- clad . decoded ( Options ( foo : , bar : , baz : , qux : ) )
147
+ use names <- clad . list ( "name" , "n" , dynamic . string )
148
+ clad . decoded ( Options ( foo : , bar : , baz : , qux : , names : ) )
124
149
}
125
150
126
151
// no fields
@@ -147,6 +172,14 @@ pub fn decode_errors_test() {
147
172
let args = [ "--foo" , "hello" , "-b" , "1" , "--baz" , "--qux" , "world" ]
148
173
clad . decode ( decoder , args )
149
174
|> should . equal ( Error ( [ DecodeError ( "Float" , "String" , [ "--qux" ] ) ] ) )
175
+
176
+ // list field wrong type
177
+ let args = [
178
+ "--foo" , "hello" , "-b" , "1" , "--baz" , "--qux" , "2.5" , "-n" , "Lucy" , "-n" ,
179
+ "100" ,
180
+ ]
181
+ clad . decode ( decoder , args )
182
+ |> should . equal ( Error ( [ DecodeError ( "String" , "Int" , [ "-n" , "*" ] ) ] ) )
150
183
}
151
184
152
185
pub fn add_bools_test ( ) {
0 commit comments