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

Detecting Args?

Started by H4X0RZ, 27 June 2013 - 04:25 AM
H4X0RZ #1
Posted 27 June 2013 - 06:25 AM
Hello folks,
I want to make a little command line program, but it fails when I try to run a program with args!
How do I detect args in a string like this:

al arg1 arg2
Engineer #2
Posted 27 June 2013 - 06:55 AM
I really dont get what you are asking, but if want to catch arguments when you run the program:


local tArgs = { ... }
for k, v in pairs( tArgs ) do
   print( v )
end
H4X0RZ #3
Posted 27 June 2013 - 07:07 AM
I get userinput and want to seperate it, so I can run a program with the args the user has typed.

Because
shell.run(read()) not works and I dont want to have multiple reads for one program.
Jarle212 #4
Posted 27 June 2013 - 07:37 AM
I get userinput and want to seperate it, so I can run a program with the args the user has typed.

Because
shell.run(read()) not works and I dont want to have multiple reads for one program.

Try this:

local function split(strI,regEX)
str = string.gsub(strI, regEX, " ")
str = str .. " "
local last = 1
local strTable = {}
for i=1, str:len() do
  if str:sub(i,i) == " " then
   local subStr = str:sub(last, i - 1)
   table.insert(strTable, subStr)
   last = i + 1
  end
end
return strTable
end

shell.run(unpack(split(read()," ")))


H4X0RZ #5
Posted 27 June 2013 - 07:41 AM
I get userinput and want to seperate it, so I can run a program with the args the user has typed.

Because
shell.run(read()) not works and I dont want to have multiple reads for one program.

Try this:
There is nothing…

If anyone don't understand what I mean.
Look at CraftOS. You can enter programs with args, how?
Engineer #6
Posted 27 June 2013 - 07:43 AM
I get userinput and want to seperate it, so I can run a program with the args the user has typed.

Because
shell.run(read()) not works and I dont want to have multiple reads for one program.
uhm… Using this code it and we run `program hi Im Engineer`:


local tArgs = { ... }
for k,v in pairs( tArgs ) do
   print(k)
end
--> hi
--> Im
--> Engineer
tArgs[1] --> hi
tArgs[2] --> Im
tArgs[3] --> Engineer

Edit: OH, I get it!


local function runProg()
	 local prog = read()
	 local tProg = {}
	 for word in prog:gmatch("[^%s]+") do
		tProg[#tProg + 1] = word
	 end
	 shell.run( unpack( tProg ))
end

EDIT: Actually, if you do

shell.run( read():lower() )
It should work properly
Jarle212 #7
Posted 27 June 2013 - 07:44 AM
I get userinput and want to seperate it, so I can run a program with the args the user has typed.

Because
shell.run(read()) not works and I dont want to have multiple reads for one program.

Try this:
There is nothing…

If anyone don't understand what I mean.
Look at CraftOS. You can enter programs with args, how?


Yeah fixed it now.


local function split(strI,regEX)
str = string.gsub(strI, regEX, " ")
str = str .. " "
local last = 1
local strTable = {}
for i=1, str:len() do
  if str:sub(i,i) == " " then
   local subStr = str:sub(last, i - 1)
   table.insert(strTable, subStr)
   last = i + 1
  end
end
return strTable
end

shell.run(unpack(split(read()," ")))
theoriginalbit #8
Posted 27 June 2013 - 07:48 AM
This is how the ComputerCraft devs do it in the shell program.


local function runLine( _sLine )
  local tWords = {}
  for match in string.gmatch( _sLine, "[^ \t]+" ) do
	table.insert( tWords, match )
  end
  local sCommand = tWords[1]
  if sCommand then
	return run( sCommand, unpack( tWords, 2 ) )
  end
  return false
end


Try this:

local function split(strI,regEX)
str = string.gsub(strI, regEX, " ")
str = str .. " "
local last = 1
local strTable = {}
for i=1, str:len() do
  if str:sub(i,i) == " " then
   local subStr = str:sub(last, i - 1)
   table.insert(strTable, subStr)
   last = i + 1
  end
end
return strTable
end

shell.run(unpack(split(read()," ")))
This is how i'd do it, much more efficient split function

local function split( str, patt )
  local t = {}
  for s in str:gmatch("[^"..patt.."]+") do
	t[#t+1] = s
  end
  return t
end
Jarle212 #9
Posted 27 June 2013 - 07:52 AM
This is how the ComputerCraft devs do it in the shell program.


local function runLine( _sLine )
  local tWords = {}
  for match in string.gmatch( _sLine, "[^ \t]+" ) do
	table.insert( tWords, match )
  end
  local sCommand = tWords[1]
  if sCommand then
	return run( sCommand, unpack( tWords, 2 ) )
  end
  return false
end


Try this:

local function split(strI,regEX)
str = string.gsub(strI, regEX, " ")
str = str .. " "
local last = 1
local strTable = {}
for i=1, str:len() do
  if str:sub(i,i) == " " then
   local subStr = str:sub(last, i - 1)
   table.insert(strTable, subStr)
   last = i + 1
  end
end
return strTable
end

shell.run(unpack(split(read()," ")))
This is how i'd do it, much more efficient split function

local function split( str, patt )
  local t = {}
  for s in str:gmatch("[^"..patt.."]+") do
	t[#t+1] = s
  end
  return t
end

Didn't know you could do it that way, a lot more efficient and compact :)/>

Edit: I am not very good at regEx stuff :P/>
H4X0RZ #10
Posted 27 June 2013 - 08:36 AM
Thank you so much guys!
Jarle212 #11
Posted 27 June 2013 - 10:47 AM
NP :)/>
Lyqyd #12
Posted 27 June 2013 - 11:18 AM
If you're really using shell.run, you can literally just do shell.run(read()).
0099 #13
Posted 01 July 2013 - 09:42 AM
I really dont get what you are asking, but if want to catch arguments when you run the program:


local tArgs = { ... }
for k, v in pairs( tArgs ) do
   print( v )
end
Maybe ipairs()? Order of table elements is undefined when using pairs(). But ipairs() gets tArgs[1], then [2] and so on integer indexes until it gets nil value.
Engineer #14
Posted 01 July 2013 - 09:54 AM
I really dont get what you are asking, but if want to catch arguments when you run the program:


local tArgs = { ... }
for k, v in pairs( tArgs ) do
   print( v )
end
Maybe ipairs()? Order of table elements is undefined when using pairs(). But ipairs() gets tArgs[1], then [2] and so on integer indexes until it gets nil value.
Its pretty much the same, since that table has only indexed keys. And to add to that, ipairs is pretty much deprecated. At least, that is what me is told
Lyqyd #15
Posted 01 July 2013 - 12:38 PM
ipairs is not deprecated. It was, in fact, expanded upon in Lua 5.2.
theoriginalbit #16
Posted 01 July 2013 - 07:26 PM
Maybe ipairs()? Order of table elements is undefined when using pairs(). But ipairs() gets tArgs[1], then [2] and so on integer indexes until it gets nil value.
It doesn't matter, in both pairs and ipairs all indexed values are returned in order, it is just the keyed values that return in a different order in pairs. Don't believe me, test it.


local t = {1,7,5,7,4,3,7,9}
for k,v in pairs(t) do
  print('Index: '..k..' Value: '..v)
end