我试图通过将字符串中的每个单词分配给一个新变量来使用命令的(file , parameters
) 功能。shell.run()
我知道这样做的唯一方法是使用tArgs[#]
. 如果在命令行中键入程序名称及其tArgs
用于执行此操作的参数,则可以正常工作。但我想从程序中的提示中做到这一点。所以场景是:
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])"
我一直在努力解决这个问题,但无法让它发挥作用。这是我整理的一些代码。
------------------------------------------------------------
-- 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]]
并且这个结果应该像程序“cmd”中的那样,因为这是自定义 shell 所在的位置。
>echo yes
yes
>
--4.Then because functions return after completion it should loop back into function prompt().
非常感谢任何帮助,如果您有任何建议,请提供显示您如何在程序中使用它的代码。谢谢!