From: Panagiotis Atmatzidis Date: 2012-08-04T01:03:48+09:00 Subject: OptionParser error handling (InvalidArgument) Hello, I'd like to make an OptParser error look a bit more explanatory. I tried to deal with it outside OpiontParser like this: ---------------------------------------------------- # check if options[:no] is interger if options[:no] begin $no = Integer(options[:no]) rescue ArgumentError puts "#{$no} is not an Integer!" else true end else $no = 0 end ---------------------------------------------------- but didn't work. The error I get from cli is: f2bread.rb:483:in `
': invalid argument: -s duck (OptionParser::InvalidArgument) Apparently I need to somehow handle this from within OptionParser. Any ideas on how to proceed? I found this post[1] online which explains what I want but I couldn't figure how exactly it works. So some actual codes with a sort of explanation of where *exactly* sits on the program (outside or inside "OptionParser.new" class?). The relevant OptionParser lines, where is specified that '-n' accepts Integers, are: ---------------------------------------------------- opts.on('-n', '--no N', Integer, 'Number of top entries to be displayed. By default all entries are displayed.') do |no| options[:no] = no end ---------------------------------------------------- [1] http://blog.segment7.net/2008/01/05/optionparser-argument-casting Best Regards, Panagiotis Atmatzidis ----------------------------- Pharmacy Student at VFU email4lists: ml@convalesco.org More info: http://about.me/atmosx The wise man said: "Never argue with an idiot, he brings you down to his level and beat you with experience."