feature: separator text when displaying help

SEPARATOR:
-abbr, --name desc
-abbr, --name desc

---------
-abbr, --name desc
-abbr, --name desc
-abbr, --name desc
This commit is contained in:
jangko 2021-09-02 15:12:38 +07:00 committed by Zahary Karadjov
parent 0cd09d75c8
commit aa0ff5b0dc
3 changed files with 34 additions and 3 deletions

View File

@ -280,6 +280,24 @@ This field represents an argument to the program. If the program expects
multiple arguments, this pragma can be applied to multiple fields or to
a single `seq[T]` field depending on the desired behavior.
-----------------
```nim
template separator(v: string)* {.pragma.}
```
Using this pragma, a customizable separator text will be displayed just before
this field. E.g.:
```text
Network Options: # this is a separator
-a, --opt1 desc
-b, --opt2 desc
---------------- # this is a separator too
-c, --opt3 desc
```
## Configuration field types
The `confutils/defs` module provides a number of types frequently used

View File

@ -40,6 +40,7 @@ type
OptInfo = ref object
name, abbr, desc, typename: string
separator: string
idx: int
isHidden: bool
hasDefault: bool
@ -293,6 +294,10 @@ proc describeOptions(help: var string,
opt.kind == Discriminator or
opt.isHidden: continue
if opt.separator.len > 0:
helpOutput opt.separator
helpOutput "\p"
# Indent all command-line switches
helpOutput " "
@ -684,6 +689,11 @@ proc findPath(parent, node: CmdInfo): seq[CmdInfo] =
doAssert validPath(result, parent, node)
result.add parent
func toText(n: NimNode): string =
if n == nil: ""
elif n.kind in {nnkStrLit..nnkTripleStrLit}: n.strVal
else: repr(n)
proc cmdInfoFromType(T: NimNode): CmdInfo =
result = CmdInfo()
@ -699,9 +709,10 @@ proc cmdInfoFromType(T: NimNode): CmdInfo =
defaultValueDesc = field.readPragma"defaultValueDesc"
defaultInHelp = if defaultValueDesc != nil: defaultValueDesc
else: defaultValue
defaultInHelpText = if defaultInHelp == nil: ""
elif defaultInHelp.kind in {nnkStrLit..nnkTripleStrLit}: defaultInHelp.strVal
else: repr(defaultInHelp)
defaultInHelpText = toText(defaultInHelp)
separator = field.readPragma"separator"
separatorText = toText(separator)
isHidden = field.readPragma("hidden") != nil
abbr = field.readPragma"abbr"
name = field.readPragma"name"
@ -713,6 +724,7 @@ proc cmdInfoFromType(T: NimNode): CmdInfo =
var opt = OptInfo(kind: optKind,
idx: fieldIdx,
name: $field.name,
separator: separatorText,
isHidden: isHidden,
hasDefault: defaultValue != nil,
defaultInHelpText: defaultInHelpText,

View File

@ -45,6 +45,7 @@ template `$`*(x: SomeDistinctString): string =
template desc*(v: string) {.pragma.}
template name*(v: string) {.pragma.}
template abbr*(v: string) {.pragma.}
template separator*(v: string) {.pragma.}
template defaultValue*(v: untyped) {.pragma.}
template defaultValueDesc*(v: string) {.pragma.}
template required* {.pragma.}