This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Scotty's profile picture

Incorrect handling of command line arguments

Started by Scotty, 11 May 2013 - 11:41 PM
Scotty #1
Posted 12 May 2013 - 01:41 AM
I'm trying to make use of Pruby's dome and sphere program.
However - the latest version (which was a quick one I gather) has a flaw and while I have figured out where it is, I can't figure out how best to solve it.

The program in full is at http://pastebin.com/SUJsjAqi

The problem is (I think) the handling of the command line arguments. I include the relevant code below


for argn=2,#arg do
  if arg[argn] == "-c" then
	cost_only = true
  elseif arg[argn] == "-sz" then
	zstart = tonumber(arg[argn + 1]) - 1
	argn = argn + 1
  elseif arg[argn] == "-ez" then
	zend = tonumber(arg[argn + 1]) - 1
	argn = argn + 1
  else
	print("Unrecognised argument: " .. arg[argn])
	print("Usage: sdbuild <radius> [-c] [-sz <start layer>] [-ez <end layer>]")
	os.exit(1)
  end
end

(Please note - the above code is NOT the complete program … it's just a chunk I lifted out).

When attempting to run the program with the command line

sdbuild 24 -sz 24

the result is:

Unrecognized argument: 24Usage: sdbuild <radius> [-c] [-sz<start layer>] [-ez <end layer>]sdbuild:218: attempt to call nil

My reading of the code makes me think that it has no means by which to handle the numeric values.

Is this correct and if so, how can I correct the problem.


Thanks
Lyqyd #2
Posted 12 May 2013 - 04:56 PM
Split into new topic.
KaoS #3
Posted 12 May 2013 - 06:30 PM
replace
else
with
elseif type(arg[argn])~="number" then

EDIT: oh and remove the whole argn=argn+1 thing, that doesn't work in a for loop
Edited on 12 May 2013 - 04:33 PM
Scotty #4
Posted 12 May 2013 - 11:34 PM
Thanks for the help guys.

I've managed to get it working - although I'm sure that what I've come up with is ugly as sin, if it works it's good enough. :D/>