Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Fetch the arguments passed to the program
- local args = { ... }
- -- Check if an argument was provided
- if #args == 0 then
- print("Usage: turtle <command> [arguments]")
- return
- end
- -- The first argument is the function name (e.g., "up", "place")
- local command = args[1]
- local apiFunction = turtle[command]
- -- Check if the command actually exists in the turtle API and is a function
- if type(apiFunction) == "function" then
- -- Prepare any extra arguments (like the '2' in "turtle select 2")
- local functionArgs = {}
- for i = 2, #args do
- -- Convert to numbers where possible, since shell arguments are always strings
- local num = tonumber(args[i])
- if num then
- table.insert(functionArgs, num)
- else
- table.insert(functionArgs, args[i])
- end
- end
- -- Execute the function safely using pcall (protected call) and unpack
- local success, result, errorMsg = pcall(apiFunction, table.unpack(functionArgs))
- if success then
- -- Print the result of the action (true/false)
- print("Success: " .. tostring(result))
- else
- -- If something went terribly wrong with the code itself
- print("Error: " .. tostring(result))
- end
- else
- print("Invalid turtle command: " .. tostring(command))
- end
Advertisement
Add Comment
Please, Sign In to add comment