add check for short/long flags

This commit is contained in:
Daylin Morgan 2024-11-25 12:30:20 -06:00
parent 06085d7ebc
commit cbdad3454d
Signed by: daylin
GPG key ID: 950D13E9719334AD
3 changed files with 39 additions and 5 deletions

View file

@ -514,6 +514,34 @@ func pasrseCliAlias(cfg: var CliCfg, node: NimNode) =
cfg.alias.incl s cfg.alias.incl s
else: bad(n, "alias") else: bad(n, "alias")
func err(c: CliCfg, msg: string) =
## quit with error while generating cli
error "failed to generate " & c.name & " hwylcli: \n" & msg
func check(c: CliCfg) =
## verify the cli is valid
var
short: Table[char, CliFlag]
long: Table[string, CliFlag]
for f in c.flags:
if f.short != '\x00':
if f.short in short:
let conflict = short[f.short]
c.err "conflicting short flags for: " & f.name & " and " & conflict.name
# hwylCliImplError c, (
# "conflicting short flags for: " & f.name & " and " & conflict.name
# )
else:
short[f.short] = f
if f.long in long:
let conflict = long[f.long]
c.err "conflicting long flags for: " & f.name & " and " & conflict.name
else:
long[f.long] = f
func propagate(c: var CliCfg) = func propagate(c: var CliCfg) =
for child in c.subcommands.mitems: for child in c.subcommands.mitems:
# push the preSub to the lowest subcommand # push the preSub to the lowest subcommand
@ -524,7 +552,9 @@ func propagate(c: var CliCfg) =
child.pre = c.preSub child.pre = c.preSub
child.post = c.postSub child.post = c.postSub
child.inheritFrom(c) child.inheritFrom(c)
propagate(child) propagate child
check child
func parseCliHelp(c: var CliCfg, node: NimNode) = func parseCliHelp(c: var CliCfg, node: NimNode) =
## some possible DSL inputs: ## some possible DSL inputs:
@ -662,7 +692,11 @@ func subCmdsArray(cfg: CliCfg): NimNode =
result.add quote do: result.add quote do:
(`cmd`, `aliases`, `desc`) (`cmd`, `aliases`, `desc`)
proc hwylCliError*(msg: string | BbString) = # is this one necessary?
proc hwylCliError*(msg: BbString) =
quit $(bb("error ", "red") & msg)
proc hwylCliError*(msg: string) =
quit $(bb("error ", "red") & bb(msg)) quit $(bb("error ", "red") & bb(msg))
func defaultUsage(cfg: CliCfg): NimNode = func defaultUsage(cfg: CliCfg): NimNode =

View file

@ -12,8 +12,8 @@ hwylCli:
flags: flags:
[global] [global]
color: color:
T Color T Color
? "a color (red, green, blue)" ? "a color (red, green, blue)"
yes: yes:
T bool T bool
? "set flag to yes" ? "set flag to yes"

View file

@ -22,7 +22,7 @@
- [x] add support to either (lengthen commands) or provide an alias for a subcommand - [x] add support to either (lengthen commands) or provide an alias for a subcommand
- [x] add command aliases to hwylcli help with switch - [x] add command aliases to hwylcli help with switch
- [x] don't recreate "global"" variables in var section - [x] don't recreate "global"" variables in var section
- [ ] add flag overlap check before case statement generation (use `postParse` proc) - [x] add flag overlap check before case statement generation (use `postParse` proc)
- [x] add key-value flag support -> `--setting:a:off` - [x] add key-value flag support -> `--setting:a:off`
- [x] add defaultFlagType CliCfg setting - [x] add defaultFlagType CliCfg setting
- [x] add node to flagDef to override builtin `parse(p, varName)` - [x] add node to flagDef to override builtin `parse(p, varName)`