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

只愿长相守 提交于 2019-12-04 03:33:26

问题


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!")

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?


回答1:


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.

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).



来源:https://stackoverflow.com/questions/51949627/how-to-return-an-exit-code-from-the-main-block-in-nim

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!