elvish_config/lib/moi/util/option.elv

76 lines
1.5 KiB
Plaintext
Raw Normal View History

2020-03-13 14:03:36 +00:00
use re
use str
use ./list
fn is-moptions [o]{ re:match '^\-\w{2,}$' $o }
fn is-soption [o]{ re:match '^\-\w$' $o }
fn is-loption [o]{ re:match '^\-\-\w+(=.*)?$' $o }
fn is-option [o]{ or (is-soption $o) (is-loption $o) (is-moptions $o) }
fn -splitm [o]{ each [e]{ put -$e } $o[1:] }
fn -splitl [o]{
if (str:contains $o '=') {
local:opt @local:value = (splits '=' $o)
put $opt
joins '=' $value
} else {
put $o
}
}
fn split [o]{
if (is-moptions $o) {
-splitm $o
} elif (is-loption $o) {
-splitl $o
} else {
put $o
}
}
fn -joins [opt]{
local:o = (each [o]{ put $o[1:] } $opt | joins '')
if (not-eq $o '') { put -$o }
}
fn join [@opt]{
local:opt = (list:-p $@opt)
local:cb = [o]{ or (is-soption $o) (is-moptions $o) }
local:idx = [(list:filter-index-not $cb $opt)]
local:i0 = 0
each [i]{
-joins $opt[{$i0}:$i]
put $opt[$i]
i0 = (+ $i 1)
} $idx
-joins $opt[{$i0}:]
}
fn expand [@argv]{ each $split~ (list:-p $@argv) }
fn map [@argv]{
use ./map
argv = [(expand $@argv)]
local:result = [&]
local:i local:c = 0 (count $argv)
while (< $i $c) {
local:o = $argv[$i]
if (is-option $o) {
local:k local:v = $o []
if (< $i (- $c 1)) {
local:e = $argv[(+ $i 1)]
if (not (is-option $e)) {
v = [$@v $e]
i = (+ $i 1)
}
}
result = (map:add $result $k $@v)
} else {
result = (map:add $result '' [$o])
}
i = (+ $i 1)
}
put $result
}