This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
JstuffJr's profile picture

Passing Arbitrary Number of Arguments Through shell.run()

Started by JstuffJr, 03 April 2014 - 12:05 AM
JstuffJr #1
Posted 03 April 2014 - 02:05 AM
So I'm trying to design all my programs around the concept of being pulled from pastebin whenever they are run. So for example, my "Excavate" program that I wrote on the in-game computer would really just simply pull my real excavate program from pastebin, and then run it.

Here is my actual code so far:
On-Computer Excavate Program:

--Get Command-Line Arguments
local tArgs = {...}

--Delete Old File
if(fs.exists("toRun")) then
  fs.delete("toRun")
end

--Get New One
shell.run("pastebin","get","VT2WRccM","toRun")

--Run New File, passing tArgs from earlier
shell.run("toRun", tArgs)

The Actual On-Pastebin Excavate File (which for now is just printing to the console):

--Get Command-Line Arguments
local tArgs = {...}

--print each argument
for i=1, #tArgs do
		print(tArgs[i])
end

As you perhaps can already tell, the main thing I am trying to do here is be capable of passing an arbitrary number of arguments from the command line into my on-computer excavate program, and then have the on-computer program pass all of those arguments into the downloaded pastebin program.

The problem is that apparently shell.run can only take an explicitly defined number of string arguments, and cannot, for example, pass a table like I'm trying to do above.

Does anyone know of a way to pass an arbitrary or unlimited amount of arguments through shell.run()?
Lyqyd #2
Posted 03 April 2014 - 02:42 AM
The shell.run function will take any amount of arguments you want to give it. A single argument, many arguments, it doesn't matter. It takes all of the arguments, squishes them into a single string, and then picks out the arguments to pass to the program from there. You can pass it a single string and it will work just fine:


shell.run("pastebin get VT2WRccM toRun")

This means that you don't have to break up the arguments if you get them in a single string, you can just pass the arguments in all together. If you have them in a table, table.concat(argsTable, " ") can concatenate them into a single string with a space between each value in the table, or you could pass each argument independently:


shell.run(unpack(argsTable))

There's nothing terribly difficult about shell.run, but people do like to make it more complex than it needs to be. :)/>
apemanzilla #3
Posted 03 April 2014 - 05:48 AM
The shell.run function will take any amount of arguments you want to give it. A single argument, many arguments, it doesn't matter. It takes all of the arguments, squishes them into a single string, and then picks out the arguments to pass to the program from there. You can pass it a single string and it will work just fine:


shell.run("pastebin get VT2WRccM toRun")

This means that you don't have to break up the arguments if you get them in a single string, you can just pass the arguments in all together. If you have them in a table, table.concat(argsTable, " ") can concatenate them into a single string with a space between each value in the table, or you could pass each argument independently:


shell.run(unpack(argsTable))

There's nothing terribly difficult about shell.run, but people do like to make it more complex than it needs to be. :)/>/>
I think people tend to get caught up when they see it documented as "shell.run(program, arguments)" instead of something like "shell.run(command)" or similar.
Lyqyd #4
Posted 03 April 2014 - 04:02 PM
Amusingly enough, that's what I changed it to yesterday. I also added a note about how it works to the shell API wiki page. I still need to take a look at the shell.run page itself, of course.
6677 #5
Posted 03 April 2014 - 04:16 PM
shell.run("chat host room")
shell.run("chat host", "room")
shell.run("chat", "host room")
shell.run("chat", "host", "room")

All equally valid and do the same thing.

I only discovered that recently though. I had been doing the 3rd option from the above list for quite some time.
Edited on 03 April 2014 - 02:17 PM
JstuffJr #6
Posted 03 April 2014 - 05:36 PM
This means that you don't have to break up the arguments if you get them in a single string, you can just pass the arguments in all together. If you have them in a table, table.concat(argsTable, " ") can concatenate them into a single string with a space between each value in the table, or you could pass each argument independently:


shell.run(unpack(argsTable))
Exactly what I was looking for. Thanks!

I think people tend to get caught up when they see it documented as "shell.run(program, arguments)" instead of something like "shell.run(command)" or similar.

Yes, that seems to be a much better way to think about it