diff --git a/.github/actions/test/action.yml b/.github/actions/test/action.yml index e822492..b761d6b 100644 --- a/.github/actions/test/action.yml +++ b/.github/actions/test/action.yml @@ -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 }} diff --git a/.gitignore b/.gitignore index 170cca9..8c9a6a0 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ *.ez build erl_crash.dump +.ignore_me.* diff --git a/examples/hello/gleam.toml b/examples/hello/gleam.toml index de6b9b9..8752ec6 100644 --- a/examples/hello/gleam.toml +++ b/examples/hello/gleam.toml @@ -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. diff --git a/examples/hello/src/hello.gleam b/examples/hello/src/hello.gleam index 352bed0..ef0463f 100644 --- a/examples/hello/src/hello.gleam +++ b/examples/hello/src/hello.gleam @@ -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 @@ -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") } @@ -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"), ) @@ -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 diff --git a/examples/hello/test/hello_test.gleam b/examples/hello/test/hello_test.gleam index b2c2f2a..bb15fb7 100644 --- a/examples/hello/test/hello_test.gleam +++ b/examples/hello/test/hello_test.gleam @@ -1,6 +1,7 @@ import gleam/list import gleeunit import gleeunit/should +import glint import hello pub fn main() { @@ -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!"))) +} diff --git a/gleam.toml b/gleam.toml index 4952583..020bcaf 100644 --- a/gleam.toml +++ b/gleam.toml @@ -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" diff --git a/manifest.toml b/manifest.toml index 92b6c32..130b87e 100644 --- a/manifest.toml +++ b/manifest.toml @@ -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" } diff --git a/src/glint.gleam b/src/glint.gleam index b5e8f5c..5d1c43a 100644 --- a/src/glint.gleam +++ b/src/glint.gleam @@ -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), ) } @@ -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)) } @@ -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() @@ -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. @@ -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), + ), ) } @@ -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 { @@ -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) }, ) } @@ -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, @@ -948,8 +955,8 @@ 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")) @@ -957,8 +964,8 @@ pub fn int() -> Flag(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) @@ -967,8 +974,8 @@ 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")) @@ -976,8 +983,8 @@ pub fn float() -> Flag(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) @@ -986,14 +993,14 @@ 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 @@ -1001,8 +1008,8 @@ pub fn strings() -> Flag(List(String)) { /// 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) @@ -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 diff --git a/test/contraint_test.gleam b/test/contraint_test.gleam index d0fef7b..24551bc 100644 --- a/test/contraint_test.gleam +++ b/test/contraint_test.gleam @@ -54,9 +54,8 @@ pub fn none_of_test() { } pub fn flag_one_of_none_of_test() { - let #(test_flag_name, test_flag, success, failure) = #( - "i", - glint.int() + let #(test_flag, success, failure) = #( + glint.int("i") |> glint.constraint(one_of([1, 2, 3])) |> glint.constraint(none_of([4, 5, 6])), "1", @@ -65,64 +64,62 @@ pub fn flag_one_of_none_of_test() { glint.new() |> glint.add([], { - use access <- glint.flag(test_flag_name, test_flag) + use access <- glint.flag(test_flag) use _, _, flags <- glint.command() flags |> access |> should.be_ok }) - |> glint.execute(["--" <> test_flag_name <> "=" <> success]) + |> glint.execute(["--i=" <> success]) |> should.be_ok glint.new() |> glint.add([], { - use _access <- glint.flag(test_flag_name, test_flag) + use _access <- glint.flag(test_flag) use _, _, _flags <- glint.command() panic }) - |> glint.execute(["--" <> test_flag_name <> "=" <> failure]) + |> glint.execute(["--i=" <> failure]) |> should.be_error - let #(test_flag_name, test_flag, success, failure) = #( - "li", - glint.ints() + let #(test_flag, success, failure) = #( + glint.ints("li") |> glint.constraint( - [1, 2, 3] - |> one_of - |> each, - ) + [1, 2, 3] + |> one_of + |> each, + ) |> glint.constraint( - [4, 5, 6] - |> none_of - |> each, - ), + [4, 5, 6] + |> none_of + |> each, + ), "1,1,1", "2,2,6", ) glint.new() |> glint.add([], { - use access <- glint.flag(test_flag_name, test_flag) + use access <- glint.flag(test_flag) use _, _, flags <- glint.command() flags |> access |> should.be_ok }) - |> glint.execute(["--" <> test_flag_name <> "=" <> success]) + |> glint.execute(["--li=" <> success]) |> should.be_ok glint.new() |> glint.add([], { - use _access <- glint.flag(test_flag_name, test_flag) + use _access <- glint.flag(test_flag) use _, _, _flags <- glint.command() panic }) - |> glint.execute(["--" <> test_flag_name <> "=" <> failure]) + |> glint.execute(["--li=" <> failure]) |> should.be_error - let #(test_flag_name, test_flag, success, failure) = #( - "f", - glint.float() + let #(test_flag, success, failure) = #( + glint.float("f") |> glint.constraint(one_of([1.0, 2.0, 3.0])) |> glint.constraint(none_of([4.0, 5.0, 6.0])), "1.0", @@ -130,63 +127,61 @@ pub fn flag_one_of_none_of_test() { ) glint.new() |> glint.add([], { - use access <- glint.flag(test_flag_name, test_flag) + use access <- glint.flag(test_flag) use _, _, flags <- glint.command() flags |> access |> should.be_ok }) - |> glint.execute(["--" <> test_flag_name <> "=" <> success]) + |> glint.execute(["--f=" <> success]) |> should.be_ok glint.new() |> glint.add([], { - use _access <- glint.flag(test_flag_name, test_flag) + use _access <- glint.flag(test_flag) use _, _, _flags <- glint.command() panic }) - |> glint.execute(["--" <> test_flag_name <> "=" <> failure]) + |> glint.execute(["--f=" <> failure]) |> should.be_error - let #(test_flag_name, test_flag, success, failure) = #( - "lf", - glint.floats() + let #(test_flag, success, failure) = #( + glint.floats("lf") |> glint.constraint( - [1.0, 2.0, 3.0] - |> one_of() - |> each, - ) + [1.0, 2.0, 3.0] + |> one_of() + |> each, + ) |> glint.constraint( - [4.0, 5.0, 6.0] - |> none_of() - |> each, - ), + [4.0, 5.0, 6.0] + |> none_of() + |> each, + ), "3.0,2.0,1.0", "2.0,3.0,6.0", ) glint.new() |> glint.add([], { - use access <- glint.flag(test_flag_name, test_flag) + use access <- glint.flag(test_flag) use _, _, flags <- glint.command() flags |> access |> should.be_ok }) - |> glint.execute(["--" <> test_flag_name <> "=" <> success]) + |> glint.execute(["--lf=" <> success]) |> should.be_ok glint.new() |> glint.add([], { - use _access <- glint.flag(test_flag_name, test_flag) + use _access <- glint.flag(test_flag) use _, _, _flags <- glint.command() panic }) - |> glint.execute(["--" <> test_flag_name <> "=" <> failure]) + |> glint.execute(["--lf=" <> failure]) |> should.be_error - let #(test_flag_name, test_flag, success, failure) = #( - "s", - glint.string() + let #(test_flag, success, failure) = #( + glint.string("s") |> glint.constraint(one_of(["t1", "t2", "t3"])) |> glint.constraint(none_of(["t4", "t5", "t6"])), "t3", @@ -195,58 +190,57 @@ pub fn flag_one_of_none_of_test() { glint.new() |> glint.add([], { - use access <- glint.flag(test_flag_name, test_flag) + use access <- glint.flag(test_flag) use _, _, flags <- glint.command() flags |> access |> should.be_ok }) - |> glint.execute(["--" <> test_flag_name <> "=" <> success]) + |> glint.execute(["--s=" <> success]) |> should.be_ok glint.new() |> glint.add([], { - use _access <- glint.flag(test_flag_name, test_flag) + use _access <- glint.flag(test_flag) use _, _, _flags <- glint.command() panic }) - |> glint.execute(["--" <> test_flag_name <> "=" <> failure]) + |> glint.execute(["--s=" <> failure]) |> should.be_error - let #(test_flag_name, test_flag, success, failure) = #( - "ls", - glint.strings() + let #(test_flag, success, failure) = #( + glint.strings("ls") |> glint.constraint( - ["t1", "t2", "t3"] - |> one_of - |> each, - ) + ["t1", "t2", "t3"] + |> one_of + |> each, + ) |> glint.constraint( - ["t4", "t5", "t6"] - |> none_of - |> each, - ), + ["t4", "t5", "t6"] + |> none_of + |> each, + ), "t3,t2,t1", "t2,t4,t1", ) glint.new() |> glint.add([], { - use access <- glint.flag(test_flag_name, test_flag) + use access <- glint.flag(test_flag) use _, _, flags <- glint.command() flags |> access |> should.be_ok }) - |> glint.execute(["--" <> test_flag_name <> "=" <> success]) + |> glint.execute(["--ls=" <> success]) |> should.be_ok glint.new() |> glint.add([], { - use _access <- glint.flag(test_flag_name, test_flag) + use _access <- glint.flag(test_flag) use _, _, _flags <- glint.command() panic }) - |> glint.execute(["--" <> test_flag_name <> "=" <> failure]) + |> glint.execute(["--ls=" <> failure]) |> should.be_error } diff --git a/test/flag_test.gleam b/test/flag_test.gleam index 339bae7..8ce83b1 100644 --- a/test/flag_test.gleam +++ b/test/flag_test.gleam @@ -5,13 +5,13 @@ pub fn update_flag_test() { let app = glint.new() |> glint.add([], { - use _bflag <- glint.flag("bflag", glint.bool()) - use _sflag <- glint.flag("sflag", glint.string()) - use _lsflag <- glint.flag("lsflag", glint.strings()) - use _iflag <- glint.flag("iflag", glint.int()) - use _liflag <- glint.flag("liflag", glint.ints()) - use _fflag <- glint.flag("fflag", glint.float()) - use _lfflag <- glint.flag("lfflag", glint.floats()) + use _bflag <- glint.flag(glint.bool("bflag")) + use _sflag <- glint.flag(glint.string("sflag")) + use _lsflag <- glint.flag(glint.strings("lsflag")) + use _iflag <- glint.flag(glint.int("iflag")) + use _liflag <- glint.flag(glint.ints("liflag")) + use _fflag <- glint.flag(glint.float("fflag")) + use _lfflag <- glint.flag(glint.floats("lfflag")) glint.command(fn(_, _, _) { Nil }) }) @@ -95,15 +95,13 @@ pub fn unsupported_flag_test() { pub fn flag_default_test() { let args = ["arg1", "arg2"] - let flag = #( - "flag", - glint.string() - |> glint.default("default"), - ) + let flag = + glint.string("flag") + |> glint.default("default") glint.new() |> glint.add(["cmd"], { - use flag_ <- glint.flag(flag.0, flag.1) + use flag_ <- glint.flag(flag) use _, unnamed, flags <- glint.command() should.equal(args, unnamed) @@ -116,10 +114,10 @@ pub fn flag_default_test() { pub fn flag_value_test() { let args = ["arg1", "arg2"] - let flag = #("flag", glint.string()) + let flag = glint.string("flag") let flag_input = "--flag=flag_value" let flag_value_should_be_set = { - use flag_ <- glint.flag(flag.0, flag.1) + use flag_ <- glint.flag(flag) use _, in_args, flags <- glint.command() should.equal(in_args, args) @@ -134,16 +132,13 @@ pub fn flag_value_test() { } pub fn int_flag_test() { - let flags = #("flag", glint.int()) - + let flags = glint.int("flag") // fails to parse input for flag as int, returns error let flag_input = "--flag=X" glint.new() |> glint.add( [], - glint.flag(flags.0, flags.1, fn(_flag) { - glint.command(fn(_, _, _) { Nil }) - }), + glint.flag(flags, fn(_flag) { glint.command(fn(_, _, _) { Nil }) }), ) |> glint.execute([flag_input]) |> should.be_error() @@ -151,7 +146,7 @@ pub fn int_flag_test() { // parses flag input as int, sets value let flag_input = "--flag=10" let expect_flag_value_of_10 = { - use flag_ <- glint.flag(flags.0, flags.1) + use flag_ <- glint.flag(flags) use _, _, flags <- glint.command() flag_(flags) |> should.equal(Ok(10)) @@ -164,14 +159,14 @@ pub fn int_flag_test() { } pub fn bool_flag_test() { - let flag = #("flag", glint.bool()) + let flag = glint.bool("flag") // fails to parse input for flag as bool, returns error let flag_input = "--flag=X" glint.new() |> glint.add( [], - glint.flag(flag.0, flag.1, fn(_flag) { glint.command(fn(_, _, _) { Nil }) }), + glint.flag(flag, fn(_flag) { glint.command(fn(_, _, _) { Nil }) }), ) |> glint.execute([flag_input]) |> should.be_error() @@ -186,13 +181,13 @@ pub fn bool_flag_test() { } glint.new() - |> glint.add([], glint.flag(flag.0, flag.1, expect_flag_value_of_false)) + |> glint.add([], glint.flag(flag, expect_flag_value_of_false)) |> glint.execute([flag_input]) |> should.be_ok() } pub fn strings_flag_test() { - let flags = #("flag", glint.strings()) + let flags = glint.strings("flag") let flag_input = "--flag=val3,val4" let expect_flag_value_list = fn(flag) { glint.command(fn(_, _, flags) { @@ -200,21 +195,22 @@ pub fn strings_flag_test() { |> should.equal(Ok(["val3", "val4"])) }) } + glint.new() - |> glint.add([], glint.flag(flags.0, flags.1, expect_flag_value_list)) + |> glint.add([], glint.flag(flags, expect_flag_value_list)) |> glint.execute([flag_input]) |> should.be_ok() } pub fn ints_flag_test() { - let flag = #("flag", glint.ints()) + let flag = glint.ints("flag") // fails to parse input for flag as int list, returns error let flag_input = "--flag=val3,val4" glint.new() |> glint.add( [], - glint.flag(flag.0, flag.1, fn(_) { glint.command(fn(_, _, _) { Nil }) }), + glint.flag(flag, fn(_) { glint.command(fn(_, _, _) { Nil }) }), ) |> glint.execute([flag_input]) |> should.be_error() @@ -229,19 +225,19 @@ pub fn ints_flag_test() { } glint.new() - |> glint.add([], glint.flag(flag.0, flag.1, expect_flag_value_list)) + |> glint.add([], glint.flag(flag, expect_flag_value_list)) |> glint.execute([flag_input]) |> should.be_ok() } pub fn float_flag_test() { - let flag = #("flag", glint.float()) + let flag = glint.float("flag") // fails to parse input for flag as float, returns error let flag_input = "--flag=X" glint.new() |> glint.add([], { - use _flag <- glint.flag(flag.0, flag.1) + use _flag <- glint.flag(flag) use _, _, _ <- glint.command() Nil }) @@ -251,7 +247,7 @@ pub fn float_flag_test() { // parses flag input as float, sets value let flag_input = "--flag=10.0" let expect_flag_value_of_10 = { - use flag <- glint.flag(flag.0, flag.1) + use flag <- glint.flag(flag) use _, _, flags <- glint.command() flag(flags) |> should.equal(Ok(10.0)) @@ -264,13 +260,13 @@ pub fn float_flag_test() { } pub fn floats_flag_test() { - let flag = #("flag", glint.floats()) + let flag = glint.floats("flag") // fails to parse input for flag as float list, returns error let flag_input = "--flag=val3,val4" glint.new() |> glint.add([], { - use _flag <- glint.flag(flag.0, flag.1) + use _flag <- glint.flag(flag) use _, _, _ <- glint.command() Nil }) @@ -280,7 +276,7 @@ pub fn floats_flag_test() { // parses flag input as float list, sets value let flag_input = "--flag=3.0,4.0" let expect_flag_value_list = { - use flag <- glint.flag(flag.0, flag.1) + use flag <- glint.flag(flag) use _, _, flags <- glint.command flag(flags) |> should.equal(Ok([3.0, 4.0])) @@ -301,19 +297,18 @@ pub fn global_flag_test() { // set global flag, pass in new value for flag glint.new() - |> glint.group_flag([], "flag", glint.floats()) + |> glint.group_flag([], glint.floats("flag")) |> glint.add(at: [], do: testcase([3.0, 4.0])) |> glint.execute(["--flag=3.0,4.0"]) |> should.be_ok() // set global flag and local flag, local flag should take priority glint.new() - |> glint.group_flag([], "flag", glint.floats()) + |> glint.group_flag([], glint.floats("flag")) |> glint.add( at: [], do: glint.flag( - "flag", - glint.floats() + glint.floats("flag") |> glint.default([1.0, 2.0]), fn(_) { testcase([1.0, 2.0]) }, ), @@ -325,15 +320,13 @@ pub fn global_flag_test() { glint.new() |> glint.group_flag( [], - "flag", - glint.floats() + glint.floats("flag") |> glint.default([3.0, 4.0]), ) |> glint.add(at: [], do: { use _flag <- glint.flag( - "flag", - glint.floats() - |> glint.default([1.0, 2.0]), + glint.floats("flag") + |> glint.default([1.0, 2.0]), ) testcase([5.0, 6.0]) @@ -348,9 +341,7 @@ pub fn toggle_test() { glint.new() |> glint.add( [], - glint.flag("flag", glint.bool(), fn(_) { - glint.command(fn(_, _, _) { Nil }) - }), + glint.flag(glint.bool("flag"), fn(_) { glint.command(fn(_, _, _) { Nil }) }), ) |> glint.execute([flag_input]) |> should.be_error() @@ -360,7 +351,7 @@ pub fn toggle_test() { glint.new() |> glint.add([], { - use flag <- glint.flag("flag", glint.bool()) + use flag <- glint.flag(glint.bool("flag")) use _, _, flags <- glint.command() flag(flags) |> should.equal(Ok(True)) @@ -374,9 +365,8 @@ pub fn toggle_test() { glint.new() |> glint.add([], { use flag <- glint.flag( - "flag", - glint.bool() - |> glint.default(True), + glint.bool("flag") + |> glint.default(True), ) use _, _, flags <- glint.command() flag(flags) @@ -388,7 +378,7 @@ pub fn toggle_test() { // boolean flag without default toggled, sets value to True glint.new() |> glint.add([], { - use flag <- glint.flag("flag", glint.bool()) + use flag <- glint.flag(glint.bool("flag")) use _, _, flags <- glint.command() flag(flags) |> should.equal(Ok(True)) @@ -400,9 +390,8 @@ pub fn toggle_test() { glint.new() |> glint.add([], { use _flag <- glint.flag( - "flag", - glint.int() - |> glint.default(1), + glint.int("flag") + |> glint.default(1), ) use _, _, _ <- glint.command() Nil @@ -415,39 +404,32 @@ pub fn getters_test() { glint.new() |> glint.add([], { use bflag <- glint.flag( - "bflag", - glint.bool() - |> glint.default(True), + glint.bool("bflag") + |> glint.default(True), ) use sflag <- glint.flag( - "sflag", - glint.string() - |> glint.default(""), + glint.string("sflag") + |> glint.default(""), ) use lsflag <- glint.flag( - "lsflag", - glint.strings() - |> glint.default([]), + glint.strings("lsflag") + |> glint.default([]), ) use iflag <- glint.flag( - "iflag", - glint.int() - |> glint.default(1), + glint.int("iflag") + |> glint.default(1), ) use liflag <- glint.flag( - "liflag", - glint.ints() - |> glint.default([]), + glint.ints("liflag") + |> glint.default([]), ) use fflag <- glint.flag( - "fflag", - glint.float() - |> glint.default(1.0), + glint.float("fflag") + |> glint.default(1.0), ) use lfflag <- glint.flag( - "lfflag", - glint.floats() - |> glint.default([]), + glint.floats("lfflag") + |> glint.default([]), ) use _, _, flags <- glint.command() diff --git a/test/glint_test.gleam b/test/glint_test.gleam index 7d3556f..6a8089e 100644 --- a/test/glint_test.gleam +++ b/test/glint_test.gleam @@ -97,68 +97,62 @@ pub fn runner_test() { pub fn help_test() { let nil = fn(_, _, _) { Nil } - let global_flag = #( - "global", - glint.string() - |> glint.flag_help("This is a global flag"), - ) - - let flag_1 = #( - "flag1", - glint.string() - |> glint.flag_help("This is flag1"), - ) - - let flag_2 = #( - "flag2", - glint.int() - |> glint.flag_help("This is flag2"), - ) - let flag_3 = #( - "flag3", - glint.bool() - |> glint.flag_help("This is flag3"), - ) - let flag_4 = #( - "flag4", - glint.float() - |> glint.flag_help("This is flag4"), - ) - - let flag_5 = #( - "flag5", - glint.floats() - |> glint.flag_help("This is flag5"), - ) + let global_flag = + glint.string("global") + |> glint.flag_help("This is a global flag") + + let flag_1 = + "flag1" + |> glint.string() + |> glint.flag_help("This is flag1") + + let flag_2 = + "flag2" + |> glint.int() + |> glint.flag_help("This is flag2") + let flag_3 = + "flag3" + |> glint.bool() + |> glint.flag_help("This is flag3") + + let flag_4 = + "flag4" + |> glint.float() + |> glint.flag_help("This is flag4") + + let flag_5 = + "flag5" + |> glint.floats() + |> glint.flag_help("This is flag5") let cli = glint.new() |> glint.name("test") |> glint.as_module - |> glint.group_flag([], global_flag.0, global_flag.1) + |> glint.group_flag([], global_flag) |> glint.add(at: [], do: { use <- glint.command_help("This is the root command") use _arg1 <- glint.named_arg("arg1") use _arg2 <- glint.named_arg("arg2") - use _flag <- glint.flag(flag_1.0, flag_1.1) + use _flag <- glint.flag(flag_1) glint.command(nil) }) |> glint.add(at: ["cmd1"], do: { use <- glint.command_help("This is cmd1") - use _flag2 <- glint.flag(flag_2.0, flag_2.1) - use _flag5 <- glint.flag(flag_5.0, flag_5.1) + use _flag2 <- glint.flag(flag_2) + use _flag5 <- glint.flag(flag_5) glint.command(nil) }) |> glint.add(at: ["cmd1", "cmd3"], do: { use <- glint.command_help("This is cmd3") - use _flag3 <- glint.flag(flag_3.0, flag_3.1) + use _flag3 <- glint.flag(flag_3) use <- glint.unnamed_args(glint.MinArgs(2)) use _woo <- glint.named_arg("woo") glint.command(nil) }) |> glint.add(at: ["cmd1", "cmd4"], do: { use <- glint.command_help("This is cmd4") - use _flag4 <- glint.flag(flag_4.0, flag_4.1) + use _flag4 <- glint.flag(flag_4) use <- glint.unnamed_args(glint.EqArgs(0)) glint.command(nil) }) @@ -294,8 +288,7 @@ pub fn global_and_group_flags_test() { glint.new() |> glint.group_flag( [], - "f", - glint.int() + glint.int("f") |> glint.default(2) |> glint.flag_help("global flag example"), ) @@ -308,10 +301,10 @@ pub fn global_and_group_flags_test() { ) |> glint.add(["sub"], { use f <- glint.flag( - "f", - glint.bool() - |> glint.default(True) - |> glint.flag_help("i decided to override the global flag"), + "f" + |> glint.bool() + |> glint.default(True) + |> glint.flag_help("i decided to override the global flag"), ) use _, _, flags <- glint.command() f(flags) @@ -319,16 +312,16 @@ pub fn global_and_group_flags_test() { }) |> glint.group_flag( ["sub"], - "sub_group_flag", - glint.int() + "sub_group_flag" + |> glint.int() |> glint.default(1), ) |> glint.add(["sub", "sub"], { use f <- glint.flag( - "f", - glint.bool() - |> glint.default(True) - |> glint.flag_help("i decided to override the global flag"), + "f" + |> glint.bool() + |> glint.default(True) + |> glint.flag_help("i decided to override the global flag"), ) use _, _, flags <- glint.command() f(flags)