Skip to content

Commit

Permalink
labels for functions with name params
Browse files Browse the repository at this point in the history
  • Loading branch information
TanklesXL committed Apr 21, 2024
1 parent bc7c833 commit 76a3e45
Show file tree
Hide file tree
Showing 11 changed files with 232 additions and 235 deletions.
1 change: 1 addition & 0 deletions .github/actions/test/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ runs:
with:
otp-version: ${{ inputs.erlang-version }}
gleam-version: ${{ inputs.gleam-version }}
rebar3-versin: 3
- uses: actions/setup-node@v2
with:
node-version: ${{ inputs.node-version }}
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
*.ez
build
erl_crash.dump
.ignore_me.*
2 changes: 1 addition & 1 deletion examples/hello/gleam.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "hello"
version = "0.1.0"
description = "A Gleam project"
gleam = "~> 0.34 or ~> 1.0"
gleam = ">= 1.1.0 and < 2.0.0"

# Fill out these fields if you intend to generate HTML documentation or publish
# your project to the Hex package manager.
Expand Down
14 changes: 8 additions & 6 deletions examples/hello/src/hello.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
import gleam/io
import gleam/list
import gleam/string.{uppercase}

// external dep imports
import snag

// glint imports
import argv
import glint
Expand Down Expand Up @@ -54,8 +56,8 @@ pub const caps = "caps"

