Jump to content




Detecting Args?


  • You cannot reply to this topic
15 replies to this topic

#1 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 27 June 2013 - 05: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


#2 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 27 June 2013 - 05: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


#3 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 27 June 2013 - 06: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.

#4 Jarle212

  • Members
  • 198 posts
  • LocationNorway

Posted 27 June 2013 - 06:37 AM

View PostFreack100, on 27 June 2013 - 06:07 AM, said:

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()," ")))




#5 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 27 June 2013 - 06:41 AM

View PostJarle212, on 27 June 2013 - 06:37 AM, said:

View PostFreack100, on 27 June 2013 - 06:07 AM, said:

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?

#6 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 27 June 2013 - 06:43 AM

View PostFreack100, on 27 June 2013 - 06:07 AM, said:

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

#7 Jarle212

  • Members
  • 198 posts
  • LocationNorway

Posted 27 June 2013 - 06:44 AM

View PostFreack100, on 27 June 2013 - 06:41 AM, said:

View PostJarle212, on 27 June 2013 - 06:37 AM, said:

View PostFreack100, on 27 June 2013 - 06:07 AM, said:

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.

View PostJarle212, on 27 June 2013 - 06:37 AM, said:

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()," ")))


#8 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 27 June 2013 - 06: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


View PostJarle212, on 27 June 2013 - 06:37 AM, said:

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


#9 Jarle212

  • Members
  • 198 posts
  • LocationNorway

Posted 27 June 2013 - 06:52 AM

View Posttheoriginalbit, on 27 June 2013 - 06:48 AM, said:

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


View PostJarle212, on 27 June 2013 - 06:37 AM, said:

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

#10 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 27 June 2013 - 07:36 AM

Thank you so much guys!

#11 Jarle212

  • Members
  • 198 posts
  • LocationNorway

Posted 27 June 2013 - 09:47 AM

NP :)

#12 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 27 June 2013 - 10:18 AM

If you're really using shell.run, you can literally just do shell.run(read()).

#13 0099

  • Members
  • 52 posts

Posted 01 July 2013 - 08:42 AM

View PostEngineer, on 27 June 2013 - 05:55 AM, said:

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.

#14 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 01 July 2013 - 08:54 AM

View Post0099, on 01 July 2013 - 08:42 AM, said:

View PostEngineer, on 27 June 2013 - 05:55 AM, said:

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

#15 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 01 July 2013 - 11:38 AM

ipairs is not deprecated. It was, in fact, expanded upon in Lua 5.2.

#16 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 01 July 2013 - 06:26 PM

View Post0099, on 01 July 2013 - 08:42 AM, said:

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






2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users