-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cwdhist: can place the path anywhere in the buffer (#669)
- git, kubernetes: upgrade cmd parse --------- Co-authored-by: nash <nash@iffy.me>
- Loading branch information
Showing
5 changed files
with
162 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | cmd token | ||
export def "cmd 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 "cmd parse" [] { | ||
let token = ($in | cmd 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.