In python, you can do 

if name == '__main__'

to preface code that should only be run when this module is DIRECTLY called 
as the entry point.

In julia, I tried replicating this (a la some other post in this group) as 

module M
export f
function f(x) 2x end
end

if length(ARGS) > 0 && ARGS[1] == "-r"
  eval(:(using M))
  # do something
end

Which is fine, so long as I do this for only one module.
Now let's say that I make a new file, with a new module, and want to import 
M as well

module N
export f2
function f2(x) 4x end
end

if length(ARGS) > 0 && ARGS[1] == "-r"
  include("m.jl")
  eval(:(using M))
  eval(:(using N))
  # do something
end

But when including m.jl, all the code in m is run, including the args part. 
So the program actually runs from there rather than from the module I 
actually called on the commandline. 
Is there a way around this that ISN'T hacky? EG, maybe read the name of the 
module on the commandline and see if the current file matches that? Seems 
clunky though, since you have to actually make I/O calls to the filesystem 
if the current filename isn't an environment variable already in julia 
(__FILE__ types?). 

I suppose the "real" solution is to make a main.jl file that appropriately 
exposes all the different running options of the collection of modules 
you're dealing with, but creating an entire command line util for a small 
set of modules seems like way overkill. Also, there's something nice about 
running `julia m.jl ...options` rather than `julia bootstrap.jl m.jl 
...options`, and it allows me to keep the runtime logic for each module 
contained within that module. An overall main file seems like it should 
only contain the use cases for the final program, not the run options for 
each intermediate module.


Thanks!
Vishesh













Reply via email to