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

Read to arguments

Started by lzRipTide3zl, 11 May 2013 - 10:12 PM
lzRipTide3zl #1
Posted 12 May 2013 - 12:12 AM
I was making a kind of console program. I wanted an on a read() to store everything into arguments.
So, basically I want it to get input from the user and store it to command and extra arguments.

I have some of the program done but I need help with the arguments.



function dir(dir)
  shell.run("cd", dir)
  return dir
end
function prompt(...)
	cmd = read()
  for i = 1, #arg do
	return arg[i]
  end
end
dir = dir("system")
while true do
cmd = prompt()
  for com, arg in pairs(cmd) do
	if com == "exit" then
	  shell.run("cd", "/")
	  break
	end
	if run == "run" then
	shell.run(arg)
	end
end
end


I know there are errors but mainly i just want to finish the the function prompt().
prompt is supposed to get input and store it to arguments with the first being the command to execute

I can handle the errors myself that is not the problem.
Grim Reaper #2
Posted 12 May 2013 - 03:51 PM
The way your prompt function is currently working will return the first element of the args table. This is because you can only return once, but you can return multiple items in Lua.

If I'm understanding your query correctly, I would go about writing the 'prompt' function by reading in a single string into the variable 'cmd' like you've already done and then split the string up by spaces and store each section into a separate element of the table args. Once you've got this table with your arguments, you could just return the table or you could use the 'unpack' function to return all of the elements of the args table without a table and use
 cmd = { prompt() } 
to capture all of the values returned in a table.

Here's some code to show you how I would do this:


local function prompt()
	-- Read all of the arguments in as a single string.
	local cmd = read()

	-- Split all of the arguments in the string in 'cmd' by spaces.
	local args = {}
	-- The "[^%s]+" pattern is used to match any sequence of non-space characters
	-- with a size of >= 1.
	for match in string.gmatch (match, "[^%s]+") do
		table.insert (args, match)
	end

	-- Return all of the arguments as a table.
	return args
end
lzRipTide3zl #3
Posted 12 May 2013 - 04:47 PM
The way your prompt function is currently working will return the first element of the args table. This is because you can only return once, but you can return multiple items in Lua.

If I'm understanding your query correctly, I would go about writing the 'prompt' function by reading in a single string into the variable 'cmd' like you've already done and then split the string up by spaces and store each section into a separate element of the table args. Once you've got this table with your arguments, you could just return the table or you could use the 'unpack' function to return all of the elements of the args table without a table and use

Do i use the #arg to use for arguments following the command?
lzRipTide3zl #4
Posted 12 May 2013 - 04:55 PM


local function prompt()
	-- Read all of the arguments in as a single string.
	local cmd = read()

	-- Split all of the arguments in the string in 'cmd' by spaces.
	local args = {}
	-- The "[^%s]+" pattern is used to match any sequence of non-space characters
	-- with a size of >= 1.
	for match in string.gmatch (match, "[^%s]+") do
		table.insert (args, match)
	end

	-- Return all of the arguments as a table.
	return args
end
Your code returns errors at line 9. says bad argument: string expected, got nil
Grim Reaper #5
Posted 19 May 2013 - 05:12 PM
Sorry, that was my fault. Here is the proper code:

local function prompt()
	    -- Read all of the arguments in as a single string.
	    local cmd = read()
	    -- Split all of the arguments in the string in 'cmd' by spaces.
	    local args = {}
	    -- The "[^%s]+" pattern is used to match any sequence of non-space characters
	    -- with a size of >= 1.
	    for match in string.gmatch (cmd, "[^%s]+") do
			    table.insert (args, match)
	    end
	    -- Return all of the arguments as a table.
	    return args
end