Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua - get command line input from user?

In my lua program, i want to stop and ask user for confirmation before proceeding with an operation. I'm not sure how to stop and wait for user input, how can it be done?

like image 798
RCIX Avatar asked Nov 29 '09 10:11

RCIX


People also ask

What is Lua command line?

DESCRIPTION. lua is the stand-alone Lua interpreter. It loads and executes Lua programs, either in textual source form or in precompiled binary form. (Precompiled binaries are output by luac, the Lua compiler.) lua can be used as a batch interpreter and also interactively.

What is IO read in Lua?

The call io. read("*all") reads the whole current input file, starting at its current position. If we are at the end of file, or if the file is empty, the call returns an empty string.

How do you read Lua?

Software developers typically open and modify LUA files with source code editors, such as Microsoft Visual Studio Code and MacroMates TextMate. Plain text editors, which include Microsoft Notepad and Apple TextEdit, may also be used to open and modify LUA files.


3 Answers

local answer
repeat
   io.write("continue with this operation (y/n)? ")
   io.flush()
   answer=io.read()
until answer=="y" or answer=="n"
like image 200
lhf Avatar answered Oct 01 '22 12:10

lhf


Take a look at the io library, which by default has standard-input as the default input file:

http://www.lua.org/pil/21.1.html

like image 34
Amber Avatar answered Oct 01 '22 14:10

Amber


I've worked with code like this. I will type this in a way it will work:

io.write("continue with this operation (y/n)?")
answer=io.read()
if answer=="y" then
   --(put what you want it to do if you say y here)
elseif answer=="n" then
   --(put what you want to happen if you say n)
end
like image 24
Scythe Avatar answered Oct 01 '22 13:10

Scythe