From 9e35d4ea128eeb892fdcda44c16c5a389e455d04 Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Wed, 13 Nov 2024 09:07:54 -0600 Subject: [PATCH] add subcommand aliases to help output --- src/hwylterm/hwylcli.nim | 16 ++++++++++++---- tests/example.nim | 8 ++++++++ todo.md | 2 +- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/hwylterm/hwylcli.nim b/src/hwylterm/hwylcli.nim index 5f80124..13060b0 100644 --- a/src/hwylterm/hwylcli.nim +++ b/src/hwylterm/hwylcli.nim @@ -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() - result.subcmds = - subcmds.mapIt((it.name, it.desc.firstLine)) + if Aliases in styles.settings: + result.subcmds = + 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)) diff --git a/tests/example.nim b/tests/example.nim index e9a7543..ab94693 100644 --- a/tests/example.nim +++ b/tests/example.nim @@ -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 diff --git a/todo.md b/todo.md index 6c9e2b6..f1a06bd 100644 --- a/todo.md +++ b/todo.md @@ -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