mirror of
https://github.com/daylinmorgan/hwylterm.git
synced 2025-02-23 09:45:50 -06:00
infer short flags
This commit is contained in:
parent
fe3c7c141f
commit
17a44c17f7
3 changed files with 51 additions and 6 deletions
|
@ -191,11 +191,12 @@ type
|
||||||
ShowHelp, ## If cmdline empty show help
|
ShowHelp, ## If cmdline empty show help
|
||||||
NoNormalize, ## Don't normalize flags and commands
|
NoNormalize, ## Don't normalize flags and commands
|
||||||
NoPositional, ## Raise error if any remaing positional arguments DEPRECATED
|
NoPositional, ## Raise error if any remaing positional arguments DEPRECATED
|
||||||
HideDefault ## Don't show default values
|
HideDefault, ## Don't show default values
|
||||||
# ExactArgs, ## Raise error if missing positional argument
|
InferShort ## Autodefine short flags
|
||||||
|
|
||||||
CliFlagSetting* = enum
|
CliFlagSetting* = enum
|
||||||
HideDefault ## Don't show default values
|
HideDefault, ## Don't show default values
|
||||||
|
NoShort ## Counter option to Parent's InferShort
|
||||||
|
|
||||||
BuiltinFlag = object
|
BuiltinFlag = object
|
||||||
name*: string
|
name*: string
|
||||||
|
@ -269,6 +270,9 @@ template `<<<`(n: NimNode) {.used.} =
|
||||||
## for debugging macros
|
## for debugging macros
|
||||||
<<< treeRepr n
|
<<< treeRepr n
|
||||||
|
|
||||||
|
template `<<<`(n: untyped) {.used.} =
|
||||||
|
debugEcho n, "|||", instantiationInfo().line
|
||||||
|
|
||||||
func `<<<`(f: CliFlag) {.used.}=
|
func `<<<`(f: CliFlag) {.used.}=
|
||||||
var s: string
|
var s: string
|
||||||
let fields = [
|
let fields = [
|
||||||
|
@ -398,6 +402,19 @@ func parseCliFlag(n: NimNode): CliFlag =
|
||||||
if result.ident == nil:
|
if result.ident == nil:
|
||||||
result.ident = result.name.ident
|
result.ident = result.name.ident
|
||||||
|
|
||||||
|
func inferShortFlags(cfg: var CliCfg) =
|
||||||
|
## supplement existing short flags based on initial characters of long flags
|
||||||
|
let taken = cfg.flags.mapIt(it.short).toHashSet() - toHashSet(['\x00'])
|
||||||
|
var candidates = cfg.flags.mapIt(it.long[0]).toHashSet() - taken
|
||||||
|
for f in cfg.flags.mitems:
|
||||||
|
|
||||||
|
if f.short != '\x00' or NoShort in f.settings: continue
|
||||||
|
let c = f.long[0]
|
||||||
|
if c in candidates:
|
||||||
|
f.short = c
|
||||||
|
candidates.excl c
|
||||||
|
|
||||||
|
|
||||||
func postParse(cfg: var CliCfg) =
|
func postParse(cfg: var CliCfg) =
|
||||||
if cfg.name == "":
|
if cfg.name == "":
|
||||||
error "missing required option: name"
|
error "missing required option: name"
|
||||||
|
@ -417,6 +434,9 @@ func postParse(cfg: var CliCfg) =
|
||||||
if count > 1:
|
if count > 1:
|
||||||
cfg.err "more than one positional argument is variadic"
|
cfg.err "more than one positional argument is variadic"
|
||||||
|
|
||||||
|
if InferShort in cfg.settings:
|
||||||
|
inferShortFlags cfg
|
||||||
|
|
||||||
func parseCliFlags(cfg: var CliCfg, node: NimNode) =
|
func parseCliFlags(cfg: var CliCfg, node: NimNode) =
|
||||||
var group: string
|
var group: string
|
||||||
expectKind node, nnkStmtList
|
expectKind node, nnkStmtList
|
||||||
|
@ -613,9 +633,6 @@ func postPropagateCheck(c: CliCfg) =
|
||||||
if f.short in short:
|
if f.short in short:
|
||||||
let conflict = short[f.short]
|
let conflict = short[f.short]
|
||||||
c.err "conflicting short flags for: " & f.name & " and " & conflict.name
|
c.err "conflicting short flags for: " & f.name & " and " & conflict.name
|
||||||
# hwylCliImplError c, (
|
|
||||||
# "conflicting short flags for: " & f.name & " and " & conflict.name
|
|
||||||
# )
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
short[f.short] = f
|
short[f.short] = f
|
||||||
|
|
25
tests/cli/clis/inferShort.nim
Normal file
25
tests/cli/clis/inferShort.nim
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
import std/strformat
|
||||||
|
import hwylterm, hwylterm/hwylcli
|
||||||
|
|
||||||
|
hwylCli:
|
||||||
|
name "inferred short flags"
|
||||||
|
settings InferShort
|
||||||
|
flags:
|
||||||
|
input:
|
||||||
|
T string
|
||||||
|
? "the input var"
|
||||||
|
output:
|
||||||
|
T string
|
||||||
|
? "the output var"
|
||||||
|
count:
|
||||||
|
T int
|
||||||
|
? "a number"
|
||||||
|
- n
|
||||||
|
nancy:
|
||||||
|
? "needed a flag that starts with n :)"
|
||||||
|
ignore:
|
||||||
|
S NoShort
|
||||||
|
? "a flag to not infer"
|
||||||
|
run:
|
||||||
|
echo fmt"{input=}, {output=}, {count=}, {nancy=}, {ignore=}"
|
||||||
|
|
|
@ -114,3 +114,6 @@ inside sub c
|
||||||
failWithArgs(
|
failWithArgs(
|
||||||
"errorOverride", "--flag","override the default error\nerror unknown flag: flag"
|
"errorOverride", "--flag","override the default error\nerror unknown flag: flag"
|
||||||
)
|
)
|
||||||
|
okWithArgs(
|
||||||
|
"inferShort", "-i input -o output","""input=input, output=output, count=0, nancy=false, ignore=false"""
|
||||||
|
)
|
||||||
|
|
Loading…
Add table
Reference in a new issue