small medium large xlarge

Generic-user-small
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|
		do_cleanup
  end
end

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

</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.

Dmfcb_pragsmall
20 Feb 2012, 21:21
David Copeland (487 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.

Generic-user-small
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