Command line argument decoders for Gleam.
Clad aims to make it as easy as possible to parse command line arguments in Gleam. The goal is to support simple to medium complexity command line interfaces while staying as minimal as possible. It is inspired by minimist and gleam/json
gleam add clad
This program is in the examples directory
import argv
import clad
import decode/zero
pub type Student {
Student(name: String, age: Int, enrolled: Bool, classes: List(String))
}
pub fn main() {
let decoder = {
use name <- zero.field("name", zero.string)
use age <- zero.field("age", zero.int)
use enrolled <- zero.field("enrolled", zero.bool)
use classes <- clad.positional_arguments()
zero.success(Student(name:, age:, enrolled:, classes:))
}
// args: --name Lucy --age 8 --enrolled true math science art
let result = clad.decode(argv.load().arguments, decoder)
let assert Ok(Student("Lucy", 8, True, ["math", "science", "art"])) = result
}
Or, for more flexibility:
import argv
import clad
import decode/zero
pub type Student {
Student(name: String, age: Int, enrolled: Bool, classes: List(String))
}
pub fn main() {
let decoder = {
use name <- clad.opt("name", "n", zero.string)
use age <- clad.opt("age", "a", zero.int)
use enrolled <- clad.opt("enrolled", "e", clad.flag())
use classes <- clad.positional_arguments()
zero.success(Student(name:, age:, enrolled:, classes:))
}
// args: --name=Lucy -ea8 math science art
let result = clad.decode(argv.load().arguments, decoder)
let assert Ok(Student("Lucy", 8, True, ["math", "science", "art"])) = result
}
Further documentation can be found at https://hexdocs.pm/clad.