elvish_config/lib/moi/util/option.elv

75 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 '=') {
2021-08-27 20:06:49 +00:00
var opt @value = (str:split '=' $o)
2020-03-13 14:03:36 +00:00
put $opt
2020-07-22 09:24:43 +00:00
str:join '=' $value
2020-03-13 14:03:36 +00:00
} else {
put $o
}
}
fn split [o]{
if (is-moptions $o) {
-splitm $o
} elif (is-loption $o) {
-splitl $o
} else {
put $o
}
}
fn -joins [opt]{
2021-08-27 20:06:49 +00:00
var o = (each [o]{ put $o[1:] } $opt | str:join '')
2020-03-13 14:03:36 +00:00
if (not-eq $o '') { put -$o }
}
fn join [@opt]{
2021-08-27 20:06:49 +00:00
var opt = (list:-p $@opt)
var cb = [o]{ or (is-soption $o) (is-moptions $o) }
var idx = [(list:filter-index-not $cb $opt)]
var i0 = 0
2020-03-13 14:03:36 +00:00
each [i]{
-joins $opt[{$i0}:$i]
put $opt[$i]
2021-10-13 18:49:48 +00:00
set i0 = (+ $i 1)
2020-03-13 14:03:36 +00:00
} $idx
-joins $opt[{$i0}:]
}
fn expand [@argv]{ each $split~ (list:-p $@argv) }
fn map [@argv]{
use ./map
2021-10-13 18:49:48 +00:00
set argv = [(expand $@argv)]
2021-08-27 20:06:49 +00:00
var result i c = [&] 0 (count $argv)
2020-03-13 14:03:36 +00:00
while (< $i $c) {
2021-08-27 20:06:49 +00:00
var o = $argv[$i]
2020-03-13 14:03:36 +00:00
if (is-option $o) {
2021-08-27 20:06:49 +00:00
var k v = $o []
2020-03-13 14:03:36 +00:00
if (< $i (- $c 1)) {
2021-08-27 20:06:49 +00:00
var e = $argv[(+ $i 1)]
2020-03-13 14:03:36 +00:00
if (not (is-option $e)) {
2021-10-13 18:49:48 +00:00
set v = [$@v $e]
set i = (+ $i 1)
2020-03-13 14:03:36 +00:00
}
}
2021-10-13 18:49:48 +00:00
set result = (map:add $result $k $@v)
2020-03-13 14:03:36 +00:00
} else {
2021-10-13 18:49:48 +00:00
set result = (map:add $result '' [$o])
2020-03-13 14:03:36 +00:00
}
2021-10-13 18:49:48 +00:00
set i = (+ $i 1)
2020-03-13 14:03:36 +00:00
}
put $result
}