Jump to content




Question regarding read()


5 replies to this topic

#1 Justinjah91

  • Members
  • 12 posts

Posted 05 October 2018 - 01:44 PM

Suppose you have a program that requires a user input at the beginning via the read() command. Now, suppose I want to make a wrapper script that will automatically repeat this program. Is there a way to have my wrapper program pass inputs through that read() command?

I should clarify that the best way to approach this is probably just to set up the program so it can take either arguments OR a user input through read(), but I'm just curious if this is possible.

Thanks!

#2 EveryOS

    Resident Necromancer

  • Members
  • 566 posts
  • LocationBOO!!

Posted 05 October 2018 - 02:02 PM

Do you mean like this:
local args = {...}
local oRead = read
local function read(...)
  if args[1] then
    return unpack(args)
  else
    return oRead(...)
  end
end


#3 Justinjah91

  • Members
  • 12 posts

Posted 05 October 2018 - 02:17 PM

View PostEveryOS, on 05 October 2018 - 02:02 PM, said:

Do you mean like this:
local args = {...}
local oRead = read
local function read(...)
  if args[1] then
	return unpack(args)
  else
	return oRead(...)
  end
end

More like this:

write('Start mining? ')
local input = read()

if input == "y" then
  Do stuff
else
  Dont do stuff
end

I want to know if there is a way to set up a wrapper script for this program to make it repeat forever with an input of "y". Again, I know this is trivial using arguments. I'm just wondering if a program can enter a read() input for the user (just out of curiosity, I would never actually do that)

#4 EveryOS

    Resident Necromancer

  • Members
  • 566 posts
  • LocationBOO!!

Posted 05 October 2018 - 02:44 PM

So like
local read = function()
  return "y"
end

while true do
  write('Start mining? ')
  local input = read()

  if input == "y" then
    Do stuff
  else
    Dont do stuff
  end
end


#5 Luca_S

  • Members
  • 386 posts
  • LocationGermany

Posted 05 October 2018 - 09:15 PM

Another option would be to queue a char and key event and then call the next program while being careful not to yield. But that only works if the program you are trying to call does not yield(call os.pullEvent, os.pullEventRaw or coroutine.yield) before calling read():

while true do
  os.queueEvent("char", "y")
  os.queueEvent("key", keys.y)
  os.queueEvent("key", keys.enter)
  shell.run("YourProgram")
end


#6 Justinjah91

  • Members
  • 12 posts

Posted 06 October 2018 - 03:21 AM

View PostLuca_S, on 05 October 2018 - 09:15 PM, said:

Another option would be to queue a char and key event and then call the next program while being careful not to yield. But that only works if the program you are trying to call does not yield(call os.pullEvent, os.pullEventRaw or coroutine.yield) before calling read():

while true do
  os.queueEvent("char", "y")
  os.queueEvent("key", keys.y)
  os.queueEvent("key", keys.enter)
  shell.run("YourProgram")
end

Ah yes, this is what I was curious about. So you can sort of emulate user inputs with os.queueEvent()... Good to know!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users