1

我试图通过将字符串中的每个单词分配给一个新变量来使用命令的(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().

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

4

2 回答 2

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
于 2014-06-07T17:01:05.863 回答
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

代码未测试,在评论中报告错误。编辑:稍微注释了代码。

于 2014-03-17T15:44:18.577 回答