diff --git a/src/hwylterm/hwylcli.nim b/src/hwylterm/hwylcli.nim index c40fba6..da84632 100644 --- a/src/hwylterm/hwylcli.nim +++ b/src/hwylterm/hwylcli.nim @@ -514,6 +514,34 @@ func pasrseCliAlias(cfg: var CliCfg, node: NimNode) = cfg.alias.incl s 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) = for child in c.subcommands.mitems: # push the preSub to the lowest subcommand @@ -524,7 +552,9 @@ func propagate(c: var CliCfg) = child.pre = c.preSub child.post = c.postSub child.inheritFrom(c) - propagate(child) + propagate child + check child + func parseCliHelp(c: var CliCfg, node: NimNode) = ## some possible DSL inputs: @@ -662,7 +692,11 @@ func subCmdsArray(cfg: CliCfg): NimNode = result.add quote do: (`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)) func defaultUsage(cfg: CliCfg): NimNode = diff --git a/tests/example.nim b/tests/example.nim index f5456dd..0d6dfcb 100644 --- a/tests/example.nim +++ b/tests/example.nim @@ -12,8 +12,8 @@ hwylCli: flags: [global] color: - T Color - ? "a color (red, green, blue)" + T Color + ? "a color (red, green, blue)" yes: T bool ? "set flag to yes" diff --git a/todo.md b/todo.md index 96e272c..b8f44a7 100644 --- a/todo.md +++ b/todo.md @@ -22,7 +22,7 @@ - [x] add support to either (lengthen commands) or provide an alias for a subcommand - [x] add command aliases to hwylcli help with switch - [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 defaultFlagType CliCfg setting - [x] add node to flagDef to override builtin `parse(p, varName)`