add key val

This commit is contained in:
Daylin Morgan 2024-11-17 12:24:39 -06:00
parent ab00305c92
commit 938131c6cd
Signed by: daylin
GPG key ID: 950D13E9719334AD
3 changed files with 31 additions and 9 deletions

View file

@ -138,6 +138,10 @@ proc bb*(cli: HwylCliHelp): BbString =
type type
Count* = object ## Count type for an incrementing flag Count* = object ## Count type for an incrementing flag
val*: int val*: int
KV*[X,Y] = object ## basic key value type
key*: X
val*: Y
KVString* = KV[string, string]
type type
CliSetting* = enum CliSetting* = enum
@ -752,6 +756,29 @@ proc parse*(p: OptParser, target: var Count) =
else: else:
inc target.val 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 = func shortLongCaseStmt(cfg: CliCfg, printHelpName: NimNode, version: NimNode): NimNode =
var caseStmt = nnkCaseStmt.newTree() var caseStmt = nnkCaseStmt.newTree()
if NoNormalize notin cfg.settings: if NoNormalize notin cfg.settings:

View file

@ -1,4 +0,0 @@
import std/macros
dumpAstGen:
incl(flagSet, name)

View file

@ -6,7 +6,6 @@ type
Color = enum Color = enum
red, blue, green red, blue, green
hwylCli: hwylCli:
name "example" name "example"
V "0.1.0" V "0.1.0"
@ -72,14 +71,14 @@ hwylCli:
""" """
flags: flags:
^something ^something
auto: thing:
- a T KV[string, Color]
? "some help" ? "some key value string"
b: b:
T seq[float] T seq[float]
? "multiple floats" ? "multiple floats"
h "this will override the builtin 'h' for help" h "this will override the builtin 'h' for help"
run: run:
echo "hello from `example b` command" echo "hello from `example b` command"
echo fmt"{auto=}, {b=}" echo fmt"{thing=}, {b=}"