Skip to content

Commit 9e73124

Browse files
author
Ryan Miville
committed
clean up and readme
1 parent 093f5b3 commit 9e73124

File tree

4 files changed

+101
-69
lines changed

4 files changed

+101
-69
lines changed

README.md

+41-40
Original file line numberDiff line numberDiff line change
@@ -24,72 +24,73 @@ This program is in the [examples directory](https://github.com/ryanmiville/clad/
2424
```gleam
2525
import argv
2626
import clad
27+
import gleam/bool
2728
import gleam/dynamic
29+
import gleam/int
2830
import gleam/io
29-
import gleam/list
3031
import gleam/string
3132
32-
type Args {
33-
Args(name: String, count: Int, scream: Bool)
33+
type Order {
34+
Order(flavors: List(String), scoops: Int, cone: Bool)
3435
}
3536
36-
fn greet(args: Args) {
37-
let greeting = case args.scream {
38-
True -> "HEY " <> string.uppercase(args.name) <> "!"
39-
False -> "Hello, " <> args.name <> "."
40-
}
41-
list.repeat(greeting, args.count) |> list.each(io.println)
42-
}
43-
44-
fn args_decoder() {
45-
use name <- clad.arg(long_name: "name", short_name: "n", of: dynamic.string)
46-
use count <- clad.arg_with_default(
47-
long_name: "count",
48-
short_name: "c",
49-
of: dynamic.int,
50-
default: 1,
51-
)
52-
use scream <- clad.toggle(long_name: "scream", short_name: "s")
53-
clad.decoded(Args(name:, count:, scream:))
37+
fn order_decoder() {
38+
use flavors <- clad.arg("flavor", "f", dynamic.list(dynamic.string))
39+
use scoops <- clad.arg_with_default("scoops", "s", dynamic.int, default: 1)
40+
use cone <- clad.toggle("cone", "c")
41+
clad.decoded(Order(flavors:, scoops:, cone:))
5442
}
5543
5644
pub fn main() {
57-
let args =
58-
args_decoder()
45+
let order =
46+
order_decoder()
5947
|> clad.decode(argv.load().arguments)
6048
61-
case args {
62-
Ok(args) -> greet(args)
49+
case order {
50+
Ok(order) -> take_order(order)
6351
_ ->
6452
io.println(
6553
"
6654
Options:
67-
-n, --name <NAME> Name of the person to greet
68-
-c, --count <COUNT> Number of times to greet [default: 1]
69-
-s, --scream Whether or not to scream greeting
55+
-f, --flavor <FLAVOR> Flavors of ice cream
56+
-s, --scoops <SCOOPS> Number of scoops per flavor [default: 1]
57+
-c, --cone Put ice cream in a cone
7058
",
7159
)
7260
}
7361
}
62+
63+
fn take_order(order: Order) {
64+
let scoops = bool.guard(order.scoops == 1, " scoop", fn() { " scoops" })
65+
let container = bool.guard(order.cone, "cone", fn() { "cup" })
66+
let flavs = string.join(order.flavors, " and ")
67+
io.println(
68+
int.to_string(order.scoops)
69+
<> scoops
70+
<> " of "
71+
<> flavs
72+
<> " in a "
73+
<> container
74+
<> ", coming right up!",
75+
)
76+
}
7477
```
7578

7679
Run the program
7780

7881
```sh
79-
❯ gleam run -m examples/greet -- -n Joe
80-
Hello, Joe.
81-
❯ gleam run -m examples/greet -- --name=Joe
82-
Hello, Joe.
83-
❯ gleam run -m examples/greet -- --name Joe --count 3 --scream
84-
HEY JOE!
85-
HEY JOE!
86-
HEY JOE!
87-
❯ gleam run -m examples/greet
82+
❯ gleam run -m examples/ice_cream -- -f vanilla
83+
1 scoop of vanilla in a cup, coming right up!
84+
❯ gleam run -m examples/ice_cream -- --flavor vanilla --flavor chocolate
85+
1 scoop of vanilla and chocolate in a cup, coming right up!
86+
❯ gleam run -m examples/ice_cream -- --flavor vanilla --flavor chocolate --scoops 2 --cone
87+
2 scoops of vanilla and chocolate in a cone, coming right up!
88+
❯ gleam run -m examples/ice_cream --
8889

8990
Options:
90-
-n, --name <NAME> Name of the person to greet
91-
-c, --count <COUNT> Number of times to greet [default: 1]
92-
-s, --scream Whether or not to scream greeting
91+
-f, --flavor <FLAVOR> Flavors of ice cream
92+
-s, --scoops <SCOOPS> Number of scoops per flavor [default: 1]
93+
-c, --cone Put ice cream in a cone
9394
```
9495

9596
Further documentation can be found at <https://hexdocs.pm/clad>.

gleam.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name = "clad"
2-
version = "0.1.4"
2+
version = "0.1.5"
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

+7-28
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,7 @@ pub fn arg_with_default(
293293

294294
type Arg {
295295
Arg(long_name: String, short_name: String)
296-
LongName(String)
297-
ShortName(String)
296+
Name(String)
298297
}
299298

300299
type DecodeResult =
@@ -307,8 +306,7 @@ type ArgResults {
307306
long_result: DecodeResult,
308307
short_result: DecodeResult,
309308
)
310-
LongNameResults(long_name: String, long_result: DecodeResult)
311-
ShortNameResults(short_name: String, short_result: DecodeResult)
309+
NameResults(name: String, result: DecodeResult)
312310
}
313311

314312
/// Decode an argument by either its long name (`--name`) or short name (`-n`).
@@ -364,7 +362,7 @@ pub fn short_name(
364362
next: fn(a) -> Decoder(b),
365363
) {
366364
fn(data) {
367-
let first = do_arg(ShortName("-" <> short_name), decoder)
365+
let first = do_arg(Name("-" <> short_name), decoder)
368366
use a <- result.try(first(data))
369367
next(a)(data)
370368
}
@@ -384,7 +382,7 @@ pub fn long_name(
384382
next: fn(a) -> Decoder(b),
385383
) {
386384
fn(data) {
387-
let first = do_arg(LongName("--" <> long_name), decoder)
385+
let first = do_arg(Name("--" <> long_name), decoder)
388386
use a <- result.try(first(data))
389387
next(a)(data)
390388
}
@@ -401,14 +399,8 @@ fn do_arg(arg: Arg, using decoder: Decoder(t)) -> Decoder(t) {
401399
dynamic.optional_field(short_name, dynamic.shallow_list)(data),
402400
)
403401
}
404-
LongName(name) -> {
405-
LongNameResults(
406-
name,
407-
dynamic.optional_field(name, dynamic.shallow_list)(data),
408-
)
409-
}
410-
ShortName(name) -> {
411-
ShortNameResults(
402+
Name(name) -> {
403+
NameResults(
412404
name,
413405
dynamic.optional_field(name, dynamic.shallow_list)(data),
414406
)
@@ -417,8 +409,7 @@ fn do_arg(arg: Arg, using decoder: Decoder(t)) -> Decoder(t) {
417409

418410
case arg_res {
419411
ArgResults(l, s, lr, sr) -> do_arg_results(l, s, lr, sr, decoder)
420-
LongNameResults(n, r) -> do_single_name_results(n, r, decoder)
421-
ShortNameResults(n, r) -> do_single_name_results(n, r, decoder)
412+
NameResults(n, r) -> do_single_name_results(n, r, decoder)
422413
}
423414
}
424415
}
@@ -533,18 +524,6 @@ fn do_object_list(
533524
}
534525
}
535526

536-
// fn failure(
537-
// expected: String,
538-
// found: String,
539-
// path: List(String),
540-
// ) -> Result(t, DecodeErrors) {
541-
// Error([DecodeError(expected, found, path)])
542-
// }
543-
544-
// fn missing_field_error(long_name: String) {
545-
// failure("field", "nothing", ["--" <> long_name])
546-
// }
547-
548527
fn missing_field(name: String) {
549528
[DecodeError("field", "nothing", [name])]
550529
}

test/examples/ice_cream.gleam

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import argv
2+
import clad
3+
import gleam/bool
4+
import gleam/dynamic
5+
import gleam/int
6+
import gleam/io
7+
import gleam/string
8+
9+
type Order {
10+
Order(flavors: List(String), scoops: Int, cone: Bool)
11+
}
12+
13+
fn order_decoder() {
14+
use flavors <- clad.arg("flavor", "f", dynamic.list(dynamic.string))
15+
use scoops <- clad.arg_with_default("scoops", "s", dynamic.int, default: 1)
16+
use cone <- clad.toggle("cone", "c")
17+
clad.decoded(Order(flavors:, scoops:, cone:))
18+
}
19+
20+
pub fn main() {
21+
let order =
22+
order_decoder()
23+
|> clad.decode(argv.load().arguments)
24+
25+
case order {
26+
Ok(order) -> take_order(order)
27+
_ ->
28+
io.println(
29+
"
30+
Options:
31+
-f, --flavor <FLAVOR> Flavors of ice cream
32+
-s, --scoops <SCOOPS> Number of scoops per flavor [default: 1]
33+
-c, --cone Put ice cream in a cone
34+
",
35+
)
36+
}
37+
}
38+
39+
fn take_order(order: Order) {
40+
let scoops = bool.guard(order.scoops == 1, " scoop", fn() { " scoops" })
41+
let container = bool.guard(order.cone, "cone", fn() { "cup" })
42+
let flavs = string.join(order.flavors, " and ")
43+
io.println(
44+
int.to_string(order.scoops)
45+
<> scoops
46+
<> " of "
47+
<> flavs
48+
<> " in a "
49+
<> container
50+
<> ", coming right up!",
51+
)
52+
}

0 commit comments

Comments
 (0)