glint
Gleam command line argument parsing with basic flag support.
Installation
To install from hex:
gleam add glint
Usage
You can import glint
as a dependency and use it as follows:
(found in examples/hello/src/hello.gleam
directory)
import gleam/io
import gleam/map
import gleam/string.{join, uppercase}
import gleam/erlang.{start_arguments}
import glint.{CommandInput}
import glint/flag
fn hello(input: CommandInput) {
assert Ok(flag.B(caps)) = flag.get_value(from: input.flags, for: "caps")
let to_say = ["Hello,", ..input.args]
case caps {
True ->
to_say
|> join(" ")
|> uppercase()
False -> join(to_say, " ")
}
|> string.append("!")
|> io.println()
}
pub fn main() {
glint.new()
|> glint.add_command(
at: [],
do: hello,
with: [flag.bool("caps", False, "Capitalize the provided name")],
described: "Prints Hello, <NAME>!",
used: "'gleam run <NAME>' or 'gleam run <NAME> --caps'",
)
|> glint.run(start_arguments())
}
Run it with either of:
gleam run Bob
,gleam run -- --caps=false Bob
, orgleam run Bob --caps=false
which will printHello, Bob!
gleam run -- --caps Bob
orgleam run -- --caps=true Bob
, orgleam run Bob --caps
which will printHELLO, BOB!
Built-In Help messages
Run gleam run -- --help
to print the generated help message, which in this case will look like:
Prints Hello, <NAME>!
USAGE:
'gleam run <NAME>' or 'gleam run <NAME> --caps'
FLAGS:
--help Print help information
--caps=<CAPS> Capitalize the provided name
Note: Due to this issue commands with flags immediately after gleam run
must include the --
as shown above