Reputation: 583
I am trying to use the (file , parameters
) functionality of the shell.run()
command by assigning each word in a string to a new variable. The only way I know to do this is with tArgs[#]
.
Which if at the command line and typing a name of a program and its arguments that uses tArgs
to do so will work fine. But I want to do this from a prompt within a program. So the scenario is:
1.The computer starts a program using the startup file.
2.Using the "write()" command the program asks for a command to run with parameters.
3.The user types for ex. "echo yes"
4.The program then takes the word "echo" and assigns it to "var1" and then the
word: "yes" and assigns it to "var2"
5.The program takes "var1" and "var2" and inputs it to the "shell.run()" command in the
format: shell.run((var1),(var2))
6.The "shell.run()" calls a program named "echo" which is set up to allow
for parameters to be entered without a prompt by using the "tArgs = {...}" command
and the "echo" program sees that it is getting an argument "yes" and runs the
command: "print(tArgs[1])"
I have been working furiously trying to figure this out but cannot get it to work. Here is some code I have put together.
------------------------------------------------------------
-- 1.At the CraftOS the startup file runs a program "cmd"
[[Program: startup]]
shell.run("cmd")
[[end of startup]]
--2.Runs program "cmd"
[[Program: cmd]]
-- Now in cmd
term.clear()
term.setCursorPos(1,1)
function prompt()
write(">") --assuming user typed: echo yes
tArgs = {...}
file = tArgs[1] --will get the word "echo"
param1 = tArgs[2] --will get the word "yes"
if #tArgs < 1 then
print(Syntax: <command> <parameters>)
prompt()
else
print()
shell.run((file), (param1)) --resulting command is shell.run("echo","yes")
print()
prompt()
end
end
prompt()
[[end of cmd]]
--3.Runs the program "echo" because it was the first word in our command.
[[Program: echo]]
tArgs = {...}
param = tArgs[1]
print(param) --prints "yes" which is what the "shell.run((file), (param1)) is referring to.
[[end of echo]]
And the result of this should be like so in program "cmd" as this is where the custom shell is.
>echo yes
yes
>
--4.Then because functions return after completion it should loop back into function prompt().
Any help is greatly appreciated, if you have any advice please supply code that shows how you are using it in a program. Thank you!
Upvotes: 1
Views: 12314
Reputation: 2185
It appears you want to turn the user's input "foo bar"
into shell.run("foo", "bar")
. If so, you want to split the "foo bar"
string into a table, then unpack that table:
function split(a)
local result = {}
for i in a:gmatch("%a+") do
result[#result + 1] = i
end
return result
end
shell.run(unpack(split(read())))
Also the main issue in the previous answer is the use of the word break
as a function name. You can't use that as a variable (or function) name becuase break
is an actual statement that will end the current for
or while
loop in which it is called. Example:
for i = 1, 10 do
if i = 5 then
break
end
end
Upvotes: 1
Reputation: 155
In the cmd file, prompt() relies on the arguments passed to the program because
tArgs={...}
is equivalent to tArgs=arguments
. However, there aren't passed any parameters to the cmd program. In order to get input, use read()
. Then, you of course still have to break that down in multiple words.
For that, you might try this code (returns a table):
function break(str)
--Define variables; ret is the output, j is the current index.
local ret={""}
local j=1
--Loop through the string
for i=1,#str do
--Extract the current character
local char=string.sub(str,i,i)
--Check if it is a space and the previous item was not a space, too.
if char==" " and ret[j]~="" then
--It is whitespace; move on forward!
j=j+1
ret[j]=""
else
--No whitespace; append it to the string
ret[j]=ret[j]..char
end
end
return ret
end
Code not tested, report bugs in the comments. EDIT: Commented the code a bit.
Upvotes: 0