0

I have two Torch scripts. One is some code for training a model, and the other is for testing it.

The training script accepts command line arguments

cmd = torch.CmdLine()
cmd:option('--foo', 'bar', 'example option')
opt = cmd:parse(arg or {})

The testing script also accepts command line arguments. It requires the training script because it needs some functions and constants the training script defines

require 'training'
cmd = torch.CmdLine()
cmd:option('--bar', 'foo', 'other option')
opt = cmd:parse(arg or {})

When I run my testing script, it runs the cmd:parse call from training, and it fails because bar isn't a valid option to pass in. Is there a convention for how to make the cmd:parse call not happen unless it's run from the command line?

Titandrake
  • 616
  • 4
  • 17
  • Start your training script with `if not debug.getinfo(3) then print'It is safe to invoke cmd:parse() now' end` [Link](http://stackoverflow.com/a/9074598/1847592) – Egor Skriptunoff Feb 24 '16 at 20:47

1 Answers1

0

As said in the comment, debug.getinfo(3) gives different behavior when called from an include.

When called from the command line, debug.getinfo(3).name is nil. When called from require on that file, debug.getinfo(3).name is pcall. So I wrapped all the command line function into an if block that checks debug.getinfo(3) first. (It's analogous to __name__ = '__main__' from Python.)

This is with the Torch interpreter, I don't know if this works with other Lua interpreters.

Titandrake
  • 616
  • 4
  • 17