Io 是否有相当于 Python 的 if __name__=="__main__": main() ?

发布于 2024-12-09 02:50:20 字数 120 浏览 0 评论 0 原文

如果Io有这个那就太好了,这样你就可以控制代码是否运行,例如API-CLI的组合coolstuff.io将运行命令行界面,但前提是直接运行,而不是当coolstuff.io被其他导入时运行Io 代码(可能有自己的命令行界面)。

It would be spiffy if Io had this, so that you could control whether code is run, e.g. a combination API-CLI coolstuff.io would run a command line interface, but only if run directly, not when coolstuff.io is imported by other Io code (which may have its own command line interface).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

像极了他 2024-12-16 02:50:20

ScriptedMain.io:test.io

#!/usr/bin/env io

ScriptedMain := Object clone
ScriptedMain meaningOfLife := 42

main := method(
    "Main: The meaning of life is #{ScriptedMain meaningOfLife}" interpolate println
)

if (System args size > 0 and System args at(0) containsSeq("ScriptedMain"), main)

#!/usr/bin/env io

main := method(
    "Test: The meaning of life is #{ScriptedMain meaningOfLife}" interpolate println
)

if (System args size > 0 and System args at(0) containsSeq("test"), main)

示例:

$ ./ScriptedMain.io 
Main: The meaning of life is 42
$ ./test.io
Test: The meaning of life is 42

ScriptedMain.io:

#!/usr/bin/env io

ScriptedMain := Object clone
ScriptedMain meaningOfLife := 42

main := method(
    "Main: The meaning of life is #{ScriptedMain meaningOfLife}" interpolate println
)

if (System args size > 0 and System args at(0) containsSeq("ScriptedMain"), main)

test.io:

#!/usr/bin/env io

main := method(
    "Test: The meaning of life is #{ScriptedMain meaningOfLife}" interpolate println
)

if (System args size > 0 and System args at(0) containsSeq("test"), main)

Example:

$ ./ScriptedMain.io 
Main: The meaning of life is 42
$ ./test.io
Test: The meaning of life is 42
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文