/// a boolean flag with default False to control message capitalization.
///
pub fn caps_flag() -> glint.FlagBuilder(Bool) {
glint.bool()
pub fn caps_flag() -> glint.Flag(Bool) {
glint.bool(caps)
|> glint.default(False)
|> glint.flag_help("Capitalize the hello message")
}
Expand All @@ -66,9 +68,9 @@ pub const repeat = "repeat"
/// an int flag with default 1 to control how many times to repeat the message.
/// this flag is constrained to values greater than 0.
///
pub fn repeat_flag() -> glint.FlagBuilder(Int) {
pub fn repeat_flag() -> glint.Flag(Int) {
use n <- glint.constraint(
glint.int()
glint.int(repeat)
|> glint.default(1)
|> glint.flag_help("Repeat the message n-times"),
)
Expand Down Expand Up @@ -116,9 +118,9 @@ pub fn app() {
|> glint.pretty_help(glint.default_pretty_help())
// with group level flags
// with flag `caps` for all commands (equivalent of using glint.global_flag)
|> glint.group_flag([], caps, caps_flag())
|> glint.group_flag([], caps_flag())
// // with flag `repeat` for all commands (equivalent of using glint.global_flag)
|> glint.group_flag([], repeat, repeat_flag())
|> glint.group_flag([], repeat_flag())
// with a root command that executes the `hello` function
|> glint.add(
// add the hello command to the root
Expand Down
7 changes: 7 additions & 0 deletions examples/hello/test/hello_test.gleam
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import gleam/list
import gleeunit
import gleeunit/should
import glint
import hello

pub fn main() {
Expand Down Expand Up @@ -35,3 +36,9 @@ pub fn hello_test() {
hello.hello(head, rest, tc.caps, tc.repeat)
|> should.equal(tc.expected)
}

pub fn app_test() {
hello.app()
|> glint.execute(["Joe", "Gleamlins"])
|> should.equal(Ok(glint.Out("Hello, Joe and Gleamlins!")))
}
2 changes: 1 addition & 1 deletion gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ links = [
gleam = "~> 1.0"

[dependencies]
gleam_stdlib = "~> 0.36.0 or ~> 1.0"
gleam_stdlib = "~> 0.36 or ~> 1.0"
snag = "~> 0.3"
gleam_community_ansi = "~> 1.0"
gleam_community_colour = "~> 1.0"
Expand Down
10 changes: 6 additions & 4 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@

packages = [
{ 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.36.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "C0D14D807FEC6F8A08A7C9EF8DFDE6AE5C10E40E21325B2B29365965D82EB3D4" },
{ name = "gleeunit", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D364C87AFEB26BDB4FB8A5ABDE67D635DC9FA52D6AB68416044C35B096C6882D" },
{ name = "gleam_community_colour", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "795964217EBEDB3DA656F5EB8F67D7AD22872EB95182042D3E7AFEF32D3FD2FE" },
{ name = "gleam_json", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "thoas"], otp_app = "gleam_json", source = "hex", outer_checksum = "8B197DD5D578EA6AC2C0D4BDC634C71A5BCA8E7DB5F47091C263ECB411A60DF3" },
{ name = "gleam_stdlib", version = "0.37.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "5398BD6C2ABA17338F676F42F404B9B7BABE1C8DC7380031ACB05BBE1BCF3742" },
{ name = "gleeunit", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "72CDC3D3F719478F26C4E2C5FED3E657AC81EC14A47D2D2DEBB8693CA3220C3B" },
{ name = "snag", version = "0.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "snag", source = "hex", outer_checksum = "54D32E16E33655346AA3E66CBA7E191DE0A8793D2C05284E3EFB90AD2CE92BCC" },
{ name = "thoas", version = "0.4.1", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "4918D50026C073C4AB1388437132C77A6F6F7C8AC43C60C13758CC0ADCE2134E" },
]

[requirements]
gleam_community_ansi = { version = "~> 1.0" }
gleam_community_colour = { version = "~> 1.0" }
gleam_stdlib = { version = "~> 0.36.0 or ~> 1.0" }
gleam_stdlib = { version = "~> 0.36 or ~> 1.0" }
gleeunit = { version = "~> 1.0" }
snag = { version = "~> 0.3" }
75 changes: 45 additions & 30 deletions src/glint.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ pub fn add(
Glint(
..glint,
cmd: path
|> sanitize_path
|> do_add(to: glint.cmd, put: command),
|> sanitize_path
|> do_add(to: glint.cmd, put: command),
)
}

Expand Down Expand Up @@ -216,13 +216,16 @@ pub fn command(do runner: Runner(a)) -> Command(a) {

/// Attach a helptext description to a Command(a)
///
pub fn command_help(desc: String, f: fn() -> Command(a)) -> Command(a) {
pub fn command_help(of desc: String, with f: fn() -> Command(a)) -> Command(a) {
Command(..f(), description: desc)
}

/// Specify a specific number of unnamed args that a given command expects
///
pub fn unnamed_args(args: ArgsCount, f: fn() -> Command(b)) -> Command(b) {
pub fn unnamed_args(
of args: ArgsCount,
with f: fn() -> Command(b),
) -> Command(b) {
Command(..f(), unnamed_args: Some(args))
}

Expand All @@ -234,8 +237,8 @@ pub fn unnamed_args(args: ArgsCount, f: fn() -> Command(b)) -> Command(b) {
/// **IMPORTANT**: Matched named arguments will not be present in the commmand's unnamed args list
///
pub fn named_arg(
name: String,
f: fn(fn(NamedArgs) -> String) -> Command(a),
named name: String,
with f: fn(fn(NamedArgs) -> String) -> Command(a),
) -> Command(a) {
let cmd = {
use named_args <- f()
Expand All @@ -250,12 +253,11 @@ pub fn named_arg(
/// Add a `Flag` to a `Command`
///
pub fn flag(
called name: String,
with builder: Flag(a),
providing f: fn(fn(Flags) -> snag.Result(a)) -> Command(b),
of builder: Flag(a),
with f: fn(fn(Flags) -> snag.Result(a)) -> Command(b),
) -> Command(b) {
let cmd = f(builder.getter(_, name))
Command(..cmd, flags: insert(cmd.flags, name, build_flag(builder)))
let cmd = f(builder.getter(_, builder.name))
Command(..cmd, flags: insert(cmd.flags, builder.name, build_flag(builder)))
}

/// Add a flag for a group of commands.
Expand All @@ -264,12 +266,16 @@ pub fn flag(
pub fn group_flag(
in glint: Glint(a),
at path: List(String),
for name: String,
of flag: Flag(_),
) -> Glint(a) {
Glint(
..glint,
cmd: do_group_flag(in: glint.cmd, at: path, for: name, of: build_flag(flag)),
cmd: do_group_flag(
in: glint.cmd,
at: path,
named: flag.name,
of: build_flag(flag),
),
)
}

Expand All @@ -279,7 +285,7 @@ pub fn group_flag(
fn do_group_flag(
in node: CommandNode(a),
at path: List(String),
for name: String,
named name: String,
of flag: FlagEntry,
) -> CommandNode(a) {
case path {
Expand All @@ -293,7 +299,7 @@ fn do_group_flag(

node
|> option.unwrap(empty_command())
|> do_group_flag(at: tail, for: name, of: flag)
|> do_group_flag(at: tail, named: name, of: flag)
},
)
}
Expand Down Expand Up @@ -927,6 +933,7 @@ type Value {
///
pub opaque type Flag(a) {
Flag(
name: String,
desc: String,
parser: Parser(a, Snag),
value: fn(FlagInternals(a)) -> Value,
Expand All @@ -948,17 +955,17 @@ type Parser(a, b) =

/// initialise an int flag builder
///
pub fn int() -> Flag(Int) {
use input <- new_builder(I, get_int)
pub fn int(named name: String) -> Flag(Int) {
use input <- new_builder(name, I, get_int)
input
|> int.parse
|> result.replace_error(cannot_parse(input, "int"))
}

/// initialise an int list flag builder
///
pub fn ints() -> Flag(List(Int)) {
use input <- new_builder(LI, get_ints)
pub fn ints(named name: String) -> Flag(List(Int)) {
use input <- new_builder(name, LI, get_ints)
input
|> string.split(",")
|> list.try_map(int.parse)
Expand All @@ -967,17 +974,17 @@ pub fn ints() -> Flag(List(Int)) {

/// initialise a float flag builder
///
pub fn float() -> Flag(Float) {
use input <- new_builder(F, get_float)
pub fn float(named name: String) -> Flag(Float) {
use input <- new_builder(name, F, get_float)
input
|> float.parse
|> result.replace_error(cannot_parse(input, "float"))
}

/// initialise a float list flag builder
///
pub fn floats() -> Flag(List(Float)) {
use input <- new_builder(LF, get_floats)
pub fn floats(named name: String) -> Flag(List(Float)) {
use input <- new_builder(name, LF, get_floats)
input
|> string.split(",")
|> list.try_map(float.parse)
Expand All @@ -986,23 +993,23 @@ pub fn floats() -> Flag(List(Float)) {

/// initialise a string flag builder
///
pub fn string() -> Flag(String) {
new_builder(S, get_string, fn(s) { Ok(s) })
pub fn string(named name: String) -> Flag(String) {
new_builder(name, S, get_string, fn(s) { Ok(s) })
}

/// intitialise a string list flag builder
///
pub fn strings() -> Flag(List(String)) {
use input <- new_builder(LS, get_strings)
pub fn strings(named name: String) -> Flag(List(String)) {
use input <- new_builder(name, LS, get_strings)
input
|> string.split(",")
|> Ok
}

/// initialise a bool flag builder
///
pub fn bool() -> Flag(Bool) {
use input <- new_builder(B, get_bool)
pub fn bool(named name: String) -> Flag(Bool) {
use input <- new_builder(name, B, get_bool)
case string.lowercase(input) {
"true" | "t" -> Ok(True)
"false" | "f" -> Ok(False)
Expand All @@ -1013,11 +1020,19 @@ pub fn bool() -> Flag(Bool) {
/// initialize custom builders using a Value constructor and a parsing function
///
fn new_builder(
name: String,
valuer: fn(FlagInternals(a)) -> Value,
getter: fn(Flags, String) -> snag.Result(a),
p: Parser(a, Snag),
) -> Flag(a) {
Flag(desc: "", parser: p, value: valuer, default: None, getter: getter)
Flag(
name: name,
desc: "",
parser: p,
value: valuer,
default: None,
getter: getter,
)
}

/// convert a Flag(a) into its corresponding FlagEntry representation
Expand Down
Loading

0 comments on commit 76a3e45

Please sign in to comment.