如何 return 从 Nim 的主块中退出代码?

How to return an exit code from the main block in Nim?

在 Nim 中,要编写作为一种 main 函数执行的代码,您可以这样做(类似于 Python 中的 is main 检查):

when isMainModule:
    echo ("Hello, Nim!")

然而,对于我来说,我想不出如何 return 错误代码。传统上一直有一个选项可以使主要功能 return int,但由于这实际上并不在 proc 中,因此您似乎不能 return;我唯一想出的方法是 raise 例外。肯定有一种方法可以控制您的退出代码是否为零?

我想 system.quit 可能就是您要找的。根据 Nim docs:

proc quit(errorcode: int = QuitSuccess) {..}

Stops the program immediately with an exit code.

The proc quit(QuitSuccess) is called implicitly when your nim program finishes without incident for platforms where this is the expected behavior. A raised unhandled exception is equivalent to calling quit(QuitFailure).