mirror of
https://github.com/RGBCube/nu_scripts
synced 2025-08-01 06:37:46 +00:00
rename cmd parse
to argx
to improve importing (#671)
- imports `*` are unnecessary - shorter names for easier reference in task scripts - `cmd` is too common and may cause potential conflicts Co-authored-by: nash <nash@iffy.me>
This commit is contained in:
parent
09d6c7e437
commit
9d02fb1ef8
3 changed files with 16 additions and 16 deletions
127
modules/argx/argx.nu
Normal file
127
modules/argx/argx.nu
Normal file
|
@ -0,0 +1,127 @@
|
|||
export def get-sign [cmd] {
|
||||
let x = (scope commands | where name == $cmd).signatures?.0?.any?
|
||||
mut s = []
|
||||
mut n = {}
|
||||
mut p = []
|
||||
mut pr = []
|
||||
mut r = []
|
||||
for it in $x {
|
||||
if $it.parameter_type == 'switch' {
|
||||
if not ($it.short_flag | is-empty) {
|
||||
$s ++= $it.short_flag
|
||||
}
|
||||
if not ($it.parameter_name | is-empty) {
|
||||
$s ++= $it.parameter_name
|
||||
}
|
||||
} else if $it.parameter_type == 'named' {
|
||||
if ($it.parameter_name | is-empty) {
|
||||
$n = ($n | upsert $it.short_flag $it.short_flag)
|
||||
} else if ($it.short_flag | is-empty) {
|
||||
$n = ($n | upsert $it.parameter_name $it.parameter_name)
|
||||
} else {
|
||||
$n = ($n | upsert $it.short_flag $it.parameter_name)
|
||||
}
|
||||
} else if $it.parameter_type == 'positional' {
|
||||
if $it.is_optional == false {
|
||||
$p ++= $it.parameter_name
|
||||
} else {
|
||||
$pr ++= $it.parameter_name
|
||||
}
|
||||
} else if $it.parameter_type == 'rest' {
|
||||
$r ++= $it.parameter_name
|
||||
}
|
||||
}
|
||||
{ switch: $s, name: $n, positional: ($p ++ $pr), rest: $r }
|
||||
}
|
||||
|
||||
# "test -h [123 (3213 3)] 123 `a sdf` --cd --ef sadf -g" | token
|
||||
export def token [] {
|
||||
let s = ($in | split row '' | range 1..-2)
|
||||
let s = if ($s | last) == ' ' { $s } else { $s | append ' ' }
|
||||
mut par = []
|
||||
mut res = []
|
||||
mut cur = ''
|
||||
mut esc = false
|
||||
for c in $s {
|
||||
if $c == '\' {
|
||||
$esc = true
|
||||
} else {
|
||||
if $esc {
|
||||
$cur ++= $c
|
||||
$esc = false
|
||||
} else {
|
||||
if $c == ' ' and ($par | length) == 0 {
|
||||
$res ++= [$cur]
|
||||
$cur = ''
|
||||
} else {
|
||||
if $c in ['{' '[' '('] {
|
||||
$par ++= $c
|
||||
}
|
||||
if $c in ['}' ']' ')'] {
|
||||
$par = ($par | range ..-2)
|
||||
}
|
||||
if $c in ['"' "'" '`'] {
|
||||
if ($par | length) > 0 and ($par | last) == $c {
|
||||
$par = ($par | range ..-2)
|
||||
} else {
|
||||
$par ++= $c
|
||||
}
|
||||
}
|
||||
$cur ++= $c
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
return $res
|
||||
}
|
||||
|
||||
export def parse [] {
|
||||
let token = ($in | token)
|
||||
let sign = (get-sign $token.0)
|
||||
mut sw = ''
|
||||
mut pos = []
|
||||
mut opt = {}
|
||||
for c in $token {
|
||||
if ($sw | is-empty) {
|
||||
if ($c | str starts-with '-') {
|
||||
let c = if ($c | str substring 1..2) != '-' {
|
||||
let k = ($c | str substring 1..)
|
||||
if $k in $sign.name {
|
||||
$'($sign.name | get $k)'
|
||||
} else {
|
||||
$k
|
||||
}
|
||||
} else {
|
||||
$c | str substring 2..
|
||||
}
|
||||
if $c in $sign.switch {
|
||||
$opt = ($opt | upsert $c true)
|
||||
} else {
|
||||
$sw = $c
|
||||
}
|
||||
} else {
|
||||
$pos ++= [$c]
|
||||
}
|
||||
} else {
|
||||
$opt = ($opt | upsert $sw $c)
|
||||
$sw = ''
|
||||
}
|
||||
}
|
||||
$opt._args = $pos
|
||||
let p = $pos | range 1..($sign.positional | length)
|
||||
let rest = $pos | range (($sign.positional | length) + 1)..-1
|
||||
$opt._pos = ( $p | enumerate
|
||||
| reduce -f {} {|it, acc|
|
||||
$acc | upsert ($sign.positional | get $it.index) $it.item
|
||||
} )
|
||||
if ($sign.rest | length) > 0 {
|
||||
$opt._pos = ($opt._pos | upsert $sign.rest.0 $rest)
|
||||
}
|
||||
$opt
|
||||
}
|
||||
|
||||
# def test [a b x? ...y --cd(-c) --ef(-e):string -g -h:int --ij --lm:bool] {}
|
||||
# (scope commands | where name == test).signatures?.0?.any?
|
||||
# get-sign test | to yaml
|
||||
# "test -h 111 123 'asdf' --cd --ef sadf -g -h 222" | cmd parse | to yaml
|
Loading…
Add table
Add a link
Reference in a new issue