mirror of
https://github.com/daylinmorgan/hwylterm.git
synced 2024-12-21 18:50:44 -06:00
add key val
This commit is contained in:
parent
ab00305c92
commit
938131c6cd
3 changed files with 31 additions and 9 deletions
|
@ -138,6 +138,10 @@ proc bb*(cli: HwylCliHelp): BbString =
|
|||
type
|
||||
Count* = object ## Count type for an incrementing flag
|
||||
val*: int
|
||||
KV*[X,Y] = object ## basic key value type
|
||||
key*: X
|
||||
val*: Y
|
||||
KVString* = KV[string, string]
|
||||
|
||||
type
|
||||
CliSetting* = enum
|
||||
|
@ -752,6 +756,29 @@ proc parse*(p: OptParser, target: var Count) =
|
|||
else:
|
||||
inc target.val
|
||||
|
||||
proc extractKey(p: var OptParser): string =
|
||||
var i: int
|
||||
for c in p.val:
|
||||
if c notin {'=',':'}: inc i
|
||||
else: break
|
||||
if i == p.val.len:
|
||||
hwylCliError(
|
||||
"failed to parse key val flag" &
|
||||
"\nkey: " & p.key.bb("bold") &
|
||||
"\nval: " & p.val.bb("bold") &
|
||||
"\ndid you include a separator (= or :)?"
|
||||
)
|
||||
else:
|
||||
result = p.val[0..<i]
|
||||
p.key = p.key & ":" & result
|
||||
p.val = p.val[(i+1) .. ^1]
|
||||
|
||||
proc parse*[T](p: var OptParser, target: var KV[string, T]) =
|
||||
checkVal p
|
||||
let key = extractKey(p)
|
||||
target.key = key
|
||||
parse(p, target.val)
|
||||
|
||||
func shortLongCaseStmt(cfg: CliCfg, printHelpName: NimNode, version: NimNode): NimNode =
|
||||
var caseStmt = nnkCaseStmt.newTree()
|
||||
if NoNormalize notin cfg.settings:
|
||||
|
|
4
test.nim
4
test.nim
|
@ -1,4 +0,0 @@
|
|||
import std/macros
|
||||
|
||||
dumpAstGen:
|
||||
incl(flagSet, name)
|
|
@ -6,7 +6,6 @@ type
|
|||
Color = enum
|
||||
red, blue, green
|
||||
|
||||
|
||||
hwylCli:
|
||||
name "example"
|
||||
V "0.1.0"
|
||||
|
@ -72,14 +71,14 @@ hwylCli:
|
|||
"""
|
||||
flags:
|
||||
^something
|
||||
auto:
|
||||
- a
|
||||
? "some help"
|
||||
thing:
|
||||
T KV[string, Color]
|
||||
? "some key value string"
|
||||
b:
|
||||
T seq[float]
|
||||
? "multiple floats"
|
||||
h "this will override the builtin 'h' for help"
|
||||
run:
|
||||
echo "hello from `example b` command"
|
||||
echo fmt"{auto=}, {b=}"
|
||||
echo fmt"{thing=}, {b=}"
|
||||
|
||||
|
|
Loading…
Reference in a new issue