Fix #8
This commit is contained in:
parent
c9d823f217
commit
31c9f9ae38
|
@ -461,7 +461,7 @@ proc parseCmdArg*(T: type SomeFloat, p: TaintedString): T =
|
||||||
result = parseFloat(p)
|
result = parseFloat(p)
|
||||||
|
|
||||||
proc parseCmdArg*(T: type bool, p: TaintedString): T =
|
proc parseCmdArg*(T: type bool, p: TaintedString): T =
|
||||||
result = parseBool(p)
|
result = p.len == 0 or parseBool(p)
|
||||||
|
|
||||||
proc parseCmdArg*(T: type enum, s: TaintedString): T =
|
proc parseCmdArg*(T: type enum, s: TaintedString): T =
|
||||||
parseEnum[T](string(s))
|
parseEnum[T](string(s))
|
||||||
|
@ -526,13 +526,13 @@ proc completeCmdArg[T](_: type Option[T], val: TaintedString): seq[string] =
|
||||||
proc completeCmdArgAux(T: type, val: TaintedString): seq[string] =
|
proc completeCmdArgAux(T: type, val: TaintedString): seq[string] =
|
||||||
return completeCmdArg(T, val)
|
return completeCmdArg(T, val)
|
||||||
|
|
||||||
template setField[T](loc: var T, val: TaintedString, defaultVal: untyped) =
|
template setField[T](loc: var T, val: Option[TaintedString], defaultVal: untyped) =
|
||||||
type FieldType = type(loc)
|
type FieldType = type(loc)
|
||||||
loc = if len(val) > 0: parseCmdArgAux(FieldType, val)
|
loc = if isSome(val): parseCmdArgAux(FieldType, val.get)
|
||||||
else: FieldType(defaultVal)
|
else: FieldType(defaultVal)
|
||||||
|
|
||||||
template setField[T](loc: var seq[T], val: TaintedString, defaultVal: untyped) =
|
template setField[T](loc: var seq[T], val: Option[TaintedString], defaultVal: untyped) =
|
||||||
loc.add parseCmdArgAux(type(loc[0]), val)
|
loc.add parseCmdArgAux(type(loc[0]), val.get)
|
||||||
|
|
||||||
template simpleSet(loc: var auto) =
|
template simpleSet(loc: var auto) =
|
||||||
discard
|
discard
|
||||||
|
@ -541,7 +541,7 @@ proc makeDefaultValue*(T: type): T =
|
||||||
discard
|
discard
|
||||||
|
|
||||||
proc requiresInput*(T: type): bool =
|
proc requiresInput*(T: type): bool =
|
||||||
not ((T is seq) or (T is Option))
|
not ((T is seq) or (T is Option) or (T is bool))
|
||||||
|
|
||||||
proc acceptsMultipleValues*(T: type): bool =
|
proc acceptsMultipleValues*(T: type): bool =
|
||||||
T is seq
|
T is seq
|
||||||
|
@ -584,12 +584,12 @@ macro generateFieldSetters(RecordType: type): untyped =
|
||||||
proc `completerName`(val: TaintedString): seq[string] {.nimcall, gcsafe.} =
|
proc `completerName`(val: TaintedString): seq[string] {.nimcall, gcsafe.} =
|
||||||
return completeCmdArgAux(`fixedFieldType`, val)
|
return completeCmdArgAux(`fixedFieldType`, val)
|
||||||
|
|
||||||
proc `setterName`(`configVar`: var `RecordType`, val: TaintedString) {.nimcall, gcsafe.} =
|
proc `setterName`(`configVar`: var `RecordType`, val: Option[TaintedString]) {.nimcall, gcsafe.} =
|
||||||
when `configField` is enum:
|
when `configField` is enum:
|
||||||
# TODO: For some reason, the normal `setField` rejects enum fields
|
# TODO: For some reason, the normal `setField` rejects enum fields
|
||||||
# when they are used as case discriminators. File this as a bug.
|
# when they are used as case discriminators. File this as a bug.
|
||||||
if len(val) > 0:
|
if val.isSome:
|
||||||
`configField` = parseEnum[type(`configField`)](string(val))
|
`configField` = parseEnum[type(`configField`)](string(val.get))
|
||||||
else:
|
else:
|
||||||
`configField` = `defaultValue`
|
`configField` = `defaultValue`
|
||||||
else:
|
else:
|
||||||
|
@ -701,7 +701,7 @@ proc load*(Configuration: type,
|
||||||
|
|
||||||
template applySetter(setterIdx: int, cmdLineVal: TaintedString) =
|
template applySetter(setterIdx: int, cmdLineVal: TaintedString) =
|
||||||
try:
|
try:
|
||||||
fieldSetters[setterIdx][1](confAddr[], cmdLineVal)
|
fieldSetters[setterIdx][1](confAddr[], some(cmdLineVal))
|
||||||
inc fieldCounters[setterIdx]
|
inc fieldCounters[setterIdx]
|
||||||
except:
|
except:
|
||||||
fail("Invalid value for " & fieldSetters[setterIdx][0] & ": " &
|
fail("Invalid value for " & fieldSetters[setterIdx][0] & ": " &
|
||||||
|
@ -722,7 +722,7 @@ proc load*(Configuration: type,
|
||||||
if opt.required:
|
if opt.required:
|
||||||
fail "The required option '$1' was not specified" % [opt.longform]
|
fail "The required option '$1' was not specified" % [opt.longform]
|
||||||
elif opt.hasDefault:
|
elif opt.hasDefault:
|
||||||
fieldSetters[opt.idx][1](conf, TaintedString(""))
|
fieldSetters[opt.idx][1](conf, none[TaintedString]())
|
||||||
|
|
||||||
template activateCmd(discriminator: OptInfo, activatedCmd: CmdInfo) =
|
template activateCmd(discriminator: OptInfo, activatedCmd: CmdInfo) =
|
||||||
let cmd = activatedCmd
|
let cmd = activatedCmd
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
import
|
import
|
||||||
../confutils
|
../confutils
|
||||||
|
|
||||||
cli do (foo: int, bar: string, args {.argument.}: seq[string]):
|
cli do (foo: int, bar: string, withBaz: bool, args {.argument.}: seq[string]):
|
||||||
echo "foo = ", foo
|
echo "foo = ", foo
|
||||||
echo "bar = ", bar
|
echo "bar = ", bar
|
||||||
|
echo "baz = ", withBaz
|
||||||
for arg in args: echo "arg ", arg
|
for arg in args: echo "arg ", arg
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue