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
2021-12-11 21:09:02 +00:00
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) }
2020-03-13 14:03:36 +00:00
2021-12-11 21:09:02 +00:00
fn -splitm {|o| each {|e| put -$e } $o[1:] }
2020-03-13 14:03:36 +00:00
2021-12-11 21:09:02 +00:00
fn -splitl {|o|
2020-03-13 14:03:36 +00:00
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
}
}
2021-12-11 21:09:02 +00:00
fn split {|o|
2020-03-13 14:03:36 +00:00
if (is-moptions $o) {
-splitm $o
} elif (is-loption $o) {
-splitl $o
} else {
put $o
}
}
2021-12-11 21:09:02 +00:00
fn -joins {|opt|
var o = (each {|o| put $o[1:] } $opt | str:join '')
2020-03-13 14:03:36 +00:00
if (not-eq $o '') { put -$o }
}
2021-12-11 21:09:02 +00:00
fn join {|@opt|
2021-08-27 20:06:49 +00:00
var opt = (list:-p $@opt)
2021-12-11 21:09:02 +00:00
var cb = {|o| or (is-soption $o) (is-moptions $o) }
2021-08-27 20:06:49 +00:00
var idx = [(list:filter-index-not $cb $opt)]
var i0 = 0
2021-12-11 21:09:02 +00:00
each {|i|
2020-03-13 14:03:36 +00:00
-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}:]
}
2021-12-11 21:09:02 +00:00
fn expand {|@argv| each $split~ (list:-p $@argv) }
2020-03-13 14:03:36 +00:00
2021-12-11 21:09:02 +00:00
fn map {|@argv|
2020-03-13 14:03:36 +00:00
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
}