Skip to content

Commit

Permalink
add subcommands to usage text (#32)
Browse files Browse the repository at this point in the history
* add subcommands to usage text

* changelog
  • Loading branch information
TanklesXL authored Mar 19, 2024
1 parent 81cd1ed commit a582422
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 28 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## [Unreleased](https://github.com/TanklesXL/glint/compare/v0.18.0...HEAD)

- add subcommands to usage text

## [0.18.0](https://github.com/TanklesXL/glint/compare/v0.17.1...v0.18.0)

- support for group flags at a given path
Expand Down
44 changes: 18 additions & 26 deletions src/glint.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -814,26 +814,6 @@ fn args_count_to_usage_string(count: ArgsCount) -> String {
}
}

fn args_to_usage_string(
unnamed: Option(ArgsCount),
named: List(String),
) -> String {
let named_args =
named
|> list.map(fn(s) { "<" <> s <> ">" })
|> string.join(" ")
let unnamed_args =
option.map(unnamed, args_count_to_usage_string)
|> option.unwrap("[ ARGS ]")

case named_args, unnamed_args {
"", "" -> ""
"", _ -> unnamed_args
_, "" -> named_args
_, _ -> named_args <> " " <> unnamed_args
}
}

/// convert a CommandHelp to a styled usage block
///
fn command_help_to_usage_string(help: CommandHelp, config: Config) -> String {
Expand All @@ -844,8 +824,21 @@ fn command_help_to_usage_string(help: CommandHelp, config: Config) -> String {
}

let flags = flags_help_to_usage_string(help.flags)
let subcommands =
list.map(help.subcommands, fn(sc) { sc.name })
|> list.sort(string.compare)
|> string.join(" | ")
|> string_map(string.append("( ", _))
|> string_map(string.append(_, " )"))

let named_args =
help.named_args
|> list.map(fn(s) { "<" <> s <> ">" })
|> string.join(" ")

let args = args_to_usage_string(help.unnamed_args, help.named_args)
let unnamed_args =
option.map(help.unnamed_args, args_count_to_usage_string)
|> option.unwrap("[ ARGS ]")

case config.pretty_help {
None -> usage_heading
Expand All @@ -854,11 +847,10 @@ fn command_help_to_usage_string(help: CommandHelp, config: Config) -> String {
<> "\n\t"
<> app_name
<> string_map(help.meta.name, string.append(" ", _))
<> case args {
"" -> " "
_ -> " " <> args <> " "
}
<> flags
<> string_map(subcommands, string.append(" ", _))
<> string_map(named_args, string.append(" ", _))
<> string_map(unnamed_args, string.append(" ", _))
<> string_map(flags, string.append(" ", _))
}

// -- HELP - FUNCTIONS - STRINGIFIERS - FLAGS --
Expand Down
4 changes: 2 additions & 2 deletions test/glint_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ pub fn help_test() {
"This is the root command
USAGE:
\tgleam run -m test <arg1> <arg2> [ ARGS ] [ --flag1=<STRING> --global=<STRING> ]
\tgleam run -m test ( cmd1 | cmd2 | cmd5 ) <arg1> <arg2> [ ARGS ] [ --flag1=<STRING> --global=<STRING> ]
FLAGS:
\t--flag1=<STRING>\t\tThis is flag1
Expand All @@ -228,7 +228,7 @@ SUBCOMMANDS:
This is cmd1
USAGE:
\tgleam run -m test cmd1 [ ARGS ] [ --flag2=<INT> --flag5=<FLOAT_LIST> --global=<STRING> ]
\tgleam run -m test cmd1 ( cmd3 | cmd4 ) [ ARGS ] [ --flag2=<INT> --flag5=<FLOAT_LIST> --global=<STRING> ]
FLAGS:
\t--flag2=<INT>\t\tThis is flag2
Expand Down

0 comments on commit a582422

Please sign in to comment.