From: sto.mar@... Date: 2012-08-04T03:51:12+09:00 Subject: Re: OptionParser error handling (InvalidArgument) Maybe you were looking for something like this: ----- require 'optparse' options = { :no => 0 } opts = OptionParser.new do |opts| opts.on('-n', '--no N', "Number of... Default value: #{options[:no]}") do |no| if /\A[+]?\d+\Z/ === no # nonnegative integer? options[:no] = no else warn "#{no} is not a nonnegative integer, using default value" end end end opts.parse!(ARGV) p options ----- $ ruby optparse.rb -n -5 -5 is not a nonnegative integer, using default value {:no=>0} $ ruby optparse.rb -n 3 {:no=>"3"} Am 03.08.2012 18:03, schrieb Panagiotis Atmatzidis: > ---------------------------------------------------- > # 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 > ---------------------------------------------------- Personally, I do not like provoking an exception only to test whether no is an integer. Also, the 'else' part from begin-rescue-else is useless. --