Skip to content

Commit

Permalink
start setting up arguments help section
Browse files Browse the repository at this point in the history
  • Loading branch information
TanklesXL committed Dec 19, 2024
1 parent b0921f8 commit a6733d5
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 12 deletions.
2 changes: 0 additions & 2 deletions .mise.toml

This file was deleted.

14 changes: 7 additions & 7 deletions src/glint.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -681,13 +681,13 @@ fn build_command_help(name: String, node: CommandNode(_)) -> help.Command {
///
fn parameters_type_info(p: Parameters(a)) {
case p {
I(_) -> "INT"
B(_) -> "BOOL"
F(_) -> "FLOAT"
LF(_) -> "FLOAT_LIST"
LI(_) -> "INT_LIST"
LS(_) -> "STRING_LIST"
S(_) -> "STRING"
B(_) -> "bool"
I(_) -> "int"
F(_) -> "float"
S(_) -> "string"
LF(_) -> "float list (comma-separated)"
LI(_) -> "int list (comma-separated)"
LS(_) -> "string list (comma-separated)"
}
}

Expand Down
40 changes: 39 additions & 1 deletion src/glint/internal/help.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const subcommands_heading = "SUBCOMMANDS:"

const usage_heading = "USAGE:"

const named_args_heading = "ARGUMENTS:"

// --- HELP: TYPES ---

pub type ArgsCount {
Expand Down Expand Up @@ -86,6 +88,7 @@ pub fn command_help_to_string(help: Command, config: Config) -> String {
command_description,
command_help_to_usage_string(help, config),
subcommands_help_to_string(help.subcommands, config),
named_args_help_to_string(help.named_args, config),
flags_help_to_string(help.flags, config),
]
|> list.filter(fn(s) { s != "" })
Expand Down Expand Up @@ -158,7 +161,7 @@ fn command_help_to_usage_string(help: Command, config: Config) -> String {

// -- HELP - FUNCTIONS - STRINGIFIERS - FLAGS --

/// generate the usage help string for a command
/// generate the usage help string for a list of flags
///
fn flags_help_to_string(help: List(Parameter), config: Config) -> String {
use <- bool.guard(help == [], "")
Expand Down Expand Up @@ -219,6 +222,41 @@ fn subcommands_help_to_string(help: List(Metadata), config: Config) -> String {
heading <> content
}

// -- HELP - FUNCTIONS - STRINGIFIERS - NAMED ARGUMENTS --
/// generate the usage help string for named arguments
///
fn named_args_help_to_string(help: List(Parameter), config: Config) -> String {
use <- bool.guard(help == [], "")

let longest_arg_length =
help
|> list.map(named_arg_help_to_string(_))
|> utils.max_string_length
|> int.max(config.min_first_column_width)

let heading = config.named_args_colour(named_args_heading)

let content =
to_spaced_indented_string(
help,
fn(help) { #(named_arg_help_to_string(help), help.meta.description) },
longest_arg_length,
config,
)

heading <> content
}

/// generate the help text for a flag without a description
///
fn named_arg_help_to_string(help: Parameter) -> String {
help.meta.name
<> case help.type_ {
"" -> ""
_ -> ": " <> help.type_
}
}

/// convert a list of items to an indented string with spaced contents
///
fn to_spaced_indented_string(
Expand Down
33 changes: 31 additions & 2 deletions test/examples/hello.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,16 @@ pub fn capitalize(msg, caps) -> String {

/// hello is a function that says hello
pub fn hello(names: List(String), caps: Bool, repeat: Int) -> String {
{ "Hello, " <> join_names(names) <> "!" }
greet("Hello", names, caps, repeat)
}

pub fn greet(
greeting: String,
names: List(String),
caps: Bool,
repeat: Int,
) -> String {
{ greeting <> ", " <> join_names(names) <> "!" }
|> capitalize(caps)
|> list.repeat(repeat)
|> string.join("\n")
Expand Down Expand Up @@ -84,12 +93,27 @@ pub fn hello_cmd() -> glint.Command(String) {
hello(args, caps, repeat)
}

pub fn hello_custom_cmd() -> glint.Command(String) {
use <- glint.command_help("Prints a greeting for the names provided!")
use greeting <- glint.named_arg(
glint.string("greeting") |> glint.param_help("The greeting to give."),
)
use <- glint.unnamed_args(glint.MinArgs(1))
use named_args, args, flags <- glint.command()
let assert Ok(caps) = glint.get_flag(flags, caps_flag())
let assert Ok(repeat) = glint.get_flag(flags, repeat_flag())
greet(greeting(named_args), args, caps, repeat)
}

/// the command function that will be executed as the "single" command
///
pub fn hello_single_cmd() -> glint.Command(String) {
use <- glint.command_help("Prints Hello, <name>!")
use <- glint.unnamed_args(glint.EqArgs(0))
use name <- glint.named_arg(glint.string("name"))
use name <- glint.named_arg(
glint.string("name")
|> glint.param_help("The name of the person we're saying hello to."),
)
use named_args, _, flags <- glint.command()
let assert Ok(caps) = glint.get_flag(flags, caps_flag())
let assert Ok(repeat) = glint.get_flag(flags, repeat_flag())
Expand Down Expand Up @@ -125,6 +149,11 @@ pub fn app() {
at: ["single"],
do: hello_single_cmd(),
)
|> glint.add(
// add the hello custom command
at: ["custom"],
do: hello_custom_cmd(),
)
}

pub fn main() {
Expand Down

0 comments on commit a6733d5

Please sign in to comment.