### Gleam Mini Example for Glint CLI App Source: https://github.com/tanklesxl/glint/blob/main/README.md This example demonstrates a basic 'hello world' style command-line application using the Glint library. It defines a command that accepts a name and an optional flag to capitalize the output. It showcases creating flags, commands, and integrating with `glint.new` and `glint.run`. ```gleam import gleam/io import gleam/list import gleam/result import gleam/string.{uppercase} import glint import argv // this function returns the builder for the caps flag fn caps_flag() -> glint.Flag(Bool) { // create a new boolean flag with key "caps" // this flag will be called as --caps=true (or simply --caps as glint handles boolean flags in a bit of a special manner) from the command line glint.bool_flag("caps") // set the flag default value to False |> glint.flag_default(False) // set the flag help text |> glint.flag_help("Capitalize the hello message") } /// the glint command that will be executed /// fn hello() -> glint.Command(Nil) { // set the help text for the hello command use <- glint.command_help("Prints Hello, !") // register the caps flag with the command // the `caps` variable there is a type-safe getter for the flag value use caps <- glint.flag(caps_flag()) // start the body of the command // this is what will be executed when the command is called use _, args, flags <- glint.command() // we can assert here because the caps flag has a default // and will therefore always have a value assigned to it let assert Ok(caps) = caps(flags) // this is where the business logic of our command starts let name = case args { [] -> "Joe" [name,..] -> name } let msg = "Hello, " <> name <> "!" case caps { True -> uppercase(msg) False -> msg } |> io.println } pub fn main() { // create a new glint instance glint.new() // with an app name of "hello", this is used when printing help text |> glint.with_name("hello") // with pretty help enabled, using the built-in colours |> glint.pretty_help(glint.default_pretty_help()) // with a root command that executes the `hello` function |> glint.add(at: [], do: hello()) // execute given arguments from stdin |> glint.run(argv.load().arguments) } ``` -------------------------------- ### Gleam Installation Command for Glint Source: https://github.com/tanklesxl/glint/blob/main/README.md This snippet shows the command to add the Glint library as a dependency to a Gleam project using the `gleam add` command. It is a standard package management operation in the Gleam ecosystem. ```sh gleam add glint ``` -------------------------------- ### Gleam: Constrain List of Integers with `one_of` and `each` Source: https://github.com/tanklesxl/glint/blob/main/README.md This example shows how to constrain a list of integers using Glint's `constraint.each` and `constraint.one_of` utilities. It ensures that each integer in the `my_ints` flag is one of the allowed values [1, 2, 3, 4]. ```gleam import glint import glint/constraint import snag // ... glint.ints_flag("my_ints") |> glint.flag_default([]) |> glint.flag_constraint( [1, 2, 3, 4] |> constraint.one_of |> constraint.each ) ``` -------------------------------- ### Initialize Glint CLI Application Source: https://context7.com/tanklesxl/glint/llms.txt Initializes a new Glint CLI application instance with basic configuration, sets the application name, customizes the help message format, defines the root command, and runs the application with provided arguments. ```gleam import glint import argv pub fn main() { glint.new() |> glint.with_name("myapp") |> glint.pretty_help(glint.default_pretty_help()) |> glint.as_module() |> glint.add(at: [], do: root_command()) |> glint.run(argv.load().arguments) } fn root_command() -> glint.Command(Nil) { use <- glint.command_help("Root command description") use _, args, flags <- glint.command() // Command logic here Nil } ``` -------------------------------- ### Add Subcommands with Named Arguments Source: https://context7.com/tanklesxl/glint/llms.txt Creates a CLI tool with nested subcommands. The 'user add' command requires 'username' and 'email' as named arguments and supports an optional 'admin' flag, demonstrating hierarchical command structure and argument parsing. ```gleam import glint import gleam/io import argv fn user_add_command() -> glint.Command(Nil) { use <- glint.command_help("Add a new user") use username <- glint.named_arg("username") use email <- glint.named_arg("email") use admin <- glint.flag( glint.bool_flag("admin") |> glint.flag_default(False) |> glint.flag_help("Grant admin privileges") ) use named, _, flags <- glint.command() let user = username(named) let user_email = email(named) let assert Ok(is_admin) = admin(flags) io.println("Creating user: " <> user) io.println("Email: " <> user_email) io.println("Admin: " <> case is_admin { True -> "yes" | False -> "no" }) Nil } pub fn main() { glint.new() |> glint.with_name("usertool") |> glint.global_help("User management tool") |> glint.add(at: ["user", "add"], do: user_add_command()) |> glint.run(argv.load().arguments) } // Usage: gleam run -- user add john john@example.com --admin ``` -------------------------------- ### Handle Command Return Values Programmatically with `glint.run_and_handle` in Gleam Source: https://context7.com/tanklesxl/glint/llms.txt Executes a command and handles its return value programmatically using `glint.run_and_handle`. This is useful for commands that produce structured data that needs further processing, like saving to a file or sending to an API. ```gleam import glint import gleam/io import gleam/result import argv fn calculate_command() -> glint.Command(Int) { use <- glint.command_help("Calculate a value") use multiplier <- glint.flag( glint.int_flag("multiply") |> glint.flag_default(1) |> glint.flag_help("Multiply result by this value") ) use _, args, flags <- glint.command() let assert Ok(mult) = multiplier(flags) let base_value = 42 base_value * mult } pub fn main() { let app = glint.new() |> glint.with_name("calculator") |> glint.add(at: [], do: calculate_command()) glint.run_and_handle(app, argv.load().arguments, fn(result) { io.println("Calculation result: " <> int.to_string(result)) // Could save to file, send to API, etc. }) } // Usage: gleam run -- --multiply=5 // Output: Calculation result: 210 ``` -------------------------------- ### Share Group Flags Across Commands (Gleam) Source: https://context7.com/tanklesxl/glint/llms.txt Demonstrates how to define a flag once and make it available to multiple commands within a command hierarchy using Glint. This reduces code duplication for common configurations like a config file path. ```gleam import glint import gleam/io import argv fn config_flag() -> glint.Flag(String) { glint.string_flag("config") |> glint.flag_default("config.toml") |> glint.flag_help("Configuration file path") } fn deploy_command() -> glint.Command(Nil) { use <- glint.command_help("Deploy the application") use _, _, flags <- glint.command() let assert Ok(config_path) = glint.get_flag(flags, config_flag()) io.println("Deploying with config: " <> config_path) Nil } fn rollback_command() -> glint.Command(Nil) { use <- glint.command_help("Rollback the deployment") use _, _, flags <- glint.command() let assert Ok(config_path) = glint.get_flag(flags, config_flag()) io.println("Rolling back with config: " <> config_path) Nil } pub fn main() { glint.new() |> glint.with_name("deployer") |> glint.group_flag([], config_flag()) // Available to all commands |> glint.add(at: ["deploy"], do: deploy_command()) |> glint.add(at: ["rollback"], do: rollback_command()) |> glint.run(argv.load().arguments) } // Usage: gleam run -- deploy --config=prod.toml // Usage: gleam run -- rollback --config=prod.toml ``` -------------------------------- ### Parse Comma-Separated Typed Lists (Gleam) Source: https://context7.com/tanklesxl/glint/llms.txt Illustrates how to use Glint to parse comma-separated string inputs into typed lists of integers or strings. This is useful for flags that accept multiple values, such as a list of numbers or tags. ```gleam import glint import gleam/io import gleam/int import gleam/list import argv fn stats_command() -> glint.Command(Nil) { use <- glint.command_help("Calculate statistics") use numbers <- glint.flag( glint.ints_flag("numbers") |> glint.flag_help("Comma-separated list of integers") ) use tags <- glint.flag( glint.strings_flag("tags") |> glint.flag_default([]) |> glint.flag_help("Comma-separated list of tags") ) use _, _, flags <- glint.command() case numbers(flags) { Ok(nums) -> { let sum = list.fold(nums, 0, int.add) let count = list.length(nums) io.println("Sum: " <> int.to_string(sum)) io.println("Count: " <> int.to_string(count)) } Error(_) -> io.println("Error: --numbers flag is required") } let assert Ok(tag_list) = tags(flags) case tag_list { [] -> Nil tags -> io.println("Tags: " <> string.join(tags, ", ")) } Nil } pub fn main() { glint.new() |> glint.with_name("stats") |> glint.add(at: [], do: stats_command()) |> glint.run(argv.load().arguments) } // Usage: gleam run -- --numbers=1,2,3,4,5 --tags=prod,urgent ``` -------------------------------- ### Define Command with Typed Flags Source: https://context7.com/tanklesxl/glint/llms.txt Defines a command with typed flags for 'verbose' (boolean) and 'output' (string), setting default values and help text. It then accesses these flags within the command logic to conditionally print output. ```gleam import glint import gleam/io fn build_command() -> glint.Command(Nil) { use <- glint.command_help("Build the project") use verbose <- glint.flag( glint.bool_flag("verbose") |> glint.flag_default(False) |> glint.flag_help("Enable verbose output") ) use output <- glint.flag( glint.string_flag("output") |> glint.flag_default("./build") |> glint.flag_help("Output directory") ) use _, args, flags <- glint.command() let assert Ok(verbose_enabled) = verbose(flags) let assert Ok(output_dir) = output(flags) case verbose_enabled { True -> io.println("Building to: " <> output_dir) False -> Nil } // Build logic here Nil } pub fn main() { glint.new() |> glint.with_name("builder") |> glint.add(at: ["build"], do: build_command()) |> glint.run(argv.load().arguments) } ``` -------------------------------- ### Apply List Item Constraints with `constraint.each` in Gleam Source: https://context7.com/tanklesxl/glint/llms.txt Validates each element within a list flag individually using `constraint.each`. This ensures all provided ports are within an allowed set, preventing invalid configurations. ```gleam import glint import glint/constraint import gleam/io import gleam/int import argv fn ports_command() -> glint.Command(Nil) { use <- glint.command_help("Configure allowed ports") use ports <- glint.flag( glint.ints_flag("ports") |> glint.flag_default([8080, 8443]) |> glint.flag_constraint( constraint.one_of([8080, 8443, 3000, 3001, 5000]) |> constraint.each ) |> glint.flag_help("List of allowed ports") ) use _, _, flags <- glint.command() let assert Ok(port_list) = ports(flags) io.println("Configured ports:") list.each(port_list, fn(p) { io.println(" - " <> int.to_string(p)) }) Nil } pub fn main() { glint.new() |> glint.with_name("portconfig") |> glint.add(at: [], do: ports_command()) |> glint.run(argv.load().arguments) } // Usage: gleam run -- --ports=8080,3000,5000 (OK) // Invalid: gleam run -- --ports=8080,9999 (9999 not in allowed list) ``` -------------------------------- ### Specify Unnamed Argument Counts Source: https://context7.com/tanklesxl/glint/llms.txt Demonstrates defining commands that accept a specific number of unnamed positional arguments. 'concat_command' requires at least two arguments and supports a 'separator' flag, while 'exact_args_command' requires exactly two arguments. ```gleam import glint import gleam/io import gleam/list import gleam/string import argv fn concat_command() -> glint.Command(Nil) { use <- glint.command_help("Concatenate files") use <- glint.unnamed_args(glint.MinArgs(2)) use separator <- glint.flag( glint.string_flag("separator") |> glint.flag_default(" ") |> glint.flag_help("Separator between files") ) use _, args, flags <- glint.command() let assert Ok(sep) = separator(flags) let result = string.join(args, sep) io.println("Concatenating " <> string.inspect(list.length(args)) <> " files with: " <> sep) io.println(result) Nil } fn exact_args_command() -> glint.Command(Nil) { use <- glint.command_help("Takes exactly 2 arguments") use <- glint.unnamed_args(glint.EqArgs(2)) use _, args, _ <- glint.command() let assert [first, second] = args io.println("First: " <> first <> ", Second: " <> second) Nil } pub fn main() { glint.new() |> glint.with_name("filetools") |> glint.add(at: ["concat"], do: concat_command()) |> glint.add(at: ["exact"], do: exact_args_command()) |> glint.run(argv.load().arguments) } // Usage: gleam run -- concat file1.txt file2.txt file3.txt --separator="," // Usage: gleam run -- exact arg1 arg2 ``` -------------------------------- ### Constrain Flag Values with `one_of` (Gleam) Source: https://context7.com/tanklesxl/glint/llms.txt Demonstrates how to restrict a flag's accepted values to a predefined list using Glint's `one_of` constraint. This ensures that user input conforms to expected options, such as specific environments or ports. ```gleam import glint import glint/constraint import gleam/io import argv fn server_command() -> glint.Command(Nil) { use <- glint.command_help("Start a server") use env <- glint.flag( glint.string_flag("env") |> glint.flag_default("development") |> glint.flag_constraint(constraint.one_of(["development", "staging", "production"])) |> glint.flag_help("Environment to run in") ) use port <- glint.flag( glint.int_flag("port") |> glint.flag_default(8080) |> glint.flag_constraint(constraint.one_of([8080, 8443, 3000])) |> glint.flag_help("Port to listen on") ) use _, _, flags <- glint.command() let assert Ok(environment) = env(flags) let assert Ok(port_num) = port(flags) io.println("Starting server in " <> environment <> " mode on port " <> int.to_string(port_num)) Nil } pub fn main() { glint.new() |> glint.with_name("server") |> glint.add(at: ["start"], do: server_command()) |> glint.run(argv.load().arguments) } // Usage: gleam run -- start --env=production --port=8443 // Invalid: gleam run -- start --env=testing (will error) ``` -------------------------------- ### Constrain Flag Values with `none_of` (Gleam) Source: https://context7.com/tanklesxl/glint/llms.txt Shows how to use Glint's `none_of` constraint to prevent a flag from accepting specific forbidden values. This is useful for avoiding reserved keywords or problematic inputs, such as administrative usernames. ```gleam import glint import glint/constraint import gleam/io import argv fn username_command() -> glint.Command(Nil) { use <- glint.command_help("Create a username") use name <- glint.flag( glint.string_flag("name") |> glint.flag_constraint(constraint.none_of(["admin", "root", "system"])) |> glint.flag_help("Username to create") ) use _, _, flags <- glint.command() case name(flags) { Ok(username) -> io.println("Creating user: " <> username) Error(_) -> io.println("Error: --name flag is required") } Nil } pub fn main() { glint.new() |> glint.with_name("usercreate") |> glint.add(at: [], do: username_command()) |> glint.run(argv.load().arguments) } // Usage: gleam run -- --name=john (OK) // Invalid: gleam run -- --name=admin (will error) ``` -------------------------------- ### Gleam: Define Positive Integer Flag Constraint Source: https://github.com/tanklesxl/glint/blob/main/README.md This snippet demonstrates how to define a constraint for an integer flag in Gleam using pipes or the 'use' keyword. The constraint ensures that the flag's value is not negative. It uses the `glint.int_flag`, `glint.flag_default`, and `glint.constraint` functions. ```gleam import glint import snag // ... // with pipes glint.int_flag("my_int") |> glint.flag_default(0) |> glint.constraint(fn(i){ case i < 0 { True -> snag.error("cannot be negative") False -> Ok(i) } }) // or // with use use i <- glint.flag_constraint( glint.int_flag("my_int") |> glint.flag_default(0) ) case i < 0 { True -> snag.error("cannot be negative") False -> Ok(i) } ``` -------------------------------- ### Define Custom Validation Logic with Custom Constraint Functions in Gleam Source: https://context7.com/tanklesxl/glint/llms.txt Implements custom validation logic for flags using custom constraint functions that return a `snag.Result`. This allows for flexible and detailed input validation, such as checking for positive numbers or ranges. ```gleam import glint import gleam/io import gleam/int import snag import argv fn positive_constraint(value: Int) -> snag.Result(Int) { case value > 0 { True -> Ok(value) False -> snag.error("Value must be positive (greater than 0)") } } fn range_constraint(min: Int, max: Int) -> fn(Int) -> snag.Result(Int) { fn(value: Int) -> snag.Result(Int) { case value >= min && value <= max { True -> Ok(value) False -> snag.error( "Value must be between " <> int.to_string(min) <> " and " <> int.to_string(max) ) } } } fn worker_command() -> glint.Command(Nil) { use <- glint.command_help("Configure worker processes") use workers <- glint.flag( glint.int_flag("workers") |> glint.flag_default(4) |> glint.flag_constraint(positive_constraint) |> glint.flag_constraint(range_constraint(1, 32)) |> glint.flag_help("Number of worker processes") ) use _, _, flags <- glint.command() let assert Ok(count) = workers(flags) io.println("Starting " <> int.to_string(count) <> " worker processes") Nil } pub fn main() { glint.new() |> glint.with_name("workers") |> glint.add(at: [], do: worker_command()) |> glint.run(argv.load().arguments) } // Usage: gleam run -- --workers=8 (OK) // Invalid: gleam run -- --workers=0 (fails positive constraint) // Invalid: gleam run -- --workers=100 (fails range constraint) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.