Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c1dbcd90a4 | |||
| a33500f6d4 | |||
| ca7b2a620a | |||
| f2c729fd7d | |||
| eaffb20df5 |
11
cliutils.nim
11
cliutils.nim
@@ -21,14 +21,19 @@ proc termColor*(color: TermColor, bright, bold = false): string =
|
|||||||
if bright: inc(colorVal, 60)
|
if bright: inc(colorVal, 60)
|
||||||
return "\e[" & $colorVal & (if bold: ";1" else: "") & "m"
|
return "\e[" & $colorVal & (if bold: ";1" else: "") & "m"
|
||||||
|
|
||||||
proc withColor*(str: string, color: TermColor, bright, bold = false): string =
|
|
||||||
|
proc withColor*(str: string, color: TermColor, bright, bold, skipReset = false): string =
|
||||||
|
if skipReset:
|
||||||
return termColor(color, bright, bold) & str
|
return termColor(color, bright, bold) & str
|
||||||
|
else:
|
||||||
|
return termColor(color, bright, bold) & str & termReset
|
||||||
|
|
||||||
|
|
||||||
proc stripAnsi*(str: string): string =
|
proc stripAnsi*(str: string): string =
|
||||||
let STRIP_ANSI_REGEX = re"\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]"
|
let STRIP_ANSI_REGEX = re"\x1B\[([0-9]{1,2}(;[0-9]{0,2})?)?[m|K]"
|
||||||
return str.replace(STRIP_ANSI_REGEX, "")
|
return str.replace(STRIP_ANSI_REGEX, "")
|
||||||
|
|
||||||
|
|
||||||
proc doParseQueryParams[T](queryParams: T): seq[string] =
|
proc doParseQueryParams[T](queryParams: T): seq[string] =
|
||||||
result = @[]
|
result = @[]
|
||||||
|
|
||||||
@@ -40,8 +45,10 @@ proc doParseQueryParams[T](queryParams: T): seq[string] =
|
|||||||
result.add("--" & k)
|
result.add("--" & k)
|
||||||
if v != "true": result.add(v) # support things like ?verbose=true -> cmd --verbose
|
if v != "true": result.add(v) # support things like ?verbose=true -> cmd --verbose
|
||||||
|
|
||||||
|
|
||||||
proc queryParamsToCliArgs*(queryParams: Table[string, string]): seq[string] =
|
proc queryParamsToCliArgs*(queryParams: Table[string, string]): seq[string] =
|
||||||
doParseQueryParams(queryParams)
|
doParseQueryParams(queryParams)
|
||||||
|
|
||||||
|
|
||||||
proc queryParamsToCliArgs*(queryParams: StringTableRef): seq[string] =
|
proc queryParamsToCliArgs*(queryParams: StringTableRef): seq[string] =
|
||||||
doParseQueryParams(queryParams)
|
doParseQueryParams(queryParams)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
# Package
|
# Package
|
||||||
|
|
||||||
version = "0.9.0"
|
version = "0.10.1"
|
||||||
author = "Jonathan Bernard"
|
author = "Jonathan Bernard"
|
||||||
description = "Helper functions for writing command line interfaces."
|
description = "Helper functions for writing command line interfaces."
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
|||||||
@@ -6,13 +6,13 @@ type
|
|||||||
docopt*: Table[string, Value]
|
docopt*: Table[string, Value]
|
||||||
json*: JsonNode
|
json*: JsonNode
|
||||||
|
|
||||||
func keyNames(key: string): tuple[arg, env, json: string] {.raises: [KeyError].} =
|
proc keyNames(key: string): tuple[arg, env, json: string] {.raises: [KeyError].} =
|
||||||
try:
|
try:
|
||||||
result = (
|
result = (
|
||||||
"--" & key,
|
"--" & key,
|
||||||
key.replace('-', '_').toUpper,
|
key.replace('-', '_').toUpper,
|
||||||
key.replace(re"(-\w)", proc (m: RegexMatch): string = ($m)[1..1].toUpper))
|
key.replace(re"(-\w)", proc (m: RegexMatch): string = ($m)[1..1].toUpper))
|
||||||
except CatchableError:
|
except Exception:
|
||||||
raise newException(KeyError, "invalid config key: '" & key & "'")
|
raise newException(KeyError, "invalid config key: '" & key & "'")
|
||||||
|
|
||||||
template walkFieldDefs*(t: NimNode, body: untyped) =
|
template walkFieldDefs*(t: NimNode, body: untyped) =
|
||||||
@@ -95,7 +95,7 @@ proc getJson*(
|
|||||||
elif cfg.json.hasKey(jsonKey): return cfg.json[jsonKey]
|
elif cfg.json.hasKey(jsonKey): return cfg.json[jsonKey]
|
||||||
else: raise newException(ValueError, "cannot find a configuration value for \"" & key & "\"")
|
else: raise newException(ValueError, "cannot find a configuration value for \"" & key & "\"")
|
||||||
except Exception:
|
except Exception:
|
||||||
raise newException(ValueError, "cannot parse value as JSON:" & getCurrentExceptionMsg())
|
raise newException(ValueError, "cannot parse [" & getVal(cfg, key) & "] as JSON:" & getCurrentExceptionMsg())
|
||||||
|
|
||||||
proc getJson*(
|
proc getJson*(
|
||||||
cfg: CombinedConfig,
|
cfg: CombinedConfig,
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ proc exec*(command: string, workingDir: string = "",
|
|||||||
args: openArray[string] = [], env: StringTableRef = nil,
|
args: openArray[string] = [], env: StringTableRef = nil,
|
||||||
options: set[ProcessOption] = {poUsePath},
|
options: set[ProcessOption] = {poUsePath},
|
||||||
msgCB: HandleProcMsgCB = nil): int
|
msgCB: HandleProcMsgCB = nil): int
|
||||||
{.tags: [ExecIOEffect, ReadIOEffect, RootEffect], gcsafe.} =
|
{.tags: [ExecIOEffect, ReadIOEffect, RootEffect].} =
|
||||||
|
|
||||||
var p = startProcess(command, workingDir, args, env, options)
|
var p = startProcess(command, workingDir, args, env, options)
|
||||||
result = waitFor(p, msgCB, command)
|
result = waitFor(p, msgCB, command)
|
||||||
|
|||||||
Reference in New Issue
Block a user