如何从Nim的主块返回退出代码? [英] How to return an exit code from the main block in Nim?
问题描述
在Nim中,要编写作为某种主要功能执行的代码,您可以这样做(类似于Python中的main检查):
In Nim, to write code that executes as a sort of main function, you do (similar to the is main checks in Python):
when isMainModule:
echo ("Hello, Nim!")
但是,对于我自己的一生,我不知道如何返回错误代码.传统上,总是有一个使主函数返回int
的选项,但是由于它实际上不在proc
中,因此似乎您不能return
;因此,您可以将其返回.我唯一想出的方法是raise
一个异常.当然有一种方法可以控制您的退出代码是否为零?
However, for the life of me, I can't figure out how to return an error code. Traditionally there has always been an option to make main functions return int
, but since this is not actually in a proc
, it doesn't seem like you can return
; the only thing I've figured out how to do is to raise
an exception. Surely there is a way to just control whether your exit code is zero or not?
推荐答案
我认为system.quit
可能就是您想要的.根据 Nim文档:
I think system.quit
may be what you're looking for. According to the Nim docs:
proc quit(errorcode: int = QuitSuccess) {..}
使用退出代码立即停止程序.
Stops the program immediately with an exit code.
在您的nim程序完成而不会发生意外的平台时,隐式调用proc quit(QuitSuccess)
,这是预期的行为.引发的未处理异常等同于调用quit(QuitFailure)
.
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)
.
这篇关于如何从Nim的主块返回退出代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!