luacomputercraft

ComputerCraft Catch function during while loop


I've created a "false" OS in ComputerCraft that allows me to build from the ground up what users of my system can and cannot do.

One of the things that I cannot seem to do however is to have this OS give a valid throwback to whenever a "program" that does not exist is parsed.

For example, I have created programs "Help, Admin, Database" What I want is for whenever I enter something other than those 3 programs, an error such as "Program does not exist" appears.

This LUA OS was built to look as clean as possible for a command line interface, not efficiency. This OS was built over the course of 3 days so loosing my place was common and because of this some things aren't exactly parallel.

Here is my scriptOS, that runs immidiately upon starting the ComputerCraft computer

--Testing Comments--

--Program Start--
term.clear()            //This deletes the CraftOS 1.7 watermark//
term.setCursorPos(1,1)
print("KappaCraft Interface: 2018 Edition")
print("")
sleep(.75)
print("Welcome to the Information Center!")
sleep(.75)
print("")
print("This system is not yet finished")   //You're telling me//
print("")
sleep(.75)
print("Contact the Author of this program, Eleventy49, for any further questions")
sleep(2.0)

--Capabilities--
print("")
print("Type 'Commands' to see what you can do.")
sleep(.75)
print("")

--Setting up for Programs--
varyable = "true"
while varyable == "true" do
local input = read()

--Programs--
if input == "Commands" then
term.clear
term.setCursorPos(1,1)
print("This is the list of programs available to you")
print("")
print("Admin  Commands  Database  Help  Building Code")
print("")
sleep(2)
end

--Admin--
if input == "Admin" then
term.clear
term.setCursorPos(1,1)
write("Enter Password: ")
local passinput = read("*")
if passinput == "Classified" then
error("Adminstrator Access Granted")   //This error forces me back into CraftOS, so I can edit this Script//
sleep(2)
term.clear()
else
print("Must be Administrator to Access this Program")
sleep(2)
end
end

--Builder's Code--
if input == "Building Code" then
term.clear()
term.setCursorPos(1,1)
shell.run("Builder")
print("The Builder's Code has been printed on your right")  //Builder is a program that formats the building rules of the village onto computer craft printer sheets and then prints them//
end

--Help--
in input == "Help" then
term.clear()
term.setCursorPos(1,1)
print("Help with what? (EX: Help Commands)")
end

--Help Admin--
if input == "Help Admin" then
term.clear
term.setCursorPos(1,1)
print("This program lets Admins change settings")
end

--Help Commands--
if input == "Help Commands" then
term.clear
term.setCursorPos(1,1)
print("This program displays all the programs installed on this computer")
end

--Help Help--
if input == "Help Help" then
term.clear
term.setCursorPos(1,1)
print("This program explains what other programs do")

--Help Database--
if input == "Help Database" then
term.clear
term.setCursorPos(1,1)
print("This program lists all the plots and their owners in the city")
end

--Help Building Code--
if input== "Help Building Code" then
term.clear
term.setCursorPos(1,1)
print("This program prints the Building Code using the printer")
end

--Help Shutdown--
if input == "Shutdown" then
term.clear
term.setCursorPos(1,1)
print("This program shuts down the computer")
end

--Database--
if input == "Database" then
term.clear
term.setCursorPos(1,1)
print("This program is not yet complete")
end

--Shutdown--
if input == "Shutdown" then
term.clear
term.setCursorPos(1,1)
print("As you wish")
sleep(1)
os.shutdown()
end

end

Solution

  • Instead of using all if blocks, use if/elseif/else blocks. See here. So, you can use if and elseif for all of your accepted commands, and then use a final else that prints an error message.