Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Args count and named args #25

Merged
merged 5 commits into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
strategy:
matrix:
erlang: ["25.3.2.3", "26.0.2"]
gleam: ["0.33.0"]
gleam: ["0.34.1"]
steps:
- uses: actions/checkout@v2
- uses: ./.github/actions/test
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- uses: actions/checkout@v2
- uses: ./.github/actions/test
with:
gleam-version: "0.33.0"
gleam-version: "0.34.1"
- name: publish to hex
env:
HEXPM_USER: ${{ secrets.HEXPM_USER }}
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

- refactor of help generation logic, no change to help text output
- the `glint/flag` module loses the `flags_help` and `flag_type_help` functions
- the `glint` module gains the ArgsCount type and the `count_args` function to support exact and minimum arguments count
- the `glint` module gains the `named_args` function to support named arguments
- the `glint.CommandInput` type gains the `.named_args` field to access named arguments
- help text has been updated to support named and counted arguments

## [0.14.0](https://github.com/TanklesXL/glint/compare/v0.13.0...v0.14.0)

Expand Down
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
8 changes: 4 additions & 4 deletions examples/hello/manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
packages = [
{ name = "argv", version = "1.0.1", build_tools = ["gleam"], requirements = [], otp_app = "argv", source = "hex", outer_checksum = "A6E9009E50BBE863EB37D963E4315398D41A3D87D0075480FC244125808F964A" },
{ name = "filepath", version = "0.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "534E8161A0DE192A9A105EFEC34369E9FD5834BB58ED449B5ACAEE8704358588" },
{ name = "gleam_community_ansi", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_community_colour"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "AB7C3CCC894653637E02DC455D5890C8CF3064E83E78CFE61145A4C458D02DE6" },
{ name = "gleam_community_ansi", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_community_colour"], 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_erlang", version = "0.23.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "C21CFB816C114784E669FFF4BBF433535EEA9960FA2F216209B8691E87156B96" },
{ name = "gleam_erlang", version = "0.24.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "26BDB52E61889F56A291CB34167315780EE4AA20961917314446542C90D1C1A0" },
{ name = "gleam_stdlib", version = "0.34.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "1FB8454D2991E9B4C0C804544D8A9AD0F6184725E20D63C3155F0AEB4230B016" },
{ name = "gleescript", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_erlang", "snag", "tom", "filepath", "simplifile"], otp_app = "gleescript", source = "hex", outer_checksum = "F7C152E206167000420F90983E4D4A076703292AAC4335A9248BA46D380841AC" },
{ name = "gleescript", version = "1.0.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib", "simplifile", "snag", "tom", "gleam_erlang"], otp_app = "gleescript", source = "hex", outer_checksum = "F7C152E206167000420F90983E4D4A076703292AAC4335A9248BA46D380841AC" },
{ name = "gleeunit", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D364C87AFEB26BDB4FB8A5ABDE67D635DC9FA52D6AB68416044C35B096C6882D" },
{ name = "glint", version = "0.14.0", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_community_colour", "gleam_stdlib", "snag"], source = "local", path = "../.." },
{ name = "simplifile", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "2B7070FB8617474A35651F6AA27046576615C14A4D97B62FA7C40C24C55A6C5C" },
{ name = "simplifile", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "359CD7006E2F69255025C858CCC6407C11A876EC179E6ED1E46809E8DC6B1AAD" },
{ name = "snag", version = "0.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "snag", source = "hex", outer_checksum = "54D32E16E33655346AA3E66CBA7E191DE0A8793D2C05284E3EFB90AD2CE92BCC" },
{ name = "tom", version = "0.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "tom", source = "hex", outer_checksum = "0831C73E45405A2153091226BF98FB485ED16376988602CC01A5FD086B82D577" },
]
Expand Down
36 changes: 24 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 All @@ -110,6 +120,8 @@ pub fn app() {
glint.new()
// with an app name of "hello", this is used when printing help text
|> glint.with_name("hello")
// show in usage text that the current app is run as a gleam module
|> glint.as_gleam_module
// with pretty help enabled, using the built-in colours
|> glint.with_pretty_help(glint.default_pretty_help())
// with a root command that executes the `hello` function
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
Loading