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
精彩评论