2019-03-18 10:05:24 +07:00
|
|
|
import
|
2023-03-17 20:20:52 +07:00
|
|
|
std/[parseopt, strutils, options]
|
2019-03-18 10:05:24 +07:00
|
|
|
|
|
|
|
type
|
|
|
|
ConfigStatus* = enum
|
|
|
|
## Configuration status flags
|
|
|
|
Success, ## Success
|
|
|
|
EmptyOption, ## No options in category
|
|
|
|
ErrorUnknownOption, ## Unknown option in command line found
|
|
|
|
ErrorParseOption, ## Error in parsing command line option
|
|
|
|
ErrorIncorrectOption, ## Option has incorrect value
|
|
|
|
Error ## Unspecified error
|
|
|
|
|
|
|
|
Configuration = ref object
|
|
|
|
testSubject*: string
|
2023-01-11 00:02:21 +07:00
|
|
|
fork*: string
|
|
|
|
index*: Option[int]
|
2019-08-19 21:12:32 +07:00
|
|
|
trace*: bool
|
2020-02-19 21:26:16 +07:00
|
|
|
legacy*: bool
|
2020-02-19 21:50:03 +07:00
|
|
|
pruning*: bool
|
2023-09-25 06:53:20 +07:00
|
|
|
json*: bool
|
2019-03-18 10:05:24 +07:00
|
|
|
|
|
|
|
var testConfig {.threadvar.}: Configuration
|
|
|
|
|
|
|
|
proc initConfiguration(): Configuration =
|
|
|
|
result = new Configuration
|
2019-08-19 21:12:32 +07:00
|
|
|
result.trace = true
|
2020-02-19 21:50:03 +07:00
|
|
|
result.pruning = true
|
2019-03-18 10:05:24 +07:00
|
|
|
|
|
|
|
proc getConfiguration*(): Configuration {.gcsafe.} =
|
|
|
|
if isNil(testConfig):
|
|
|
|
testConfig = initConfiguration()
|
|
|
|
result = testConfig
|
|
|
|
|
|
|
|
proc processArguments*(msg: var string): ConfigStatus =
|
|
|
|
var
|
|
|
|
opt = initOptParser()
|
|
|
|
config = getConfiguration()
|
|
|
|
|
|
|
|
result = Success
|
|
|
|
for kind, key, value in opt.getopt():
|
|
|
|
case kind
|
|
|
|
of cmdArgument:
|
|
|
|
config.testSubject = key
|
|
|
|
of cmdLongOption, cmdShortOption:
|
|
|
|
case key.toLowerAscii()
|
2023-01-11 00:02:21 +07:00
|
|
|
of "fork": config.fork = value
|
|
|
|
of "index": config.index = some(parseInt(value))
|
2019-08-19 21:12:32 +07:00
|
|
|
of "trace": config.trace = parseBool(value)
|
2020-02-19 21:26:16 +07:00
|
|
|
of "legacy": config.legacy = parseBool(value)
|
2020-02-19 21:50:03 +07:00
|
|
|
of "pruning": config.pruning = parseBool(value)
|
2023-09-25 06:53:20 +07:00
|
|
|
of "json": config.json = parseBool(value)
|
2019-03-18 10:05:24 +07:00
|
|
|
else:
|
|
|
|
msg = "Unknown option " & key
|
|
|
|
if value.len > 0: msg = msg & " : " & value
|
|
|
|
result = ErrorUnknownOption
|
|
|
|
break
|
|
|
|
of cmdEnd:
|
2019-03-19 08:35:37 +07:00
|
|
|
doAssert(false)
|