Skip to content

Commit

Permalink
basic implementation of named args
Browse files Browse the repository at this point in the history
  • Loading branch information
TanklesXL committed Jan 28, 2024
1 parent 63bf66a commit a04428e
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 142 deletions.
4 changes: 2 additions & 2 deletions examples/hello/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ Feel free to browse `src/hello.gleam` to get a sense of how a small cli applicat

You can run this example from the `examples/hello` directory by calling `gleam run` which prints `Hello, <NAMES>!`

The `hello` application accepts any number of arguments, being the names of people to say hello to.
The `hello` application accepts at least one argument, being the names of people to say hello to.

- No input: `gleam run` -> prints "Hello, Joe!"
- One input: `gleam run Rob` -> prints "Hello, Rob!"
- One input: `gleam run Joe` -> prints "Hello, Joe!"
- Two inputs: `gleam run Rob Louis` -> prints "Hello, Rob and Louis!"
- \>2 inputs: `gleam run Rob Louis Hayleigh` -> prints "Hello, Rob, Louis and Hayleigh!"

Expand Down
34 changes: 22 additions & 12 deletions examples/hello/src/hello.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import gleam/io
import gleam/list
import gleam/string.{uppercase}
import gleam/dict
// external dep imports
import snag
// glint imports
Expand All @@ -14,9 +15,8 @@ import argv
/// a helper function to join a list of names
fn join_names(names: List(String)) -> String {
case names {
[] -> "Joe"
[name] -> name
[name, ..rest] -> do_join_names(rest, name)
[] -> ""
_ -> do_join_names(names, "")
}
}

Expand All @@ -29,10 +29,6 @@ fn do_join_names(names: List(String), acc: String) {
}
}

pub fn message(names: List(String)) {
"Hello, " <> join_names(names) <> "!"
}

pub fn capitalize(msg, caps) -> String {
case caps {
True -> uppercase(msg)
Expand All @@ -41,9 +37,13 @@ pub fn capitalize(msg, caps) -> String {
}

/// hello is a function that
pub fn hello(names: List(String), caps: Bool, repeat: Int) -> String {
names
|> message
pub fn hello(
primary: String,
rest: List(String),
caps: Bool,
repeat: Int,
) -> String {
{ "Hello, " <> primary <> join_names(rest) <> "!" }
|> capitalize(caps)
|> list.repeat(repeat)
|> string.join("\n")
Expand Down Expand Up @@ -89,19 +89,29 @@ fn gtz(n: Int) -> snag.Result(Nil) {
pub fn hello_cmd() -> glint.Command(String) {
{
use input <- glint.command()

// the caps flag has a default value, so we can be sure it will always be present
let assert Ok(caps) = flag.get_bool(from: input.flags, for: caps)

// the repeat flag has a default value, so we can be sure it will always be present
let assert Ok(repeat) = flag.get_int(from: input.flags, for: repeat)

// access named args directly
let assert Ok(name) = dict.get(input.named_args, "name")

// call the hello function with all necessary inputs
hello(input.args, caps, repeat)
hello(name, input.args, caps, repeat)
}
// with flag `caps`
|> glint.flag(caps, caps_flag())
// with flag `repeat`
|> glint.flag(repeat, repeat_flag())
// with flag `repeat`
|> glint.description("Prints Hello, <NAMES>!")
|> glint.description("Prints Hello, <names>!")
// with a first arg called name
|> glint.named_args(["name"])
// requiring at least 1 argument
|> glint.count_args(glint.MinArgs(1))
}

// the function that describes our cli structure
Expand Down
7 changes: 3 additions & 4 deletions examples/hello/test/hello_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ type TestCase {

pub fn hello_test() {
use tc <- list.each([
TestCase([], False, 1, "Hello, Joe!"),
TestCase(["Rob"], False, 1, "Hello, Rob!"),
TestCase([], True, 1, "HELLO, JOE!"),
TestCase([], True, 2, "HELLO, JOE!\nHELLO, JOE!"),
TestCase(["Rob"], True, 1, "HELLO, ROB!"),
TestCase(["Tony", "Maria"], True, 1, "HELLO, TONY AND MARIA!"),
TestCase(
Expand All @@ -33,6 +30,8 @@ pub fn hello_test() {
"Hello, Tony, Maria and Nadia!",
),
])
hello.hello(tc.input, tc.caps, tc.repeat)

let assert [head, ..rest] = tc.input
hello.hello(head, rest, tc.caps, tc.repeat)
|> should.equal(tc.expected)
}
2 changes: 1 addition & 1 deletion manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# You typically do not need to edit this file

packages = [
{ name = "gleam_community_ansi", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_stdlib"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "AB7C3CCC894653637E02DC455D5890C8CF3064E83E78CFE61145A4C458D02DE6" },
{ name = "gleam_community_ansi", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_stdlib"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "FE79E08BF97009729259B6357EC058315B6FBB916FAD1C2FF9355115FEB0D3A4" },
{ name = "gleam_community_colour", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "A49A5E3AE8B637A5ACBA80ECB9B1AFE89FD3D5351FF6410A42B84F666D40D7D5" },
{ name = "gleam_stdlib", version = "0.34.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "1FB8454D2991E9B4C0C804544D8A9AD0F6184725E20D63C3155F0AEB4230B016" },
{ name = "gleeunit", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D364C87AFEB26BDB4FB8A5ABDE67D635DC9FA52D6AB68416044C35B096C6882D" },
Expand Down
Loading

0 comments on commit a04428e

Please sign in to comment.