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
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 A.:
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.
I dislike the way the error is displayed. I’d like to print a message
like the one after the ‘rescue’ keyword. Maybe it’s not a good practice
though and I should leave it as is.
options[:no] = no
$ ruby optparse.rb -n 34
if options[:no]
This is not necessary, since the option value has already been
cast to an integer, or an exception has been raised.
Btw, you should not use global variables.
Why not?
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
actually, it did work. “duck” is not an integer,
so an exception is raised.
I just want to print a predefined error message. It seems more easy to
read to me, although as I said above, it might not be a good practice
and I’m not sure anymore if I should add it or no given the fact
that you’re the second person pointing out that “it’s working as it
should”.
A bit off-topic now. In your next email you told me that you dislike
raising an exception in order to identify an integer. A second approach
I found online was using regexp. However this was the answer I got from
a user at ‘stackoverflow’ which convinced that this is the right way to
go in ruby:
"Sarah: you can use a Regex but in order to handle all the cases that
Ruby does when parsing integers (negative numbers, hex, octal,
underscores e.g. 1_000_000) it would be a very big Regex and easy to get
wrong. Integer() is canonical because with Integer ()you know for sure
that anything that Ruby considers an integer literal will be accepted,
and everything else will be rejected. Duplicating what the language
already gives you is arguably a worse code smell than using exceptions
for control. – Avdi " from
A bit off-topic now. In your next email you told me that you dislike
raising an exception in order to identify an integer. A second
approach I found online was using regexp.
That’s what I did in my version, too.
exceptions for control. – Avdi " from
Imo there is no “canonical” way that fits all use cases.
Here, in connection with command line arguments, I prefer
using a regexp (probably as a second choice after using the Integer
cast from optparse): I do not expect an octal integer as user input,
the regexp is rather simple, and the approach is flexible.
I can for example test for integers or for nonnegative integers, with
/\A[±]?\d+\Z/ === str or /\A[+]?\d+\Z/ === str
Partly a matter of taste.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.