add subcommand aliases to help output

This commit is contained in:
Daylin Morgan 2024-11-13 09:07:54 -06:00
parent 88572a6ce4
commit 9e35d4ea12
Signed by: daylin
GPG key ID: 950D13E9719334AD
3 changed files with 21 additions and 5 deletions

View file

@ -14,13 +14,16 @@ type
HwylFlagHelp* = tuple
short, long, description: string
HwylSubCmdHelp* = tuple
name, desc: string
name, aliases, desc: string
HwylCliStyleSetting = enum
Aliases
HwylCliStyles* = object
header* = "bold cyan"
flagShort* = "yellow"
flagLong* = "magenta"
flagDesc* = ""
cmd* = "bold"
settings*: set[HwylCliStyleSetting] = {Aliases}
HwylCliHelp* = object
usage*: string
desc*: string
@ -41,8 +44,12 @@ func newHwylCliHelp*(
styles = HwylCliStyles()
): HwylCliHelp =
result.desc = dedent(desc).strip()
if Aliases in styles.settings:
result.subcmds =
subcmds.mapIt((it.name, it.desc.firstLine))
subcmds.mapIt((it.name & " " & it.aliases, it.aliases, it.desc.firstLine))
else:
result.subcmds =
subcmds.mapIt((it.name, it.aliases, it.desc.firstLine))
result.usage = dedent(usage).strip()
result.flags = @flags
result.styles = styles
@ -537,9 +544,10 @@ func subCmdsArray(cfg: CliCfg): NimNode =
result = newTree(nnkBracket)
for s in cfg.subcommands:
let cmd = newLit(s.subName)
let aliases = newLit(s.alias.mapIt("($1)" % [it]).join(" "))
let desc = s.desc or newLit("")
result.add quote do:
(`cmd`, `desc`)
(`cmd`, `aliases`, `desc`)
proc hwylCliError*(msg: string | BbString) =
quit $(bb("error ", "red") & bb(msg))

View file

@ -46,6 +46,14 @@ hwylCli:
? "a count flag"
- v
^[shared]
subcommands:
[subsub]
... "another level down subcommand"
flags:
^color
run:
echo fmt"{color=}"
run:
echo "hello from `example one` command!"
echo args

View file

@ -20,7 +20,7 @@
- [ ] add nargs to CliCfg
- [x] add support for inheriting a single flag from parent (even from a "group")
- [x] add support to either (lengthen commands) or provide an alias for a subcommand
- [ ] add command aliases to hwylcli help with switch
- [x] add command aliases to hwylcli help with switch
## features