Skip to content

Support short flags - #101

Draft
karstenpedersen wants to merge 5 commits into
TanklesXL:mainfrom
karstenpedersen:support-short-flags
Draft

Support short flags#101
karstenpedersen wants to merge 5 commits into
TanklesXL:mainfrom
karstenpedersen:support-short-flags

Conversation

@karstenpedersen

@karstenpedersen karstenpedersen commented Jul 21, 2026

Copy link
Copy Markdown

Add support for short flags

Fixes #58

  • Add short: Option(String) field on Flag
  • Add shorthands lookup dictionary on Flags that maps short flags to regular flags
  • Add flag_short function to add shorthands to flags

I am a bit unsure what the best way is to make sure that short flags are exactly one character. Right now I just ignore them like this if they are invalid:

pub fn flag_short(for flag: Flag(a), of short: String) -> Flag(a) {
  case string.length(short) {
    1 -> Flag(..flag, short: Some(short))
    _ -> flag
  }
}

It could maybe be good to check it when the CLI execute.

examples/hello

Here is the repeat flag from the hello example:

pub fn repeat_flag() -> glint.Flag(Int) {
  use n <- glint.flag_constraint(
    glint.int_flag("repeat")
    |> glint.flag_short("r") // <---- NEW FUNCTION
    |> glint.flag_default(1)
    |> glint.flag_help("Repeat the message n-times"),
  )
  case n {
    _ if n > 0 -> Ok(n)
    _ -> snag.error("Value must be greater than 0.")
  }
}

This is the output we get when we run it with -h:

gleam run -m examples/hello -- -h
   Compiled in 0.03s
    Running examples/hello.main
It's time to say hello!

Prints Hello, <names>!

USAGE:
    gleam run -m examples/hello ( single ) [ 1 or more arguments ] [
        --caps=<BOOL> --repeat=<INT> ]

FLAGS:
        --caps=<BOOL>     Capitalize the hello message
    -h, --help            Print help information
    -r, --repeat=<INT>    Repeat the message n-times

SUBCOMMANDS:
    single                Prints Hello, <name>!

This is what we get when we run with -r=3:

gleam run -m examples/hello -- World -r=3
   Compiled in 0.04s
    Running examples/hello.main
Hello, World!
Hello, World!
Hello, World!

@karstenpedersen
karstenpedersen marked this pull request as ready for review July 21, 2026 22:54
@karstenpedersen
karstenpedersen marked this pull request as draft July 23, 2026 08:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

support short flags

1 participant