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
Posted 27 June 2013 - 05:25 AM
al arg1 arg2
Posted 27 June 2013 - 05:55 AM
local tArgs = { ... }
for k, v in pairs( tArgs ) do
print( v )
end
Posted 27 June 2013 - 06:07 AM
Posted 27 June 2013 - 06:37 AM
Freack100, on 27 June 2013 - 06:07 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()," ")))
Posted 27 June 2013 - 06:43 AM
Freack100, on 27 June 2013 - 06:07 AM, said:
local tArgs = { ... }
for k,v in pairs( tArgs ) do
print(k)
end
--> hi
--> Im
--> Engineer
tArgs[1] --> hi
tArgs[2] --> Im
tArgs[3] --> Engineer
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
shell.run( read():lower() )It should work properly
Posted 27 June 2013 - 06:44 AM
Freack100, on 27 June 2013 - 06:41 AM, said:
Jarle212, 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()," ")))
Posted 27 June 2013 - 06:48 AM
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
Jarle212, 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()," ")))
local function split( str, patt )
local t = {}
for s in str:gmatch("[^"..patt.."]+") do
t[#t+1] = s
end
return t
end
Posted 27 June 2013 - 06:52 AM
theoriginalbit, on 27 June 2013 - 06:48 AM, said:
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
Jarle212, 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()," ")))
local function split( str, patt )
local t = {}
for s in str:gmatch("[^"..patt.."]+") do
t[#t+1] = s
end
return t
end
Posted 27 June 2013 - 07:36 AM
Posted 27 June 2013 - 09:47 AM
Posted 27 June 2013 - 10:18 AM
Posted 01 July 2013 - 08:42 AM
Engineer, on 27 June 2013 - 05:55 AM, said:
local tArgs = { ... }
for k, v in pairs( tArgs ) do
print( v )
end
Posted 01 July 2013 - 08:54 AM
0099, on 01 July 2013 - 08:42 AM, said:
Engineer, on 27 June 2013 - 05:55 AM, said:
local tArgs = { ... }
for k, v in pairs( tArgs ) do
print( v )
end
Posted 01 July 2013 - 11:38 AM
Posted 01 July 2013 - 06:26 PM
0099, on 01 July 2013 - 08:42 AM, said:
local t = {1,7,5,7,4,3,7,9}
for k,v in pairs(t) do
print('Index: '..k..' Value: '..v)
end
0 members, 1 guests, 0 anonymous users