clip/opt
Functions for building Opt
s. An Opt
is a named option with a
value, such as --name "Drew"
Types
Functions
pub fn default(opt: Opt(a), default: a) -> Opt(a)
Provide a default value for an Opt
when it is not provided by the user.
pub fn float(opt: Opt(String)) -> Opt(Float)
Modify an Opt(String)
to produce a Float
.
opt.new("height")
|> opt.float
Note: float
changes the type of an Opt
and therefore clears any
previously set default value.
pub fn int(opt: Opt(String)) -> Opt(Int)
Modify an Opt(String)
to produce an Int
.
opt.new("age")
|> opt.int
Note: int
changes the type of an Opt
and therefore clears any
previously set default value.
pub fn map(opt: Opt(a), f: fn(a) -> b) -> Opt(b)
Modify the value produced by an Opt
in a way that cannot fail.
opt.new("name")
|> opt.map(fn(name) { string.uppercase(name) })
Note: map
can change the type of an Opt
and therefore clears any
previously set default value.
pub fn new(name: String) -> Opt(String)
Create a new Opt
with the provided name. New Opt
s always initially
produce a String
, which is the unmodified value given by the user on the
command line.
pub fn optional(opt: Opt(a)) -> Opt(Result(a, Nil))
Transform an Opt(a)
to an Opt(Result(a, Nil)
, making it optional.
pub fn run(
opt: Opt(a),
args: List(String),
) -> Result(#(a, List(String)), String)
Run an Opt(a)
against a list of arguments. Used internally by clip
, not
intended for direct usage.
pub fn short(opt: Opt(String), short_name: String) -> Opt(String)
Add a short name for the given Opt
. Short names are provided at the
command line with a single -
as a prefix.
clip.command(fn(a) { a })
|> clip.opt(opt.new("name") |> opt.short("n"))
|> clip.run(["-n", "Drew"])
// Ok("Drew")
pub fn to_arg_info(opt: Opt(a)) -> ArgInfo
Used internally, not intended for direct usage.
pub fn try_map(
opt: Opt(a),
f: fn(a) -> Result(b, String),
) -> Opt(b)
Modify the value produced by an Opt
in a way that may fail.
opt.new("age")
|> opt.try_map(fn(age_str) {
case int.parse(age_str) {
Ok(age) -> Ok(age)
Error(Nil) -> Error("Unable to parse integer")
}
})
Note: try_map
can change the type of an Opt
and therefore clears any
previously set default value.