ComputerCraft:在程序中使用shell.run()命令

时间:2014-03-16 05:14:58

标签: shell lua minecraft computercraft

我试图使用(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]]

结果应该是这样的程序&#34; cmd&#34;因为这是自定义shell所在的位置。

>echo yes

yes

>

--4.Then because functions return after completion it should loop back into function prompt().

非常感谢任何帮助,如果您有任何建议,请提供显示您如何在程序中使用它的代码。 谢谢!

2 个答案:

答案 0 :(得分:1)

您似乎想要将用户的输入"foo bar"变为shell.run("foo", "bar")。如果是这样,您希望将"foo bar"字符串拆分为表,然后解压缩该表:

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

上一个答案中的主要问题是使用单词break作为函数名称。您不能将其用作变量(或函数)名称,因为break是一个实际语句,它将结束调用它的当前forwhile循环。例如:

for i = 1, 10 do
    if i = 5 then
        break
    end
end

答案 1 :(得分:0)

在cmd文件中,prompt()依赖于传递给程序的参数,因为 tArgs={...}相当于tArgs=arguments。但是,没有将任何参数传递给cmd程序。要获得输入,请使用read()。那么,你当然还是要用多个词来打破它。 为此,您可以尝试此代码(返回一个表):

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

代码未经过测试,报告评论中的错误。 编辑:稍微评论一下代码。