1

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!

cmd
  • 563
  • 3
  • 10
  • 26

2 Answers2

1

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
Potassium Ion
  • 2,075
  • 1
  • 22
  • 39
0

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.

scheurneus
  • 155
  • 5
  • its giving an error, attempt to get length of a function – cmd Mar 19 '14 at 21:49
  • also, could you show the loop running and then filling in the shell.run() command? I don't fully understand how your loop is separating anything and adding it to a new variable – cmd Mar 19 '14 at 21:50
  • @cmd What code do you run this function with? Perhaps break(read)? Try break(read()) in that case. – scheurneus Mar 21 '14 at 06:44
  • all of my code is in the question, if you could please use your example to make a working version it'd be much appreciated as your code seems to not separate each word into a new variable but gets the length of the function `break(str)` – cmd Mar 22 '14 at 22:35