Hello,
Thanks for the reply.
It is not clear to me what your problem is.
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.
Slightly adapted from the post cited by you:
-----
require 'optparse'
options = {}
opts = OptionParser.new do |opts|
# Cast 'no' argument to an Integer.
opts.on('-n', '--no N', Integer, 'Number of...') do |no|
options[:no] = no
end
end
opts.parse!(ARGV)
p options
-----
results (as it should) in
$ ruby optparse.rb -n 34
{:no=>34}
$ ruby optparse.rb -n duck
optparse.rb:12:in `<main>': invalid argument: -n duck (OptionParser::InvalidArgument)
----------------------------------------------------
# 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
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?
----------------------------------------------------
but didn't work. The error I get from cli is:
f2bread.rb:483:in `<main>': 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
----------------------------------------------------
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".
[1] http://blog.segment7.net/2008/01/05/optionparser-argument-casting
--
<https://github.com/stomar/>
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 How to test if a string is basically an integer in quotes using Ruby - Stack Overflow
Best Regards
Panagiotis Atmatzidis
···
On 3 Αυγ 2012, at 21:21 , sto.mar@web.de wrote:
Am 03.08.2012 18:03, schrieb 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."