diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 2ea8c3d..18f32ad 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -8,7 +8,7 @@ jobs: strategy: matrix: erlang: ["25.3.2.3", "26.0.2"] - gleam: ["0.32.4"] + gleam: ["0.33.0"] steps: - uses: actions/checkout@v2 - uses: ./.github/actions/test diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 7c97ed9..076823d 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -20,7 +20,7 @@ jobs: - uses: actions/checkout@v2 - uses: ./.github/actions/test with: - gleam-version: "0.32.4" + gleam-version: "0.33.0" - name: publish to hex env: HEXPM_USER: ${{ secrets.HEXPM_USER }} diff --git a/.tool-versions b/.tool-versions deleted file mode 100644 index 22690bd..0000000 --- a/.tool-versions +++ /dev/null @@ -1,2 +0,0 @@ -gleam 0.32.4 -nodejs 20.5.1 diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d7985a..4125d19 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # Changelog -## [Unreleased](https://github.com/TanklesXL/glint/compare/v0.13.0...HEAD) +## [0.14.0](https://github.com/TanklesXL/glint/compare/v0.14.0...HEAD) + +- updated to work with gleam 0.33 +- removed deprecated stub api ## [0.13.0](https://github.com/TanklesXL/glint/compare/v0.12.0...v0.13.0) diff --git a/README.md b/README.md index d6d5821..f945704 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,14 @@ gleam add glint 1. assign the command a custom description 1. run your cli with `glnt.run`, run with a function to handle command output with `glint.run_and_handle` -### Mini Example +## ✨ Complementary packages + +Glint works amazingly with these other packages: + +- [argv](https://github.com/lpil/argv), use this for cross-platform argument fetching +- [gleescript](https://github.com/lpil/gleescript), use this to generate erlang escripts for your applications + +## Mini Example You can import `glint` as a dependency and use it to build simple command-line applications like the following simplified version of the [the hello world example](https://github.com/TanklesXL/glint/tree/main/examples/hello/README.md) @@ -41,13 +48,10 @@ import gleam/result import gleam/string.{uppercase} // external dep imports import snag +import argv // glint imports import glint import glint/flag -// erlang-specific imports - -@target(erlang) -import gleam/erlang.{start_arguments} /// the key for the caps flag const caps = "caps" @@ -99,6 +103,6 @@ pub fn main() { // with a short description |> glint.description("Prints Hello, !"), ) - |> glint.run(start_arguments()) + |> glint.run(argv.load().arguments) } ``` diff --git a/examples/hello/.github/workflows/test.yml b/examples/hello/.github/workflows/test.yml index a13562c..5c35d7b 100644 --- a/examples/hello/.github/workflows/test.yml +++ b/examples/hello/.github/workflows/test.yml @@ -15,9 +15,8 @@ jobs: - uses: erlef/setup-beam@v1.15.4 with: otp-version: "25.2" - gleam-version: "0.30.3" + gleam-version: "0.33.0" rebar3-version: "3" - # elixir-version: "1.14.2" - run: gleam format --check src test - run: gleam deps download - run: gleam test diff --git a/examples/hello/gleam.toml b/examples/hello/gleam.toml index f966f4e..b47c920 100644 --- a/examples/hello/gleam.toml +++ b/examples/hello/gleam.toml @@ -1,6 +1,7 @@ name = "hello" version = "0.1.0" description = "A Gleam project" +gleam = ">= 0.33.0" # Fill out these fields if you intend to generate HTML documentation or publish # your project to the Hex package manager. @@ -13,7 +14,8 @@ description = "A Gleam project" gleam_stdlib = "~> 0.29" glint = { path = "../.." } snag = "~> 0.2" -gleam_erlang = "~> 0.19" +argv = "~> 1.0" [dev-dependencies] -gleeunit = "~> 0.10" +gleeunit = "~> 1.0" +gleescript = "~> 1.0" diff --git a/examples/hello/manifest.toml b/examples/hello/manifest.toml index 548e668..f5cdc76 100644 --- a/examples/hello/manifest.toml +++ b/examples/hello/manifest.toml @@ -2,19 +2,24 @@ # You typically do not need to edit this file packages = [ - { name = "gleam_bitwise", version = "1.3.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_bitwise", source = "hex", outer_checksum = "E2A46EE42E5E9110DAD67E0F71E7358CBE54D5EC22C526DD48CBBA3223025792" }, - { name = "gleam_community_ansi", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_stdlib", "gleam_bitwise"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "6E4E0CF2B207C1A7FCD3C21AA43514D67BC7004F21F82045CDCCE6C727A14862" }, - { name = "gleam_community_colour", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_bitwise", "gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "D27CE357ECB343929A8CEC3FBA0B499943A47F0EE1F589EE16AFC2DC21C61E5B" }, - { name = "gleam_erlang", version = "0.22.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "367D8B41A7A86809928ED1E7E55BFD0D46D7C4CF473440190F324AFA347109B4" }, - { name = "gleam_stdlib", version = "0.31.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "6D1BC5B4D4179B9FEE866B1E69FE180AC2CE485AD90047C0B32B2CA984052736" }, - { name = "gleeunit", version = "0.11.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "1397E5C4AC4108769EE979939AC39BF7870659C5AFB714630DEEEE16B8272AD5" }, - { name = "glint", version = "0.12.1", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_community_colour", "gleam_stdlib", "snag"], source = "local", path = "../.." }, - { name = "snag", version = "0.2.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "snag", source = "hex", outer_checksum = "8FD70D8FB3728E08AC425283BB509BB0F012BE1AE218424A597CDE001B0EE589" }, + { 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_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_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 = "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 = "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" }, ] [requirements] -gleam_erlang = { version = "~> 0.19" } +argv = { version = "~> 1.0" } gleam_stdlib = { version = "~> 0.29" } -gleeunit = { version = "~> 0.10" } +gleescript = { version = "~> 1.0" } +gleeunit = { version = "~> 1.0" } glint = { path = "../.." } snag = { version = "~> 0.2" } diff --git a/examples/hello/src/hello.gleam b/examples/hello/src/hello.gleam index b805405..683a400 100644 --- a/examples/hello/src/hello.gleam +++ b/examples/hello/src/hello.gleam @@ -7,9 +7,7 @@ import snag // glint imports import glint import glint/flag -// erlang-specific imports -@target(erlang) -import gleam/erlang.{start_arguments} +import argv // ----- APPLICATION LOGIC ----- @@ -122,8 +120,7 @@ pub fn app() { ) } -@target(erlang) pub fn main() { // run with a handler that prints the command output - glint.run_and_handle(app(), start_arguments(), io.println) + glint.run_and_handle(app(), argv.load().arguments, io.println) } diff --git a/gleam.toml b/gleam.toml index e8ac4ae..b0ee456 100644 --- a/gleam.toml +++ b/gleam.toml @@ -1,5 +1,5 @@ name = "glint" -version = "0.13.0" +version = "0.14.0" # Fill out these fields if you intend to generate HTML documentation or publishname = "glint" # your project to the Hex package manager. @@ -11,7 +11,7 @@ links = [ { title = "Hex", href = "https://hex.pm/packages/glint" }, { title = "Docs", href = "https://hexdocs.pm/glint/" }, ] -gleam = ">= 0.32.0" +gleam = ">= 0.33.0" [dependencies] gleam_stdlib = "~> 0.19" @@ -20,4 +20,4 @@ gleam_community_ansi = "~> 1.0" gleam_community_colour = "~> 1.0" [dev-dependencies] -gleeunit = "~> 0.5" +gleeunit = "~> 1.0" diff --git a/manifest.toml b/manifest.toml index 57fbc6c..5f80cac 100644 --- a/manifest.toml +++ b/manifest.toml @@ -2,16 +2,16 @@ # You typically do not need to edit this file packages = [ - { name = "gleam_community_ansi", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_stdlib"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "8B5A9677BC5A2738712BBAF2BA289B1D8195FDF962BBC769569976AD5E9794E1" }, - { name = "gleam_community_colour", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "036C206886AFB9F153C552700A7A0B4D2864E3BC96A20C77E5F34A013C051BE3" }, - { name = "gleam_stdlib", version = "0.32.1", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "ABF00CDCCB66FABBCE351A50060964C4ACE798F95A0D78622C8A7DC838792577" }, - { name = "gleeunit", version = "0.11.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "1397E5C4AC4108769EE979939AC39BF7870659C5AFB714630DEEEE16B8272AD5" }, - { name = "snag", version = "0.2.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "snag", source = "hex", outer_checksum = "8FD70D8FB3728E08AC425283BB509BB0F012BE1AE218424A597CDE001B0EE589" }, + { 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_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" }, + { name = "snag", version = "0.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "snag", source = "hex", outer_checksum = "54D32E16E33655346AA3E66CBA7E191DE0A8793D2C05284E3EFB90AD2CE92BCC" }, ] [requirements] gleam_community_ansi = { version = "~> 1.0" } gleam_community_colour = { version = "~> 1.0" } gleam_stdlib = { version = "~> 0.19" } -gleeunit = { version = "~> 0.5" } +gleeunit = { version = "~> 1.0" } snag = { version = "~> 0.2" } diff --git a/src/glint.gleam b/src/glint.gleam index b159016..152c063 100644 --- a/src/glint.gleam +++ b/src/glint.gleam @@ -1,4 +1,4 @@ -import gleam/map.{type Map} +import gleam/dict import gleam/option.{type Option, None, Some} import gleam/list import gleam/io @@ -93,7 +93,7 @@ pub type Runner(a) = type CommandNode(a) { CommandNode( contents: Option(Command(a)), - subcommands: Map(String, CommandNode(a)), + subcommands: dict.Dict(String, CommandNode(a)), ) } @@ -116,7 +116,7 @@ pub type CmdResult(a) = /// Creates a new command tree. /// pub fn new() -> Glint(a) { - Glint(config: default_config, cmd: empty_command(), global_flags: map.new()) + Glint(config: default_config, cmd: empty_command(), global_flags: dict.new()) } /// Adds a new command to be run at the specified path. @@ -154,7 +154,7 @@ fn do_add( CommandNode( ..root, subcommands: { - use node <- map.update(root.subcommands, x) + use node <- dict.update(root.subcommands, x) node |> option.lazy_unwrap(empty_command) |> do_add(xs, contents) @@ -166,7 +166,7 @@ fn do_add( /// Helper for initializing empty commands /// fn empty_command() -> CommandNode(a) { - CommandNode(contents: None, subcommands: map.new()) + CommandNode(contents: None, subcommands: dict.new()) } /// Trim each path element and remove any resulting empty strings. @@ -180,7 +180,7 @@ fn sanitize_path(path: List(String)) -> List(String) { /// Create a Command(a) from a Runner(a) /// pub fn command(do runner: Runner(a)) -> Command(a) { - Command(do: runner, flags: map.new(), description: "") + Command(do: runner, flags: dict.new(), description: "") } /// Attach a description to a Command(a) @@ -196,7 +196,7 @@ pub fn flag( at key: String, of flag: flag.FlagBuilder(_), ) -> Command(a) { - Command(..cmd, flags: map.insert(cmd.flags, key, flag.build(flag))) + Command(..cmd, flags: dict.insert(cmd.flags, key, flag.build(flag))) } /// Add a `flag.Flag to a `Command` when the flag name and builder are bundled as a #(String, flag.FlagBuilder(a)). @@ -216,7 +216,7 @@ pub fn flag_tuple( /// pub fn flags(cmd: Command(a), with flags: List(#(String, Flag))) -> Command(a) { use cmd, #(key, flag) <- list.fold(flags, cmd) - Command(..cmd, flags: map.insert(cmd.flags, key, flag)) + Command(..cmd, flags: dict.insert(cmd.flags, key, flag)) } /// Add global flags to the existing command tree @@ -228,7 +228,7 @@ pub fn global_flag( ) -> Glint(a) { Glint( ..glint, - global_flags: map.insert(glint.global_flags, key, flag.build(flag)), + global_flags: dict.insert(glint.global_flags, key, flag.build(flag)), ) } @@ -251,11 +251,8 @@ pub fn global_flags(glint: Glint(a), flags: List(#(String, Flag))) -> Glint(a) { Glint( ..glint, global_flags: { - list.fold( - flags, - glint.global_flags, - fn(acc, tup) { map.insert(acc, tup.0, tup.1) }, - ) + use acc, elem <- list.fold(flags, glint.global_flags) + dict.insert(acc, elem.0, elem.1) }, ) } @@ -315,18 +312,13 @@ fn do_execute( // when there are arguments remaining // check if the next one is a subcommand of the current command [arg, ..rest] -> - case map.get(cmd.subcommands, arg) { + case dict.get(cmd.subcommands, arg) { // subcommand found, continue Ok(cmd) -> - do_execute( - cmd, - config, - global_flags, - rest, - flags, - help, - [arg, ..command_path], - ) + do_execute(cmd, config, global_flags, rest, flags, help, [ + arg, + ..command_path + ]) // subcommand not found, but help flag has been passed // generate and return help message _ if help -> @@ -353,7 +345,7 @@ fn execute_root( Some(contents) -> { use new_flags <- result.try(list.try_fold( over: flag_inputs, - from: map.merge(global_flags, contents.flags), + from: dict.merge(global_flags, contents.flags), with: flag.update_flags, )) CommandInput(args, new_flags) @@ -450,7 +442,7 @@ fn usage_help(cmd_name: String, flags: FlagMap, config: Config) -> String { let app_name = option.unwrap(config.name, "gleam run") let flags = flags - |> map.to_list + |> dict.to_list |> list.map(flag.flag_type_help) |> list.sort(string.compare) @@ -470,7 +462,8 @@ fn usage_help(cmd_name: String, flags: FlagMap, config: Config) -> String { |> sb.prepend( config.pretty_help |> option.map(fn(styling) { heading_style(usage_heading, styling.usage) }) - |> option.unwrap(usage_heading) <> "\n\t", + |> option.unwrap(usage_heading) + <> "\n\t", ) |> sb.to_string } @@ -491,13 +484,15 @@ fn cmd_help( let flags = option.map(cmd.contents, fn(contents) { contents.flags }) - |> option.lazy_unwrap(map.new) - |> map.merge(global_flags, _) + |> option.lazy_unwrap(dict.new) + |> dict.merge(global_flags, _) let flags_help_body = config.pretty_help |> option.map(fn(p) { heading_style(flags_heading, p.flags) }) - |> option.unwrap(flags_heading) <> "\n\t" <> string.join( + |> option.unwrap(flags_heading) + <> "\n\t" + <> string.join( list.sort([help_flag_message, ..flag.flags_help(flags)], string.compare), "\n\t", ) @@ -521,7 +516,9 @@ fn cmd_help( subcommands_help_body -> config.pretty_help |> option.map(fn(p) { heading_style(subcommands_heading, p.subcommands) }) - |> option.unwrap(subcommands_heading) <> "\n\t" <> subcommands_help_body + |> option.unwrap(subcommands_heading) + <> "\n\t" + <> subcommands_help_body } // join the resulting help blocks into the final help message @@ -531,10 +528,10 @@ fn cmd_help( } // create the help text for subcommands -fn subcommands_help(cmds: Map(String, CommandNode(a))) -> String { +fn subcommands_help(cmds: dict.Dict(String, CommandNode(a))) -> String { cmds - |> map.map_values(subcommand_help) - |> map.values + |> dict.map_values(subcommand_help) + |> dict.values |> list.sort(string.compare) |> string.join("\n\t") } @@ -558,31 +555,3 @@ fn heading_style(heading: String, colour: Colour) -> String { |> ansi.hex(colour.to_rgb_hex(colour)) |> ansi.reset } - -// -- DEPRECATED: STUBS -- - -/// DEPRECATED: use `glint.cmd` and related new functions instead to create a Command -/// -/// Create command stubs to be used in `add_command_from_stub` -/// -pub type Stub(a) { - Stub( - path: List(String), - run: Runner(a), - flags: List(#(String, Flag)), - description: String, - ) -} - -/// Add a command to the root given a stub -/// -@deprecated("use `glint.cmd` and related new functions instead to create a Command") -pub fn add_command_from_stub(to glint: Glint(a), with stub: Stub(a)) -> Glint(a) { - add( - to: glint, - at: stub.path, - do: command(stub.run) - |> flags(stub.flags) - |> description(stub.description), - ) -} diff --git a/src/glint/flag.gleam b/src/glint/flag.gleam index 0a6cae1..5090ca7 100644 --- a/src/glint/flag.gleam +++ b/src/glint/flag.gleam @@ -1,4 +1,4 @@ -import gleam/map +import gleam/dict import gleam/string import gleam/result import gleam/int @@ -209,12 +209,12 @@ pub fn default(for builder: FlagBuilder(a), of default: a) -> FlagBuilder(a) { /// Associate flag names to their current values. /// pub type Map = - map.Map(String, Flag) + dict.Dict(String, Flag) /// Convert a list of flags to a Map. /// pub fn build_map(flags: List(#(String, Flag))) -> Map { - map.from_list(flags) + dict.from_list(flags) } /// Updates a flag value, ensuring that the new value can satisfy the required type. @@ -237,7 +237,7 @@ fn update_flag_value(in flags: Map, with data: #(String, String)) -> Result(Map) compute_flag(with: input, given: contents.value) |> result.map_error(layer_invalid_flag(_, key)), ) - map.insert(flags, key, Flag(..contents, value: value)) + dict.insert(flags, key, Flag(..contents, value: value)) } fn attempt_toggle_flag(in flags: Map, at key: String) -> Result(Map) { @@ -247,14 +247,14 @@ fn attempt_toggle_flag(in flags: Map, at key: String) -> Result(Map) { Internal(..internal, value: Some(True)) |> B |> fn(val) { Flag(..contents, value: val) } - |> map.insert(into: flags, for: key) - |> Ok() + |> dict.insert(into: flags, for: key) + |> Ok B(Internal(Some(val), ..) as internal) -> Internal(..internal, value: Some(!val)) |> B |> fn(val) { Flag(..contents, value: val) } - |> map.insert(into: flags, for: key) - |> Ok() + |> dict.insert(into: flags, for: key) + |> Ok _ -> Error(no_value_flag_err(key)) } } @@ -342,7 +342,7 @@ fn flag_help(flag: #(String, Flag)) -> String { /// pub fn flags_help(flags: Map) -> List(String) { flags - |> map.to_list + |> dict.to_list |> list.map(flag_help) } @@ -351,7 +351,7 @@ pub fn flags_help(flags: Map) -> List(String) { /// Access the contents for the associated flag /// fn access(flags: Map, name: String) -> Result(Flag) { - map.get(flags, name) + dict.get(flags, name) |> result.replace_error(undefined_flag_err(name)) } diff --git a/src/glint/flag/constraint.gleam b/src/glint/flag/constraint.gleam index e474bc2..0130d7d 100644 --- a/src/glint/flag/constraint.gleam +++ b/src/glint/flag/constraint.gleam @@ -19,11 +19,15 @@ pub fn one_of(allowed: List(a)) -> Constraint(a) { True -> Ok(Nil) False -> snag.error( - "invalid value '" <> string.inspect(val) <> "', must be one of: [" <> { + "invalid value '" + <> string.inspect(val) + <> "', must be one of: [" + <> { allowed |> list.map(fn(a) { "'" <> string.inspect(a) <> "'" }) - |> string.join(", ") <> "]" - }, + |> string.join(", ") + } + <> "]", ) } } @@ -38,11 +42,15 @@ pub fn none_of(disallowed: List(a)) -> Constraint(a) { False -> Ok(Nil) True -> snag.error( - "invalid value '" <> string.inspect(val) <> "', must not be one of: [" <> { + "invalid value '" + <> string.inspect(val) + <> "', must not be one of: [" + <> { { disallowed |> list.map(fn(a) { "'" <> string.inspect(a) <> "'" }) - |> string.join(", ") <> "]" + |> string.join(", ") + <> "]" } }, ) diff --git a/test/glint/flag_test.gleam b/test/glint/flag_test.gleam index 2aa1b61..8cdf586 100644 --- a/test/glint/flag_test.gleam +++ b/test/glint/flag_test.gleam @@ -1,7 +1,7 @@ import gleeunit/should import glint.{type CommandInput} import glint/flag -import gleam/map +import gleam/dict as map import gleam/list import gleam/string diff --git a/test/glint_test.gleam b/test/glint_test.gleam index 0d62dd1..a9d8786 100644 --- a/test/glint_test.gleam +++ b/test/glint_test.gleam @@ -177,8 +177,9 @@ pub fn help_test() { // help message for root command glint.execute(cli, [glint.help_flag()]) - |> should.equal(Ok(Help( - "This is the root command + |> should.equal( + Ok(Help( + "This is the root command USAGE: \ttest [ ARGS ] [ --flag1= --global= ] @@ -192,12 +193,14 @@ SUBCOMMANDS: \tcmd1\t\tThis is cmd1 \tcmd2\t\tThis is cmd2 \tcmd5", - ))) + )), + ) // help message for command glint.execute(cli, ["cmd1", glint.help_flag()]) - |> should.equal(Ok(Help( - "cmd1 + |> should.equal( + Ok(Help( + "cmd1 This is cmd1 USAGE: @@ -212,12 +215,14 @@ FLAGS: SUBCOMMANDS: \tcmd3\t\tThis is cmd3 \tcmd4\t\tThis is cmd4", - ))) + )), + ) // help message for nested command glint.execute(cli, ["cmd1", "cmd4", glint.help_flag()]) - |> should.equal(Ok(Help( - "cmd1 cmd4 + |> should.equal( + Ok(Help( + "cmd1 cmd4 This is cmd4 USAGE: @@ -227,12 +232,14 @@ FLAGS: \t--flag4=\t\tThis is flag4 \t--global=\t\tThis is a global flag \t--help\t\t\tPrint help information", - ))) + )), + ) // help message for command with no additional flags glint.execute(cli, ["cmd2", glint.help_flag()]) - |> should.equal(Ok(Help( - "cmd2 + |> should.equal( + Ok(Help( + "cmd2 This is cmd2 USAGE: @@ -241,7 +248,8 @@ USAGE: FLAGS: \t--global=\t\tThis is a global flag \t--help\t\t\tPrint help information", - ))) + )), + ) } pub fn global_flags_test() {