如果__name __ ==“__ main__”:main(),Io是否具有Python的等价物?

时间:2011-10-09 23:49:52

标签: io

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

1 个答案:

答案 0 :(得分:2)

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)

示例:

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