cli.oak
← Standard library
See on GitHub ↗
{
default: default
slice: slice
join: join
each: each
} := import('std')
{
startsWith?: startsWith?
} := import('str')
fn _maybeOpt(part) if {
part |> startsWith?('--') -> part |> slice(2)
part |> startsWith?('-') -> part |> slice(1)
_ -> ?
}
fn parseArgv(argv) {
if _maybeOpt(default(argv.2, '')) != ? -> {
argv <- slice(argv, 0, 2) |> join([?]) |> join(slice(argv, 2))
}
opts := {}
args := []
lastOpt := ?
onlyPositional? := false
argv |> slice(3) |> with each() fn(part) if {
part = ? -> ?
part = '--' -> onlyPositional? <- true
onlyPositional? -> args << part
_ -> if [lastOpt, opt := _maybeOpt(part)] {
[?, ?] -> args << part
[_, ?] -> {
opts.(lastOpt) := part
lastOpt <- ?
}
[?, _] -> lastOpt <- opt
_ -> {
opts.(lastOpt) := true
lastOpt <- opt
}
}
}
if lastOpt != ? -> opts.(lastOpt) := true
{
exe: argv.0
main: argv.1
verb: argv.2
opts: opts
args: args
}
}
fn parse() parseArgv(args())