I am using torch.CmdLine() to parse command line arguments in Torch. I need to pass an array of elements for one of the variables. I am not aware of any mechanisms for passing an array of elements for a variable. So I am treating that variable as a string and passing the array of elements separated by space and enclosed within double quotes from the command line. The code for doing this looks as follows:
cmd = torch.CmdLine()
cmd:text('Training')
cmd:text()
cmd:option('-cuda_device',"1 2 3")
params = cmd:parse(arg or {})
--parse the string to extract array of numbers
for i=1,string.len(params.cuda_device) do
if params.cuda_device[i] ~= ' ' then
-- some code here
end
end
Here since Lua string indexing is not provided by default, I had to override __index to enable indexing of string as follows,
getmetatable('').__index = function(str,i) return string.sub(str,i,i) end
This works for parsing the string to an array of numbers.
However, overriding __index breaks the code somewhere else, throwing the following error:
qlua: /home/torch/install/share/lua/5.1/torch/init.lua:173: bad argument #2 to '__index' (number expected, got string)
I can do some workarounds to fix this (instead of overriding __index use string.sub(str,i,i) directly) but I would like to know your suggestions in passing an array of elements using torch.CmdLine() in an elegant way--if applicable.
Thanks in Advance.