diff --git a/CHANGELOG.md b/CHANGELOG.md index bb2c92f..6ba50d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ ## [Unreleased](https://github.com/TanklesXL/glint/compare/v0.15.0...HEAD) - `glint.CommandResult(a)` is now a `Result(Out(a), String)` instead of a `Result(Out(a),Snag)` - command exectution failures due to things like invalid flags or too few args now print help text for the current command +- fix help text formatting for commands that do not include arguments +- remove named args from help text usage notes ## [0.15.0](https://github.com/TanklesXL/glint/compare/v0.14.0...v0.15.0) diff --git a/src/glint.gleam b/src/glint.gleam index acbce39..db9c5d7 100644 --- a/src/glint.gleam +++ b/src/glint.gleam @@ -467,8 +467,8 @@ fn execute_root( Error(#(snag, help)) -> Error( snag.pretty_print(snag) - <> "\nSee the following help text, available via the '--help' flag.\n\n" - <> help, + <> "\nSee the following help text, available via the '--help' flag.\n\n" + <> help, ) } } @@ -666,8 +666,8 @@ fn build_subcommands_help( Metadata( name: name, description: cmd.contents - |> option.map(fn(command) { command.description }) - |> option.unwrap(""), + |> option.map(fn(command) { command.description }) + |> option.unwrap(""), ), ..acc ] @@ -744,13 +744,6 @@ fn args_count_to_notes_string(count: Option(ArgsCount)) -> String { |> option.unwrap("") } -fn named_args_to_notes_string(named: List(String)) -> String { - named - |> list.map(fn(name) { "\"" <> name <> "\"" }) - |> string.join(", ") - |> string_map(fn(s) { "this command has named arguments: " <> s }) -} - fn args_to_usage_string(count: Option(ArgsCount), named: List(String)) -> String { case named @@ -773,15 +766,11 @@ fn args_to_usage_string(count: Option(ArgsCount), named: List(String)) -> String } } -fn usage_notes(count: Option(ArgsCount), named: List(String)) -> String { - [args_count_to_notes_string(count), named_args_to_notes_string(named)] - |> list.filter_map(fn(elem) { - case elem { - "" -> Error(Nil) - s -> Ok(string.append("\n* ", s)) - } - }) - |> string.concat +fn usage_notes(count: Option(ArgsCount)) -> String { + case args_count_to_notes_string(count) { + "" -> "" + s -> "\n* " <> s + } |> string_map(fn(s) { "\nnotes:" <> s }) } @@ -805,9 +794,12 @@ fn command_help_to_usage_string(help: CommandHelp, config: Config) -> String { <> "\n\t" <> app_name <> string_map(help.meta.name, string.append(" ", _)) - <> string_map(args, fn(s) { " " <> s <> " " }) + <> case args { + "" -> " " + _ -> " " <> args <> " " + } <> flags - <> usage_notes(help.count_args, help.named_args) + <> usage_notes(help.count_args) } // -- HELP - FUNCTIONS - STRINGIFIERS - FLAGS -- diff --git a/test/glint_test.gleam b/test/glint_test.gleam index d692693..27a7f36 100644 --- a/test/glint_test.gleam +++ b/test/glint_test.gleam @@ -100,35 +100,35 @@ pub fn help_test() { let global_flag = #( "global", flag.string() - |> flag.description("This is a global flag"), + |> flag.description("This is a global flag"), ) let flag_1 = #( "flag1", flag.string() - |> flag.description("This is flag1"), + |> flag.description("This is flag1"), ) let flag_2 = #( "flag2", flag.int() - |> flag.description("This is flag2"), + |> flag.description("This is flag2"), ) let flag_3 = #( "flag3", flag.bool() - |> flag.description("This is flag3"), + |> flag.description("This is flag3"), ) let flag_4 = #( "flag4", flag.float() - |> flag.description("This is flag4"), + |> flag.description("This is flag4"), ) let flag_5 = #( "flag5", flag.float_list() - |> flag.description("This is flag5"), + |> flag.description("This is flag5"), ) let cli = @@ -139,40 +139,41 @@ pub fn help_test() { |> glint.add( at: [], do: glint.command(do: nil) - |> glint.named_args(["arg1", "arg2"]) - |> glint.flag(flag_1.0, flag_1.1) - |> glint.description("This is the root command"), + |> glint.named_args(["arg1", "arg2"]) + |> glint.flag(flag_1.0, flag_1.1) + |> glint.description("This is the root command"), ) |> glint.add( at: ["cmd1"], do: glint.command(nil) - |> glint.flag(flag_2.0, flag_2.1) - |> glint.flag(flag_5.0, flag_5.1) - |> glint.description("This is cmd1"), + |> glint.flag(flag_2.0, flag_2.1) + |> glint.flag(flag_5.0, flag_5.1) + |> glint.description("This is cmd1"), ) |> glint.add( at: ["cmd1", "cmd3"], do: glint.command(nil) - |> glint.flag(flag_3.0, flag_3.1) - |> glint.description("This is cmd3"), + |> glint.flag(flag_3.0, flag_3.1) + |> glint.description("This is cmd3"), ) |> glint.add( at: ["cmd1", "cmd4"], do: glint.command(nil) - |> glint.flag(flag_4.0, flag_4.1) - |> glint.description("This is cmd4"), + |> glint.flag(flag_4.0, flag_4.1) + |> glint.description("This is cmd4") + |> glint.count_args(glint.EqArgs(0)), ) |> glint.add( at: ["cmd2"], do: glint.command(nil) - |> glint.named_args(["arg1", "arg2"]) - |> glint.count_args(glint.MinArgs(2)) - |> glint.description("This is cmd2"), + |> glint.named_args(["arg1", "arg2"]) + |> glint.count_args(glint.MinArgs(2)) + |> glint.description("This is cmd2"), ) |> glint.add( at: ["cmd5", "cmd6"], do: glint.command(nil) - |> glint.description("This is cmd6"), + |> glint.description("This is cmd6"), ) // execute root command @@ -205,8 +206,6 @@ pub fn help_test() { USAGE: \tgleam run -m test [ --flag1= --global= ] -notes: -* this command has named arguments: \"arg1\", \"arg2\" FLAGS: \t--flag1=\t\tThis is flag1 @@ -250,7 +249,9 @@ SUBCOMMANDS: This is cmd4 USAGE: -\tgleam run -m test cmd1 cmd4 [ ARGS ] [ --flag4= --global= ] +\tgleam run -m test cmd1 cmd4 [ --flag4= --global= ] +notes: +* this command accepts no arguments FLAGS: \t--flag4=\t\tThis is flag4 @@ -269,7 +270,6 @@ USAGE: \tgleam run -m test cmd2 ... [ --global= ] notes: * this command accepts 2 or more arguments -* this command has named arguments: \"arg1\", \"arg2\" FLAGS: \t--global=\t\tThis is a global flag @@ -284,8 +284,8 @@ pub fn global_flags_test() { |> glint.global_flag( "f", flag.int() - |> flag.default(2) - |> flag.description("global flag example"), + |> flag.default(2) + |> flag.description("global flag example"), ) |> glint.add( [], @@ -297,18 +297,18 @@ pub fn global_flags_test() { |> glint.add( ["sub"], glint.command(fn(ctx) { - flag.get_bool(ctx.flags, "f") - |> should.equal(Ok(True)) - }) - |> glint.flag( - "f", - flag.bool() - |> flag.default(True) - |> flag.description("i decided to override the global flag"), - ), + flag.get_bool(ctx.flags, "f") + |> should.equal(Ok(True)) + }) + |> glint.flag( + "f", + flag.bool() + |> flag.default(True) + |> flag.description("i decided to override the global flag"), + ), ) - // root command keeps the global flag as an int + // root command keeps the global flag as an int cli |> glint.execute(["--f=2"]) |> should.be_ok