clad

Functions

pub fn bool(
  long_name long_name: String,
  short_name short_name: String,
) -> fn(Dynamic) -> Result(Bool, List(DecodeError))

A decoder that decodes Bool arguments.

Examples

clad.bool(long_name: "verbose", short_name: "v")
|> clad.decode(["-v"])
// -> Ok(True)
clad.bool(long_name: "verbose", short_name: "v")
|> clad.decode([])
// -> Ok(False)
pub fn decode(
  decoder: fn(Dynamic) -> Result(a, List(DecodeError)),
  arguments: List(String),
) -> Result(a, List(DecodeError))

Run a decoder on a list of command line arguments, decoding the value if it is of the desired type, or returning errors.

This function works well with the argv package.

Examples

dynamic.decode2(
  SignUp,
  clad.string(long_name: "name", short_name: "n"),
  clad.string(long_name: "email", short_name: "e"),
)
|> clad.decode(["-n", "Lucy", "--email=lucy@example.com"])
// -> Ok(SignUp(name: "Lucy", email: "lucy@example.com"))

with argv:

dynamic.decode2(
  SignUp,
  clad.string(long_name: "name", short_name: "n"),
  clad.string(long_name: "email", short_name: "e"),
)
|> clad.decode(argv.load().arguments)
pub fn float(
  long_name long_name: String,
  short_name short_name: String,
) -> fn(Dynamic) -> Result(Float, List(DecodeError))

A decoder that decodes Float arguments.

Examples

clad.float(long_name: "price", short_name: "p")
|> clad.decode(["--price", "2.50"])
// -> Ok(2.5)
pub fn int(
  long_name long_name: String,
  short_name short_name: String,
) -> fn(Dynamic) -> Result(Int, List(DecodeError))

A decoder that decodes Int arguments.

Examples

clad.int(long_name: "count", short_name: "c")
|> clad.decode(["-c", "2"])
// -> Ok(2)
pub fn string(
  long_name long_name: String,
  short_name short_name: String,
) -> fn(Dynamic) -> Result(String, List(DecodeError))

A decoder that decodes String arguments.

Examples

clad.string(long_name: "name", short_name: "n")
|> clad.decode(["-n", "Lucy"])
// -> Ok("Lucy")
pub fn with_default(
  decoder: fn(Dynamic) -> Result(a, List(DecodeError)),
  default: a,
) -> fn(Dynamic) -> Result(a, List(DecodeError))

Provide a default value for a decoder.

Examples

clad.int(long_name: "count", short_name: "c") |> clad.with_default(1)
|> clad.decode([])
// -> Ok(1)
clad.int(long_name: "count", short_name: "c") |> clad.with_default(1)
|> clad.decode(["-c", "2"])
// -> Ok(2)
Search Document