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

can a user-made program be used with arguments? or do I need to make a custom API for this program?

Started by digitalEntity, 21 March 2014 - 10:09 PM
digitalEntity #1
Posted 21 March 2014 - 11:09 PM
So, I'm working on a program to read the content of other files on the system and print them to the screen (or a nearby monitor, but that can be done easily from the shell lol.)

But the main thing I want the program to do is accept arguments lol. For example, the reader program might be called "zreader" and the file I'm trying to open is called "zfile" and is found on a disk (so "disk/zfile"). As soon as I boot the computer I want to be able to type in "zreader disk/zfile", and I'm wondering if/how I could do that without making a custom API, but I wouldn't be completely opposed to a custom API.

My current code is as follows


a = args{...}
if fs.exists(a) then
	 local h = fs.open(a, "r")
	 local b = h.readAll()
	 print(B)/>/>
else
	 print("File not found.")
end

Of course, I may have just been an idiot and completely miswrote the first line, and I make no claims to the contrary lol.

And as always, any helpful input would be appreciated. Thank you.

Sincerely,
digitalEntity.
CometWolf #2
Posted 21 March 2014 - 11:31 PM
you miswrote the first line, and then misunderstood how … works

local args = {...} --stores the arguments in a table called args
args{1] --first argument
args[2] --second argument
-- and so on
Bomb Bloke #3
Posted 21 March 2014 - 11:43 PM
After fixing that, you'll also want to ensure you close "h" once you're done with it.

Consider using more descriptive variable names.
digitalEntity #4
Posted 22 March 2014 - 02:34 AM
you miswrote the first line, and then misunderstood how … works
Ah, thanks.
Consider using more descriptive variable names.
yeah… that would probably be smart lol

Thanks you guys. It's working now, and with a few minor tweaks I've made, I can run the program on a monitor, and the screen will update every second to show any changes saved to the file that the program is reading.