nimsuggest: add an option to bind to a free port (#10328)

This commit is contained in:
alaviss
2019-01-17 03:06:32 +07:00
committed by Andreas Rumpf
parent 1cb5e20620
commit fc30cf02bc

View File

@@ -35,6 +35,7 @@ Usage:
nimsuggest [options] projectfile.nim
Options:
--autobind automatically binds into a free port
--port:PORT port, by default 6000
--address:HOST binds to that address, by default ""
--stdin read commands from stdin and write results to
@@ -50,6 +51,8 @@ Options:
The server then listens to the connection and takes line-based commands.
If --autobind is used, the binded port number will be printed to stdout.
In addition, all command line options of Nim that do not affect code generation
are supported.
"""
@@ -68,6 +71,7 @@ var
gEmitEof: bool # whether we write '!EOF!' dummy lines
gLogging = defined(logging)
gRefresh: bool
gAutoBind = false
requests: Channel[string]
results: Channel[Suggest]
@@ -306,9 +310,15 @@ proc replCmdline(x: ThreadParams) {.thread.} =
proc replTcp(x: ThreadParams) {.thread.} =
var server = newSocket()
server.bindAddr(x.port, x.address)
if gAutoBind:
let port = server.connectToNextFreePort(x.address)
server.listen()
echo port
stdout.flushFile()
else:
server.bindAddr(x.port, x.address)
server.listen()
var inp = "".TaintedString
server.listen()
while true:
var stdoutSocket = newSocket()
accept(server, stdoutSocket)
@@ -544,6 +554,9 @@ proc processCmdLine*(pass: TCmdLinePass, cmd: string; conf: ConfigRef) =
of "help", "h":
stdout.writeline(Usage)
quit()
of "autobind":
gMode = mtcp
gAutoBind = true
of "port":
gPort = parseInt(p.val).Port
gMode = mtcp