Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

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

- captured each flag's configured default value in the help data
(`help.Flag.default`)

# v1

## [1.3.0](https://github.com/TanklesXL/glint/compare/v1.2.1...v1.3.0)
Expand Down
24 changes: 24 additions & 0 deletions src/glint.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,29 @@ fn flag_type_info(flag: FlagEntry) {
}
}

fn flag_default_info(flag: FlagEntry) -> Option(String) {
case flag.value {
I(FlagInternals(value: Some(v), ..)) -> Some(int.to_string(v))
F(FlagInternals(value: Some(v), ..)) -> Some(float.to_string(v))
S(FlagInternals(value: Some(v), ..)) -> Some(v)
B(FlagInternals(value: Some(v), ..)) ->
Some(case v {
True -> "true"
False -> "false"
})
LI(FlagInternals(value: Some(v), ..)) -> Some(join_csv(v, int.to_string))
LF(FlagInternals(value: Some(v), ..)) -> Some(join_csv(v, float.to_string))
LS(FlagInternals(value: Some(v), ..)) -> Some(string.join(v, ","))
_ -> None
}
}

/// stringify each item and join with commas, for list-flag defaults.
///
fn join_csv(items: List(a), to_string: fn(a) -> String) -> String {
items |> list.map(to_string) |> string.join(",")
}

/// build the help representation for a list of flags
///
fn build_flags_help(flags: Flags) -> List(help.Flag) {
Expand All @@ -737,6 +760,7 @@ fn build_flags_help(flags: Flags) -> List(help.Flag) {
help.Flag(
meta: help.Metadata(name: name, description: flag.description),
type_: flag_type_info(flag),
default: flag_default_info(flag),
),
..acc
]
Expand Down
4 changes: 2 additions & 2 deletions src/glint/internal/help.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn heading_style(heading: String, colour: Colour) -> String {

// --- HELP: CONSTANTS ---
//
pub const help_flag = Flag(Metadata("help", "Print help information"), "")
pub const help_flag = Flag(Metadata("help", "Print help information"), "", None)

const flags_heading = "FLAGS:"

Expand Down Expand Up @@ -61,7 +61,7 @@ pub type Metadata {
/// Help type for flag metadata
///
pub type Flag {
Flag(meta: Metadata, type_: String)
Flag(meta: Metadata, type_: String, default: Option(String))
}

/// Help type for command metadata
Expand Down