mirror of
https://github.com/daylinmorgan/hwylterm.git
synced 2024-12-21 18:50:44 -06:00
get key val from parser object
This commit is contained in:
parent
c80c0d3db9
commit
fdec798b30
1 changed files with 27 additions and 29 deletions
|
@ -671,26 +671,26 @@ func generateCliHelpProc(cfg: CliCfg, printHelpName: NimNode): NimNode =
|
||||||
styles = `styles`,
|
styles = `styles`,
|
||||||
)))
|
)))
|
||||||
|
|
||||||
proc preParseCheck(key: string, val: string) =
|
proc checkVal(p: OptParser) =
|
||||||
if val == "":
|
if p.val == "":
|
||||||
hwylCliError(
|
hwylCliError(
|
||||||
"expected value for flag: [b]" & key
|
"expected value for flag: [b]" & p.key
|
||||||
)
|
)
|
||||||
|
|
||||||
proc parse*(p: OptParser, key: string, val: string, target: var bool) =
|
proc parse*(p: OptParser, target: var bool) =
|
||||||
target = true
|
target = true
|
||||||
|
|
||||||
proc parse*(p: OptParser, key: string, val: string, target: var string) =
|
proc parse*(p: OptParser, target: var string) =
|
||||||
preParseCheck(key, val)
|
checkVal p
|
||||||
target = val
|
target = p.val
|
||||||
|
|
||||||
proc parse*(p: OptParser, key: string, val: string, target: var int) =
|
proc parse*(p: OptParser, target: var int) =
|
||||||
preParseCheck(key, val)
|
checkVal p
|
||||||
try:
|
try:
|
||||||
target = parseInt(val)
|
target = parseInt(p.val)
|
||||||
except:
|
except:
|
||||||
hwylCliError(
|
hwylCliError(
|
||||||
"failed to parse value for [b]" & key & "[/] as integer: [b]" & val
|
"failed to parse value for [b]" & p.key & "[/] as integer: [b]" & p.val
|
||||||
)
|
)
|
||||||
|
|
||||||
macro enumNames(a: typed): untyped =
|
macro enumNames(a: typed): untyped =
|
||||||
|
@ -700,36 +700,36 @@ macro enumNames(a: typed): untyped =
|
||||||
assert ai.kind == nnkSym
|
assert ai.kind == nnkSym
|
||||||
result.add newLit ai.strVal
|
result.add newLit ai.strVal
|
||||||
|
|
||||||
proc parse*[E: enum](p: OptParser, key: string, val: string, target: var E) =
|
proc parse*[E: enum](p: OptParser, target: var E) =
|
||||||
preParseCheck(key, val)
|
checkVal p
|
||||||
try:
|
try:
|
||||||
target = parseEnum[E](val)
|
target = parseEnum[E](p.val)
|
||||||
except:
|
except:
|
||||||
let choices = enumNames(E).join(",")
|
let choices = enumNames(E).join(",")
|
||||||
hwylCliError(
|
hwylCliError(
|
||||||
"failed to parse value for [b]" & key & "[/] as enum: [b]" & val & "[/] expected one of: " & choices
|
"failed to parse value for [b]" & p.key & "[/] as enum: [b]" & p.val & "[/] expected one of: " & choices
|
||||||
)
|
)
|
||||||
|
|
||||||
proc parse*(p: OptParser, key: string, val: string, target: var float) =
|
proc parse*(p: OptParser, target: var float) =
|
||||||
preParseCheck(key, val)
|
checkVal p
|
||||||
try:
|
try:
|
||||||
target = parseFloat(val)
|
target = parseFloat(p.val)
|
||||||
except:
|
except:
|
||||||
hwylCliError(
|
hwylCliError(
|
||||||
"failed to parse value for [b]" & key & "[/] as float: [b]" & val
|
"failed to parse value for [b]" & p.key & "[/] as float: [b]" & p.val
|
||||||
)
|
)
|
||||||
|
|
||||||
proc parse*[T](p: OptParser, key: string, val: string, target: var seq[T]) =
|
proc parse*[T](p: OptParser, target: var seq[T]) =
|
||||||
preParseCheck(key, val)
|
checkVal p
|
||||||
var parsed: T
|
var parsed: T
|
||||||
parse(p, key, val, parsed)
|
parse(p, parsed)
|
||||||
target.add parsed
|
target.add parsed
|
||||||
|
|
||||||
proc parse*(p: OptParser, key: string, val: string, target: var Count) =
|
proc parse*(p: OptParser, target: var Count) =
|
||||||
# if value set to that otherwise increment
|
# if value set to that otherwise increment
|
||||||
if val != "":
|
if p.val != "":
|
||||||
var num: int
|
var num: int
|
||||||
parse(p, key, val, num)
|
parse(p, num)
|
||||||
target.val = num
|
target.val = num
|
||||||
else:
|
else:
|
||||||
inc target.val
|
inc target.val
|
||||||
|
@ -753,7 +753,7 @@ func shortLongCaseStmt(cfg: CliCfg, printHelpName: NimNode, version: NimNode): N
|
||||||
# add flags
|
# add flags
|
||||||
for f in cfg.flags:
|
for f in cfg.flags:
|
||||||
var branch = nnkOfBranch.newTree()
|
var branch = nnkOfBranch.newTree()
|
||||||
if f.long != "":
|
if f.long != "":
|
||||||
branch.add newLit(
|
branch.add newLit(
|
||||||
if NoNormalize notin cfg.settings: optionNormalize(f.long)
|
if NoNormalize notin cfg.settings: optionNormalize(f.long)
|
||||||
else: f.long
|
else: f.long
|
||||||
|
@ -763,7 +763,7 @@ func shortLongCaseStmt(cfg: CliCfg, printHelpName: NimNode, version: NimNode): N
|
||||||
let name = newLit(f.name)
|
let name = newLit(f.name)
|
||||||
branch.add quote do:
|
branch.add quote do:
|
||||||
flagSet.incl `name`
|
flagSet.incl `name`
|
||||||
parse(p, key, val, `varName`)
|
parse(p, `varName`)
|
||||||
|
|
||||||
caseStmt.add branch
|
caseStmt.add branch
|
||||||
|
|
||||||
|
@ -863,8 +863,6 @@ func genSubcommandHandler(cfg: CliCfg): NimNode =
|
||||||
|
|
||||||
result.add subCommandCase
|
result.add subCommandCase
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
func hwylCliImpl(cfg: CliCfg): NimNode =
|
func hwylCliImpl(cfg: CliCfg): NimNode =
|
||||||
let
|
let
|
||||||
version = cfg.version or newLit("")
|
version = cfg.version or newLit("")
|
||||||
|
|
Loading…
Reference in a new issue