2018-11-21 13:25:53 +00:00
|
|
|
import
|
2019-01-23 11:49:10 +00:00
|
|
|
os, parseopt, strutils, options, std_shims/macros_shim, typetraits, terminal,
|
2018-12-19 12:51:00 +00:00
|
|
|
confutils/defs
|
2018-11-21 13:25:53 +00:00
|
|
|
|
|
|
|
export
|
|
|
|
defs
|
|
|
|
|
2019-01-23 11:49:10 +00:00
|
|
|
type
|
|
|
|
CommandDesc = object
|
|
|
|
name: string
|
|
|
|
options: seq[OptionDesc]
|
|
|
|
subCommands: seq[CommandDesc]
|
|
|
|
fieldIdx: int
|
|
|
|
argumentsFieldIdx: int
|
|
|
|
|
|
|
|
OptionDesc = object
|
|
|
|
name, typename, shortform: string
|
|
|
|
hasDefault: bool
|
|
|
|
rejectNext: bool
|
|
|
|
fieldIdx: int
|
|
|
|
desc: string
|
|
|
|
|
|
|
|
template appName: string =
|
|
|
|
getAppFilename().splitFile.name
|
|
|
|
|
|
|
|
when not defined(confutils_no_colors):
|
|
|
|
template write(args: varargs[untyped]) =
|
|
|
|
stdout.styledWrite(args)
|
|
|
|
else:
|
|
|
|
const
|
|
|
|
styleBright = ""
|
|
|
|
|
|
|
|
template write(args: varargs[untyped]) =
|
|
|
|
stdout.write(args)
|
|
|
|
|
|
|
|
proc describeCmdOptions(cmd: CommandDesc) =
|
|
|
|
for opt in cmd.options:
|
|
|
|
write " --", opt.name, "=", opt.typename
|
|
|
|
if opt.desc.len > 0:
|
|
|
|
write repeat(" ", max(0, 40 - opt.name.len - opt.typename.len)), ": ", opt.desc
|
|
|
|
write "\n"
|
|
|
|
|
2019-03-18 02:01:07 +00:00
|
|
|
proc showHelp(version: string, cmd: CommandDesc) =
|
|
|
|
let app = appName & " " & version
|
2019-01-23 11:49:10 +00:00
|
|
|
|
|
|
|
write "Usage: ", styleBright, app
|
|
|
|
if cmd.name.len > 0: write " ", cmd.name
|
|
|
|
if cmd.options.len > 0: write " [OPTIONS]"
|
|
|
|
if cmd.subCommands.len > 0: write " <command>"
|
|
|
|
if cmd.argumentsFieldIdx != -1: write " [<args>]"
|
|
|
|
|
|
|
|
if cmd.options.len > 0:
|
|
|
|
write "\n\nThe following options are supported:\n\n"
|
|
|
|
describeCmdOptions(cmd)
|
|
|
|
|
|
|
|
if cmd.subCommands.len > 0:
|
|
|
|
write "\nAvailable sub-commands:\n\n"
|
|
|
|
for subcmd in cmd.subCommands:
|
|
|
|
write " ", styleBright, app, " ", subcmd.name, "\n\n"
|
|
|
|
describeCmdOptions(subcmd)
|
|
|
|
|
|
|
|
write "\n"
|
|
|
|
quit(0)
|
|
|
|
|
2019-03-18 02:01:07 +00:00
|
|
|
# TODO remove the overloads here to get better "missing overload" error message
|
|
|
|
proc parseCmdArg*(T: type InputDir, p: TaintedString): T =
|
|
|
|
if not dirExists(p.string):
|
|
|
|
raise newException(ValueError, "Directory doesn't exist")
|
2018-11-21 13:25:53 +00:00
|
|
|
|
2019-03-18 02:01:07 +00:00
|
|
|
result = T(p)
|
2018-12-19 10:52:32 +00:00
|
|
|
|
2019-03-18 02:01:07 +00:00
|
|
|
proc parseCmdArg*(T: type InputFile, p: TaintedString): T =
|
|
|
|
# TODO this is needed only because InputFile cannot be made
|
|
|
|
# an alias of TypedInputFile at the moment, because of a generics
|
|
|
|
# caching issue
|
|
|
|
if not fileExists(p.string):
|
|
|
|
raise newException(ValueError, "File doesn't exist")
|
|
|
|
|
|
|
|
try:
|
|
|
|
let f = open(p.string, fmRead)
|
|
|
|
close f
|
|
|
|
except IOError:
|
|
|
|
raise newException(ValueError, "File not accessible")
|
|
|
|
|
|
|
|
result = T(p.string)
|
|
|
|
|
|
|
|
proc parseCmdArg*(T: type TypedInputFile, p: TaintedString): T =
|
|
|
|
var path = p.string
|
|
|
|
when T.defaultExt.len > 0:
|
|
|
|
path = path.addFileExt(T.defaultExt)
|
|
|
|
|
|
|
|
if not fileExists(path):
|
|
|
|
raise newException(ValueError, "File doesn't exist")
|
|
|
|
|
|
|
|
try:
|
|
|
|
let f = open(path, fmRead)
|
|
|
|
close f
|
|
|
|
except IOError:
|
|
|
|
raise newException(ValueError, "File not accessible")
|
|
|
|
|
|
|
|
result = T(path)
|
|
|
|
|
|
|
|
proc parseCmdArg*(T: type[OutDir|OutFile|OutPath], p: TaintedString): T =
|
|
|
|
result = T(p)
|
2018-12-19 12:51:00 +00:00
|
|
|
|
|
|
|
proc parseCmdArg*[T](_: type Option[T], s: TaintedString): Option[T] =
|
|
|
|
return some(parseCmdArg(T, s))
|
|
|
|
|
2018-12-19 10:52:32 +00:00
|
|
|
template parseCmdArg*(T: type string, s: TaintedString): string =
|
2018-11-22 21:15:50 +00:00
|
|
|
string s
|
|
|
|
|
2018-12-19 10:52:32 +00:00
|
|
|
proc parseCmdArg*(T: type SomeSignedInt, s: TaintedString): T =
|
2018-11-22 21:15:50 +00:00
|
|
|
T parseInt(string s)
|
|
|
|
|
2018-12-19 10:52:32 +00:00
|
|
|
proc parseCmdArg*(T: type SomeUnsignedInt, s: TaintedString): T =
|
2018-11-22 21:15:50 +00:00
|
|
|
T parseUInt(string s)
|
2018-11-21 13:25:53 +00:00
|
|
|
|
2019-01-23 16:11:24 +00:00
|
|
|
proc parseCmdArg*(T: type SomeFloat, p: TaintedString): T =
|
|
|
|
result = parseFloat(p)
|
|
|
|
|
|
|
|
proc parseCmdArg*(T: type bool, p: TaintedString): T =
|
|
|
|
result = parseBool(p)
|
|
|
|
|
2018-12-19 10:52:32 +00:00
|
|
|
proc parseCmdArg*(T: type enum, s: TaintedString): T =
|
|
|
|
parseEnum[T](string(s))
|
|
|
|
|
2019-03-18 02:01:07 +00:00
|
|
|
proc parseCmdArgAux(T: type, s: TaintedString): T = # {.raises: [ValueError].} =
|
|
|
|
# The parseCmdArg procs are allowed to raise only `ValueError`.
|
|
|
|
# If you have provided your own specializations, please handle
|
|
|
|
# all other exception types.
|
2018-12-19 10:52:32 +00:00
|
|
|
mixin parseCmdArg
|
2019-03-18 02:01:07 +00:00
|
|
|
parseCmdArg(T, s)
|
2018-12-19 10:52:32 +00:00
|
|
|
|
2019-03-18 02:01:07 +00:00
|
|
|
template setField[T](loc: var T, val: TaintedString, defaultVal: untyped): bool =
|
|
|
|
type FieldType = type(loc)
|
|
|
|
loc = if len(val) > 0: parseCmdArgAux(FieldType, val)
|
2018-12-19 10:52:32 +00:00
|
|
|
else: FieldType(defaultVal)
|
|
|
|
true
|
|
|
|
|
|
|
|
template setField[T](loc: var seq[T], val: TaintedString, defaultVal: untyped): bool =
|
2019-03-18 02:01:07 +00:00
|
|
|
loc.add parseCmdArgAux(type(loc[0]), val)
|
2018-12-19 10:52:32 +00:00
|
|
|
false
|
|
|
|
|
|
|
|
template simpleSet(loc: var auto) =
|
|
|
|
discard
|
|
|
|
|
|
|
|
proc makeDefaultValue*(T: type): T =
|
|
|
|
discard
|
|
|
|
|
2018-12-19 12:51:00 +00:00
|
|
|
proc requiresInput*(T: type): bool =
|
|
|
|
result = not ((T is seq) or (T is Option))
|
|
|
|
|
2019-01-23 11:49:10 +00:00
|
|
|
# TODO: The usage of this should be replacable with just `type(x)`,
|
|
|
|
# but Nim is not able to handle it at the moment.
|
|
|
|
macro typeof(x: typed): untyped =
|
|
|
|
result = x.getType
|
|
|
|
|
|
|
|
template debugMacroResult(macroName: string) {.dirty.} =
|
|
|
|
when defined(debugMacros) or defined(debugConfutils):
|
|
|
|
echo "\n-------- ", macroName, " ----------------------"
|
|
|
|
echo result.repr
|
|
|
|
|
2018-11-21 13:25:53 +00:00
|
|
|
proc load*(Configuration: type,
|
|
|
|
cmdLine = commandLineParams(),
|
2019-03-18 02:01:07 +00:00
|
|
|
version = "",
|
2018-11-21 13:25:53 +00:00
|
|
|
printUsage = true,
|
|
|
|
quitOnFailure = true): Configuration =
|
|
|
|
## Loads a program configuration by parsing command-line arguments
|
|
|
|
## and a standard set of config files that can specify:
|
|
|
|
##
|
|
|
|
## - working directory settings
|
|
|
|
## - user settings
|
|
|
|
## - system-wide setttings
|
|
|
|
##
|
|
|
|
## Supports multiple config files format (INI/TOML, YAML, JSON).
|
|
|
|
|
|
|
|
# This is an initial naive implementation that will be improved
|
|
|
|
# over time.
|
|
|
|
|
|
|
|
type
|
2018-12-19 10:52:32 +00:00
|
|
|
FieldSetter = proc (cfg: var Configuration, val: TaintedString): bool {.nimcall.}
|
2018-11-21 13:25:53 +00:00
|
|
|
|
2018-12-19 10:52:32 +00:00
|
|
|
template readPragma(field, name): NimNode =
|
|
|
|
let p = field.pragmas.findPragma bindSym(name)
|
|
|
|
if p != nil and p.len == 2: p[1] else: p
|
|
|
|
|
|
|
|
macro generateFieldSetters(RecordType: type): untyped =
|
|
|
|
var recordDef = RecordType.getType[1].getImpl
|
|
|
|
let makeDefaultValue = bindSym"makeDefaultValue"
|
|
|
|
|
|
|
|
result = newTree(nnkStmtListExpr)
|
|
|
|
var settersArray = newTree(nnkBracket)
|
|
|
|
|
|
|
|
for field in recordFields(recordDef):
|
|
|
|
var
|
|
|
|
setterName = ident($field.name & "Setter")
|
|
|
|
fieldName = field.name
|
2019-03-18 02:01:07 +00:00
|
|
|
configVar = ident "config"
|
|
|
|
configField = newTree(nnkDotExpr, configVar, fieldName)
|
2018-12-19 10:52:32 +00:00
|
|
|
defaultValue = field.readPragma"defaultValue"
|
|
|
|
|
|
|
|
if defaultValue == nil:
|
2019-03-18 02:01:07 +00:00
|
|
|
defaultValue = newCall(makeDefaultValue, newTree(nnkTypeOfExpr, configField))
|
2018-12-19 10:52:32 +00:00
|
|
|
|
2019-01-23 11:49:10 +00:00
|
|
|
# TODO: This shouldn't be necessary. The type symbol returned from Nim should
|
|
|
|
# be typed as a tyTypeDesc[tyString] instead of just `tyString`. To be filed.
|
|
|
|
var fixedFieldType = newTree(nnkTypeOfExpr, field.typ)
|
|
|
|
|
2018-12-19 12:51:00 +00:00
|
|
|
settersArray.add newTree(nnkTupleConstr,
|
2019-03-18 02:01:07 +00:00
|
|
|
newLit($fieldName),
|
2018-12-19 12:51:00 +00:00
|
|
|
newCall(bindSym"FieldSetter", setterName),
|
2019-01-23 11:49:10 +00:00
|
|
|
newCall(bindSym"requiresInput", fixedFieldType))
|
2018-12-19 10:52:32 +00:00
|
|
|
|
|
|
|
result.add quote do:
|
2019-03-18 02:01:07 +00:00
|
|
|
proc `setterName`(`configVar`: var `RecordType`, val: TaintedString): bool {.nimcall.} =
|
|
|
|
when `configField` is enum:
|
2018-12-19 10:52:32 +00:00
|
|
|
# TODO: For some reason, the normal `setField` rejects enum fields
|
|
|
|
# when they are used as case discriminators. File this as a bug.
|
2019-01-23 20:20:25 +00:00
|
|
|
if len(val) > 0:
|
2019-03-18 02:01:07 +00:00
|
|
|
`configField` = parseEnum[type(`configField`)](string(val))
|
2019-01-23 20:20:25 +00:00
|
|
|
else:
|
2019-03-18 02:01:07 +00:00
|
|
|
`configField` = `defaultValue`
|
2018-12-19 10:52:32 +00:00
|
|
|
return true
|
|
|
|
else:
|
2019-03-18 02:01:07 +00:00
|
|
|
return setField(`configField`, val, `defaultValue`)
|
2018-12-19 10:52:32 +00:00
|
|
|
|
|
|
|
result.add settersArray
|
2019-01-23 11:49:10 +00:00
|
|
|
debugMacroResult "Field Setters"
|
2018-12-19 10:52:32 +00:00
|
|
|
|
|
|
|
macro buildCommandTree(RecordType: type): untyped =
|
|
|
|
var recordDef = RecordType.getType[1].getImpl
|
|
|
|
var res: CommandDesc
|
|
|
|
res.argumentsFieldIdx = -1
|
|
|
|
|
|
|
|
var fieldIdx = 0
|
|
|
|
for field in recordFields(recordDef):
|
|
|
|
let
|
|
|
|
isCommand = field.readPragma"command" != nil
|
|
|
|
hasDefault = field.readPragma"defaultValue" != nil
|
|
|
|
shortform = field.readPragma"shortform"
|
|
|
|
longform = field.readPragma"longform"
|
|
|
|
desc = field.readPragma"desc"
|
|
|
|
|
|
|
|
if isCommand:
|
|
|
|
let cmdType = field.typ.getImpl[^1]
|
|
|
|
if cmdType.kind != nnkEnumTy:
|
|
|
|
error "The command pragma should be specified only on enum fields", field.name
|
|
|
|
for i in 2 ..< cmdType.len:
|
|
|
|
res.subCommands.add CommandDesc(name: $cmdType[i],
|
|
|
|
fieldIdx: fieldIdx,
|
|
|
|
argumentsFieldIdx: -1)
|
|
|
|
else:
|
|
|
|
var option: OptionDesc
|
|
|
|
option.fieldIdx = fieldIdx
|
|
|
|
option.name = $field.name
|
2018-12-19 12:51:00 +00:00
|
|
|
option.hasDefault = hasDefault
|
2018-12-19 10:52:32 +00:00
|
|
|
option.typename = field.typ.repr
|
2019-01-23 11:49:10 +00:00
|
|
|
if desc != nil: option.desc = desc.strVal
|
2018-12-19 10:52:32 +00:00
|
|
|
if longform != nil: option.name = longform.strVal
|
|
|
|
if shortform != nil: option.shortform = shortform.strVal
|
2018-11-21 13:25:53 +00:00
|
|
|
|
2018-12-19 10:52:32 +00:00
|
|
|
var isSubcommandOption = false
|
|
|
|
if field.caseBranch != nil:
|
|
|
|
let branchCmd = $field.caseBranch[0]
|
|
|
|
for cmd in mitems(res.subCommands):
|
|
|
|
if cmd.name == branchCmd:
|
|
|
|
cmd.options.add option
|
|
|
|
isSubcommandOption = true
|
|
|
|
break
|
2018-11-21 13:25:53 +00:00
|
|
|
|
2018-12-19 10:52:32 +00:00
|
|
|
if not isSubcommandOption:
|
|
|
|
res.options.add option
|
2018-11-21 13:25:53 +00:00
|
|
|
|
2018-12-19 10:52:32 +00:00
|
|
|
inc fieldIdx
|
2018-11-21 13:25:53 +00:00
|
|
|
|
2018-12-19 10:52:32 +00:00
|
|
|
result = newLitFixed(res)
|
2019-01-23 11:49:10 +00:00
|
|
|
debugMacroResult "Command Tree"
|
2018-11-21 13:25:53 +00:00
|
|
|
|
2018-12-19 10:52:32 +00:00
|
|
|
let fieldSetters = generateFieldSetters(Configuration)
|
|
|
|
var rootCmd = buildCommandTree(Configuration)
|
2019-03-18 02:01:07 +00:00
|
|
|
let confAddr = addr result
|
2018-11-21 13:25:53 +00:00
|
|
|
|
|
|
|
proc fail(msg: string) =
|
|
|
|
if quitOnFailure:
|
|
|
|
stderr.writeLine(msg)
|
2019-01-23 20:20:25 +00:00
|
|
|
stderr.writeLine("Try '$1 --help' for more information" % appName)
|
2018-11-21 13:25:53 +00:00
|
|
|
quit 1
|
|
|
|
else:
|
|
|
|
raise newException(ConfigurationError, msg)
|
|
|
|
|
2018-12-19 10:52:32 +00:00
|
|
|
proc findOption(cmd: ptr CommandDesc, name: TaintedString): ptr OptionDesc =
|
|
|
|
for o in cmd.options.mitems:
|
|
|
|
if cmpIgnoreStyle(o.name, string(name)) == 0 or
|
|
|
|
cmpIgnoreStyle(o.shortform, string(name)) == 0:
|
|
|
|
return addr(o)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
proc findSubcommand(cmd: ptr CommandDesc, name: TaintedString): ptr CommandDesc =
|
|
|
|
for subCmd in cmd.subCommands.mitems:
|
|
|
|
if cmpIgnoreStyle(subCmd.name, string(name)) == 0:
|
|
|
|
return addr(subCmd)
|
2018-11-21 13:25:53 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
|
2019-03-18 02:01:07 +00:00
|
|
|
template applySetter(setterIdx: int, cmdLineVal: TaintedString): bool =
|
|
|
|
var r: bool
|
|
|
|
try:
|
|
|
|
r = fieldSetters[setterIdx][1](confAddr[], cmdLineVal)
|
|
|
|
except:
|
|
|
|
fail("Invalid value for " & fieldSetters[setterIdx][0] & ": " &
|
|
|
|
getCurrentExceptionMsg())
|
|
|
|
r
|
|
|
|
|
2018-12-19 12:51:00 +00:00
|
|
|
template required(opt: OptionDesc): bool =
|
2019-03-18 02:01:07 +00:00
|
|
|
fieldSetters[opt.fieldIdx][2] and not opt.hasDefault
|
2018-12-19 12:51:00 +00:00
|
|
|
|
2019-01-23 11:49:10 +00:00
|
|
|
proc processMissingOptions(conf: var Configuration, cmd: ptr CommandDesc) =
|
2018-12-19 10:52:32 +00:00
|
|
|
for o in cmd.options:
|
2019-01-23 11:49:10 +00:00
|
|
|
if o.rejectNext == false:
|
|
|
|
if o.required:
|
|
|
|
fail "The required option '$1' was not specified" % [o.name]
|
|
|
|
elif o.hasDefault:
|
2019-03-18 02:01:07 +00:00
|
|
|
discard fieldSetters[o.fieldIdx][1](conf, TaintedString(""))
|
2018-12-19 10:52:32 +00:00
|
|
|
|
|
|
|
var currentCmd = addr rootCmd
|
|
|
|
var rejectNextArgument = currentCmd.argumentsFieldIdx == -1
|
|
|
|
|
2018-11-21 13:25:53 +00:00
|
|
|
for kind, key, val in getopt(cmdLine):
|
2018-12-19 10:52:32 +00:00
|
|
|
case kind
|
|
|
|
of cmdLongOption, cmdShortOption:
|
2019-01-23 11:49:10 +00:00
|
|
|
if string(key) == "help":
|
2019-03-18 02:01:07 +00:00
|
|
|
showHelp version, currentCmd[]
|
2019-01-23 11:49:10 +00:00
|
|
|
|
2018-12-19 10:52:32 +00:00
|
|
|
let option = currentCmd.findOption(key)
|
|
|
|
if option != nil:
|
|
|
|
if option.rejectNext:
|
2018-11-21 13:25:53 +00:00
|
|
|
fail "The options '$1' should not be specified more than once" % [string(key)]
|
2019-03-18 02:01:07 +00:00
|
|
|
option.rejectNext = applySetter(option.fieldIdx, val)
|
2018-11-21 13:25:53 +00:00
|
|
|
else:
|
|
|
|
fail "Unrecognized option '$1'" % [string(key)]
|
|
|
|
|
2018-12-19 10:52:32 +00:00
|
|
|
of cmdArgument:
|
2019-01-23 11:49:10 +00:00
|
|
|
if string(key) == "help" and currentCmd.subCommands.len > 0:
|
2019-03-18 02:01:07 +00:00
|
|
|
showHelp version, currentCmd[]
|
2019-01-23 11:49:10 +00:00
|
|
|
|
2018-12-19 10:52:32 +00:00
|
|
|
let subCmd = currentCmd.findSubcommand(key)
|
|
|
|
if subCmd != nil:
|
2019-03-18 02:01:07 +00:00
|
|
|
discard applySetter(subCmd.fieldIdx, key)
|
2018-12-19 10:52:32 +00:00
|
|
|
currentCmd = subCmd
|
|
|
|
rejectNextArgument = currentCmd.argumentsFieldIdx == -1
|
|
|
|
else:
|
|
|
|
if rejectNextArgument:
|
|
|
|
fail "The command '$1' does not accept additional arguments" % [currentCmd.name]
|
|
|
|
let argumentIdx = currentCmd.argumentsFieldIdx
|
|
|
|
doAssert argumentIdx != -1
|
2019-03-18 02:01:07 +00:00
|
|
|
rejectNextArgument = applySetter(argumentIdx, key)
|
2018-12-19 10:52:32 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
discard
|
|
|
|
|
2019-01-23 11:49:10 +00:00
|
|
|
result.processMissingOptions(currentCmd)
|
|
|
|
|
|
|
|
proc dispatchImpl(cliProcSym, cliArgs, loadArgs: NimNode): NimNode =
|
|
|
|
# Here, we'll create a configuration object with fields matching
|
|
|
|
# the CLI proc params. We'll also generate a call to the designated
|
|
|
|
# p
|
|
|
|
let configType = genSym(nskType, "CliConfig")
|
|
|
|
let configFields = newTree(nnkRecList)
|
|
|
|
let configVar = genSym(nskLet, "config")
|
|
|
|
var dispatchCall = newCall(cliProcSym)
|
|
|
|
|
|
|
|
# The return type of the proc is skipped over
|
|
|
|
for i in 1 ..< cliArgs.len:
|
|
|
|
var arg = copy cliArgs[i]
|
|
|
|
|
|
|
|
# If an argument doesn't specify a type, we infer it from the default value
|
|
|
|
if arg[1].kind == nnkEmpty:
|
|
|
|
if arg[2].kind == nnkEmpty:
|
|
|
|
error "Please provide either a default value or type of the parameter", arg
|
|
|
|
arg[1] = newCall(bindSym"typeof", arg[2])
|
|
|
|
|
|
|
|
# Turn any default parameters into the confutils's `defaultValue` pragma
|
|
|
|
if arg[2].kind != nnkEmpty:
|
|
|
|
if arg[0].kind != nnkPragmaExpr:
|
2019-01-23 16:11:24 +00:00
|
|
|
arg[0] = newTree(nnkPragmaExpr, arg[0], newTree(nnkPragma))
|
|
|
|
arg[0][1].add newColonExpr(bindSym"defaultValue", arg[2])
|
2019-01-23 11:49:10 +00:00
|
|
|
arg[2] = newEmptyNode()
|
|
|
|
|
|
|
|
configFields.add arg
|
|
|
|
dispatchCall.add newTree(nnkDotExpr, configVar, skipPragma arg[0])
|
|
|
|
|
|
|
|
let cliConfigType = nnkTypeSection.newTree(
|
|
|
|
nnkTypeDef.newTree(
|
|
|
|
configType,
|
|
|
|
newEmptyNode(),
|
|
|
|
nnkObjectTy.newTree(
|
|
|
|
newEmptyNode(),
|
|
|
|
newEmptyNode(),
|
|
|
|
configFields)))
|
|
|
|
|
|
|
|
var loadConfigCall = newCall(bindSym"load", configType)
|
|
|
|
for p in loadArgs: loadConfigCall.add p
|
|
|
|
|
|
|
|
result = quote do:
|
|
|
|
`cliConfigType`
|
|
|
|
let `configVar` = `loadConfigCall`
|
|
|
|
`dispatchCall`
|
|
|
|
|
|
|
|
macro dispatch*(fn: typed, args: varargs[untyped]): untyped =
|
|
|
|
if fn.kind != nnkSym or
|
|
|
|
fn.symKind notin {nskProc, nskFunc, nskMacro, nskTemplate}:
|
|
|
|
error "The first argument to `confutils.dispatch` should be a callable symbol"
|
|
|
|
|
|
|
|
let fnImpl = fn.getImpl
|
|
|
|
result = dispatchImpl(fnImpl.name, fnImpl.params, args)
|
|
|
|
debugMacroResult "Dispatch Code"
|
|
|
|
|
|
|
|
macro cli*(args: varargs[untyped]): untyped =
|
|
|
|
if args.len == 0:
|
|
|
|
error "The cli macro expects a do block", args
|
|
|
|
|
|
|
|
let doBlock = args[^1]
|
|
|
|
if doBlock.kind notin {nnkDo, nnkLambda}:
|
|
|
|
error "The last argument to `confutils.cli` should be a do block", doBlock
|
|
|
|
|
|
|
|
args.del(args.len - 1)
|
|
|
|
|
|
|
|
# Create a new anonymous proc we'll dispatch to
|
|
|
|
let cliProcName = genSym(nskProc, "CLI")
|
|
|
|
var cliProc = newTree(nnkProcDef, cliProcName)
|
|
|
|
# Copy everything but the name from the do block:
|
|
|
|
for i in 1 ..< doBlock.len: cliProc.add doBlock[i]
|
|
|
|
|
|
|
|
# Generate the final code
|
|
|
|
result = newStmtList(cliProc, dispatchImpl(cliProcName, cliProc.params, args))
|
2019-01-23 16:11:24 +00:00
|
|
|
|
|
|
|
# TODO: remove this once Nim supports custom pragmas on proc params
|
|
|
|
for p in cliProc.params:
|
|
|
|
if p.kind == nnkEmpty: continue
|
|
|
|
p[0] = skipPragma p[0]
|
|
|
|
|
2019-01-23 11:49:10 +00:00
|
|
|
debugMacroResult "CLI Code"
|
2018-11-21 13:25:53 +00:00
|
|
|
|
2019-03-18 02:01:07 +00:00
|
|
|
proc load*(f: TypedInputFile): f.ContentType =
|
|
|
|
when f.Format is Unspecified or f.ContentType is Unspecified:
|
|
|
|
{.fatal: "To use `InputFile.load`, please specify the Format and ContentType of the file".}
|
|
|
|
|
|
|
|
when f.Format is Txt:
|
|
|
|
# TODO: implement a proper Txt serialization format
|
|
|
|
mixin init
|
|
|
|
f.ContentType.init readFile(f.string).string
|
|
|
|
else:
|
|
|
|
mixin loadFile
|
|
|
|
loadFile(f.Format, f.string, f.ContentType)
|
|
|
|
|