开发者

Does Io have a equivalent to Python's if __name__=="__main__": main()?

It would be spiffy if Io had this, so that you could control whether code is ru开发者_StackOverflow社区n, 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).


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