small medium large xlarge

20 Feb 2012, 06:21
Taro Fukunaga (8 posts)

I’m running into a problem where I have trouble getting user input. I’ve built a sample app using GLI and defined the following:

desc 'Do the cleanup'
command :run do |c|
  c.action do |global_options,options,args|

def do_cleanup
	puts "Do you really want to cleanup? (y/N)"
	response = gets.chomp
	puts "The response was: #{response}"

</code> When I run my app I get the following:

$ ./cleanup run Do you really want to cleanup? (y/N) error: No such file or directory - run

I never get a chance to type y or N and the program just exits.

20 Feb 2012, 21:21
David Copeland (491 posts)

It seems that @gets@ reads from “files” that it finds in ARGV, and it seems GLI leaves the command in ARGV (I believe GLI makes a copy of ARGV and leaves the original alone). Really strange behavior, but you can get around it by doing @STDIN.gets@ to explicitly read from the standard input.

21 Feb 2012, 03:25
Taro Fukunaga (8 posts)

Hi David,

Thanks for your response again. You are right that if I do

~~~ response = STDIN.gets.chomp ~~~

I have a chance to type in y or N.

Also based on your response, I found that the following works just as well:

~~~ ARGV.shift response = gets.chomp ~~~

You must be logged in to comment