Module
arglet
src/arglet.pookaA little argument parser, small enough to read over one cup of tea.
Arglet does two things. It fetches the command line from the Erlang runtime, and it sorts those words into flags, values and positionals, according to a short list of things you told it to expect.
import arglet
main() {
let expected = [
arglet.flag("verbose", "v", "Say more about what is happening"),
arglet.value("output", "o", "Where to write the result")
]
match arglet.parse(arglet.arguments(), expected) {
Ok(args) -> {
echo args.positionals
echo args.has("verbose")
echo args.get("output")
Nil
}
Error(problem) -> {
echo arglet.describe(problem)
Nil
}
}
}
For pooka run -- build -v --output=out.txt, args.positionals is
["build"], args.has("verbose") is True, and args.get("output") is
Some("out.txt").
API reference
class Arglet(runtime: String, program: String, arguments: List(String))
The command line, with each traveller in its own seat.
runtime is the Erlang executable. program is the escript path, or the
working directory outside an escript. arguments holds the words the user
typed, without runtime options or the escript path.
runtime: String
Stringprogram: String
Stringarguments: List(String)
List(String)
fn(String, String, List(String)) -> arglet.Argletload() -> Arglet
fn() -> arglet.ArgletRead the current invocation without parsing it.
import arglet
main() {
echo arglet.load().program
}
arguments() -> List(String)
fn() -> List(String)Only the words the user typed, in the order they typed them.
import arglet
main() {
echo arglet.arguments()
}
class Spec
class Flag(name: String, short: String, help: String) is Spec
A switch that is either present or absent, such as --verbose.
name: String
Stringshort: String
Stringhelp: String
String
fn(String, String, String) -> arglet.Flagclass Value(name: String, short: String, help: String) is Spec
An option that carries a value, such as --output out.txt.
name: String
Stringshort: String
Stringhelp: String
String
fn(String, String, String) -> arglet.Valueflag(name: String, short: String, help: String) -> Spec
fn(String, String, String) -> arglet.SpecExpect a switch. name is used as --name, and short as -s. Pass ""
for short when there is no short form.
import arglet
let quiet = arglet.flag("quiet", "q", "Say less")
value(name: String, short: String, help: String) -> Spec
fn(String, String, String) -> arglet.Specclass Parsed(
flags: List(String),
values: List(#(String, String)),
positionals: List(String),
)
The command line after sorting.
flags lists each switch once per appearance, so -vvv appears three
times. values pairs each option name with its value, in order.
positionals holds everything else, including every word after --.
Names are always the long ones, whichever form the user typed.
flags: List(String)
List(String)values: List(#(String, String))
List(#(String, String))positionals: List(String)
List(String)
fn(List(String), List(#(String, String)), List(String)) -> arglet.Parsedhas(name: String) -> Bool
fn(String) -> BoolWas this switch given at least once?
count(name: String) -> Int
fn(String) -> IntHow many times was this switch given? Useful for -vvv.
get(name: String) -> Option(String)
fn(String) -> Option(String)The value of an option. If it was given more than once, the last one wins, as it does in most command line tools.
all(name: String) -> List(String)
fn(String) -> List(String)Every value given for an option, in order.
class ArgError
Why a command line could not be sorted.
class UnknownOption(option: String) is ArgError
An option nobody told the parser to expect, written as the user typed it.
option: String
String
fn(String) -> arglet.UnknownOptionclass MissingValue(option: String) is ArgError
A value option at the very end, with nothing after it.
option: String
String
fn(String) -> arglet.MissingValueclass UnexpectedValue(option: String) is ArgError
A switch that was handed a value, as in --verbose=yes.
option: String
String
fn(String) -> arglet.UnexpectedValuedescribe(problem: ArgError) -> String
fn(arglet.ArgError) -> StringA sentence for the person at the keyboard.
import arglet
assert arglet.describe(arglet.UnknownOption("--colour")) == "unknown option --colour"
parse(arguments: List(String), specs: List(Spec)) -> Result(Parsed, ArgError)
fn(List(String), List(arglet.Spec)) -> Result(arglet.Parsed, arglet.ArgError)Sort a list of words into flags, values and positionals.
--nameand-nare switches or options, as described byspecs.--name=value,--name value,-n valueand-nvaluegive a value.- Short switches can share a dash:
-qvis-q -v. --ends the options. Every word after it is a positional.-alone, and negative numbers such as-5, are positionals.
import arglet
let expected = [arglet.flag("quiet", "q", ""), arglet.value("name", "n", "")]
let assert Ok(args) = arglet.parse(["greet", "-q", "--name", "Ada"], expected)
assert args.positionals == ["greet"]
assert args.has("quiet")
assert args.get("name") == Some("Ada")
usage(specs: List(Spec)) -> String
fn(List(arglet.Spec)) -> StringOne line per expected option, for a --help screen.
import arglet
let expected = [arglet.flag("quiet", "q", "Say less")]
assert arglet.usage(expected) == " -q, --quiet Say less"