Skip to content

Commit fa53876

Browse files
author
Ryan Miville
committed
fix flag
1 parent 67afa72 commit fa53876

File tree

4 files changed

+54
-46
lines changed

4 files changed

+54
-46
lines changed

gleam.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name = "clad"
2-
version = "0.2.1"
2+
version = "0.3.0"
33

44
# Fill out these fields if you intend to generate HTML documentation or publish
55
# your project to the Hex package manager.

src/clad.gleam

+25-19
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@
152152
//// ```
153153

154154
import decode/zero.{type Decoder}
155-
import gleam/bool
156155
import gleam/dict.{type Dict}
157156
import gleam/dynamic.{type Dynamic}
158157
import gleam/float
@@ -239,34 +238,41 @@ pub fn positional_arguments(
239238
/// let result = clad.decode([], decoder)
240239
/// assert result == Ok(False)
241240
/// ```
242-
pub fn flag() -> Decoder(Bool) {
243-
zero.bool
244-
|> zero.optional
245-
|> zero.map(option.unwrap(_, False))
241+
pub fn flag(
242+
long_name: String,
243+
short_name: String,
244+
next: fn(Bool) -> Decoder(final),
245+
) -> Decoder(final) {
246+
use value <- optional_field(long_name, zero.bool)
247+
case value {
248+
Some(v) -> next(v)
249+
None -> zero.optional_field(short_name, False, zero.bool, next)
250+
}
246251
}
247252

248253
fn optional_field(
249254
field_name: name,
250255
field_decoder: Decoder(t),
251256
next: fn(Option(t)) -> Decoder(final),
252257
) -> Decoder(final) {
253-
let decoding_function = fn(data: Dynamic) {
254-
use <- bool.guard(dynamic.classify(data) == "Nil", Ok(None))
258+
zero.optional_field(field_name, None, zero.optional(field_decoder), next)
259+
// let decoding_function = fn(data: Dynamic) {
260+
// use <- bool.guard(dynamic.classify(data) == "Nil", Ok(None))
255261

256-
case zero.run(data, zero.optional(field_decoder)) {
257-
Ok(None) -> {
258-
case zero.run(data, field_decoder) {
259-
Ok(v) -> Ok(Some(v))
260-
Error(_) -> Ok(None)
261-
}
262-
}
263-
other -> other
264-
}
265-
}
262+
// case zero.run(data, zero.optional(field_decoder)) {
263+
// Ok(None) -> {
264+
// case zero.run(data, field_decoder) {
265+
// Ok(v) -> Ok(Some(v))
266+
// Error(_) -> Ok(None)
267+
// }
268+
// }
269+
// other -> other
270+
// }
271+
// }
266272

267-
let decoder = zero.new_primitive_decoder(decoding_function, None)
273+
// let decoder = zero.new_primitive_decoder(decoding_function, None)
268274

269-
zero.field(field_name, decoder, next)
275+
// zero.field(field_name, decoder, next)
270276
}
271277

272278
/// Decode a command line option by either a long name or short name

test/clad_test.gleam

+27-25
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import clad
22
import decode/zero
33
import gleam/dynamic.{DecodeError}
4-
import gleam/option.{None, Some}
54
import gleeunit
65
import gleeunit/should
76

@@ -26,11 +25,11 @@ pub fn decode_test() {
2625
|> clad.decode(["-b", "1"], _)
2726
|> should.equal(Ok(1))
2827

29-
zero.field("z", clad.flag(), zero.success)
28+
clad.flag("baz", "z", zero.success)
3029
|> clad.decode(["-z"], _)
3130
|> should.equal(Ok(True))
3231

33-
zero.field("z", clad.flag(), zero.success)
32+
clad.flag("baz", "z", zero.success)
3433
|> clad.decode([], _)
3534
|> should.equal(Ok(False))
3635

@@ -45,7 +44,7 @@ pub fn decode_test() {
4544
let decoder = {
4645
use foo <- clad.opt("foo", "f", zero.string)
4746
use bar <- clad.opt("bar", "b", zero.int)
48-
use baz <- clad.opt("baz", "z", clad.flag())
47+
use baz <- clad.flag("baz", "z")
4948
use qux <- clad.opt("qux", "q", zero.float)
5049
use names <- clad.positional_arguments
5150
zero.success(Options(foo:, bar:, baz:, qux:, names:))
@@ -82,7 +81,7 @@ pub fn decode_test() {
8281
pub fn decode_errors_test() {
8382
zero.field("f", zero.string, zero.success)
8483
|> clad.decode(["--bar", "hello"], _)
85-
|> should.equal(Error([DecodeError("String", "Nil", ["f"])]))
84+
|> should.equal(Error([DecodeError("Field", "Nothing", ["f"])]))
8685

8786
zero.field("foo", zero.string, zero.success)
8887
|> clad.decode(["--foo", "1"], _)
@@ -91,7 +90,7 @@ pub fn decode_errors_test() {
9190
let decoder = {
9291
use foo <- clad.opt("foo", "f", zero.string)
9392
use bar <- clad.opt("bar", "b", zero.int)
94-
use baz <- clad.opt("baz", "z", clad.flag())
93+
use baz <- clad.flag("baz", "z")
9594
use qux <- clad.opt("qux", "q", zero.float)
9695
use names <- clad.positional_arguments
9796
zero.success(Options(foo:, bar:, baz:, qux:, names:))
@@ -102,9 +101,9 @@ pub fn decode_errors_test() {
102101
clad.decode(args, decoder)
103102
|> should.equal(
104103
Error([
105-
DecodeError("String", "Nil", ["f"]),
106-
DecodeError("Int", "Nil", ["b"]),
107-
DecodeError("Float", "Nil", ["q"]),
104+
DecodeError("Field", "Nothing", ["f"]),
105+
DecodeError("Field", "Nothing", ["b"]),
106+
DecodeError("Field", "Nothing", ["q"]),
108107
]),
109108
)
110109

@@ -113,16 +112,19 @@ pub fn decode_errors_test() {
113112
clad.decode(args, decoder)
114113
|> should.equal(
115114
Error([
116-
DecodeError("String", "Nil", ["f"]),
117-
DecodeError("Float", "Nil", ["q"]),
115+
DecodeError("Field", "Nothing", ["f"]),
116+
DecodeError("Field", "Nothing", ["q"]),
118117
]),
119118
)
120119

121120
// missing second field
122121
let args = ["--foo", "hello"]
123122
clad.decode(args, decoder)
124123
|> should.equal(
125-
Error([DecodeError("Int", "Nil", ["b"]), DecodeError("Float", "Nil", ["q"])]),
124+
Error([
125+
DecodeError("Field", "Nothing", ["b"]),
126+
DecodeError("Field", "Nothing", ["q"]),
127+
]),
126128
)
127129

128130
// wrong type
@@ -131,7 +133,7 @@ pub fn decode_errors_test() {
131133
|> should.equal(
132134
Error([
133135
DecodeError("Int", "String", ["b"]),
134-
DecodeError("Float", "Nil", ["q"]),
136+
DecodeError("Field", "Nothing", ["q"]),
135137
]),
136138
)
137139
}
@@ -144,25 +146,25 @@ pub fn opt_test() {
144146
clad.opt("foo", "f", zero.string, zero.success)
145147
|> clad.decode(["-f", "hello"], _)
146148
|> should.equal(Ok("hello"))
149+
// clad.opt("foo", "f", zero.string, zero.success)
150+
// |> clad.decode([], _)
151+
// |> should.equal(Error([DecodeError("String", "Nothing", ["f"])]))
147152

148-
clad.opt("foo", "f", zero.string, zero.success)
149-
|> clad.decode([], _)
150-
|> should.equal(Error([DecodeError("String", "Nil", ["f"])]))
151-
152-
clad.opt("foo", "f", zero.optional(zero.string), zero.success)
153-
|> clad.decode(["-f", "hello"], _)
154-
|> should.equal(Ok(Some("hello")))
153+
// clad.opt("foo", "f", zero.optional(zero.string), zero.success)
154+
// |> clad.decode(["-f", "hello"], _)
155+
// |> should.equal(Ok(Some("hello")))
155156

156-
clad.opt("foo", "f", zero.optional(zero.string), zero.success)
157-
|> clad.decode([], _)
158-
|> should.equal(Ok(None))
157+
// clad.opt("foo", "f", zero.optional(zero.string), zero.success)
158+
// |> clad.decode([], _)
159+
// |> should.equal(Ok(None))
159160
}
160161

161162
pub fn flag_test() {
162163
let decoder = {
163-
use verbose <- zero.field("v", clad.flag())
164+
use verbose <- clad.flag("verbose", "v")
164165
zero.success(verbose)
165166
}
167+
166168
clad.decode(["-v"], decoder)
167169
|> should.equal(Ok(True))
168170

@@ -181,7 +183,7 @@ pub fn flag_test() {
181183

182184
pub fn positional_arguments_test() {
183185
let decoder = {
184-
use a <- zero.field("a", clad.flag())
186+
use a <- zero.field("a", zero.bool)
185187
use b <- zero.field("b", zero.int)
186188
use c <- clad.positional_arguments()
187189
zero.success(#(a, b, c))

test/examples/student.gleam

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub fn main() {
1717
let decoder = {
1818
use name <- clad.opt("name", "n", zero.string)
1919
use age <- clad.opt("age", "a", zero.int)
20-
use enrolled <- clad.opt("enrolled", "e", clad.flag())
20+
use enrolled <- clad.flag("enrolled", "e")
2121
use classes <- clad.opt("class", "c", clad.list(zero.string))
2222
use notes <- clad.positional_arguments()
2323
let notes = string.join(notes, " ")

0 commit comments

Comments
 (0)