2020-06-05 17:09:05 +03:00
|
|
|
import std/parseutils
|
|
|
|
import stew/shims/net as stewNet
|
|
|
|
export stewNet
|
2020-03-16 23:47:48 +02:00
|
|
|
|
2020-10-01 22:04:53 +03:00
|
|
|
export ValidIpAddress
|
|
|
|
|
2022-06-03 18:24:59 +00:00
|
|
|
func parseCmdArg*(T: type ValidIpAddress, s: string): T =
|
2023-02-15 15:26:53 +07:00
|
|
|
ValidIpAddress.init(s)
|
2020-03-16 23:47:48 +02:00
|
|
|
|
2022-06-03 18:24:59 +00:00
|
|
|
func completeCmdArg*(T: type ValidIpAddress, val: string): seq[string] =
|
2020-03-16 23:47:48 +02:00
|
|
|
# TODO: Maybe complete the local IP address?
|
2022-06-03 18:24:59 +00:00
|
|
|
@[]
|
2020-03-16 23:47:48 +02:00
|
|
|
|
2022-06-03 18:24:59 +00:00
|
|
|
func parseCmdArg*(T: type Port, s: string): T =
|
2020-03-16 23:47:48 +02:00
|
|
|
template fail =
|
|
|
|
raise newException(ValueError,
|
2020-10-09 16:29:48 +03:00
|
|
|
"The supplied port must be an integer value in the range 1-65535")
|
2020-03-16 23:47:48 +02:00
|
|
|
|
|
|
|
var intVal: int
|
2023-06-08 13:12:32 +00:00
|
|
|
let parsedChars = try: parseInt(s, intVal)
|
2020-03-16 23:47:48 +02:00
|
|
|
except CatchableError: fail()
|
|
|
|
|
2020-10-09 16:29:48 +03:00
|
|
|
if parsedChars != len(s) or intVal < 1 or intVal > 65535:
|
2020-03-16 23:47:48 +02:00
|
|
|
fail()
|
|
|
|
|
|
|
|
return Port(intVal)
|
|
|
|
|
2022-06-03 18:24:59 +00:00
|
|
|
func completeCmdArg*(T: type Port, val: string): seq[string] =
|
|
|
|
@[]
|