文字列内の各単語を新しい変数に割り当てること(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」のようになるはずです。これは、カスタム シェルがある場所だからです。
>echo yes
yes
>
--4.Then because functions return after completion it should loop back into function prompt().
アドバイスがあれば、プログラムでどのように使用しているかを示すコードを提供してください。ありがとうございました!