[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Handling invalid options with OptionParser

Adam Penny

10/18/2008 6:21:00 PM

Hi there,

I wrote my own Wake on Lan Script in ruby that uses a Plist to look up
either target Mac address by selected printer or target MAC address by
server and I use option parser for this.

It all works fine if the arguments are input correctly, but if the
arguments are off you get something like this. -x is not declared in my
options and was a test to see how it did when it got something that it
didn't recognize.

ruby test.rb -x
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/optparse.rb:1443:in
`complete': invalid option: -x (OptionParser::InvalidOption)
from
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/optparse.rb:1441:in
`catch'
from
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/optparse.rb:1441:in
`complete'
from
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/optparse.rb:1280:in
`parse_in_order'
from
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/optparse.rb:1247:in
`catch'
from
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/optparse.rb:1247:in
`parse_in_order'
from
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/optparse.rb:1241:in
`order!'
from
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/optparse.rb:1332:in
`permute!'
from
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/optparse.rb:1353:in
`parse!'
from test.rb:4

It's not the end of the world as if the options are wrong then it won't
do anything anyway, but I was wondering if anyone could tell me if
there's any method in the class or extra thing I could do to catch dodgy
arguments and output the help file or the banner rather than this
inelegant list of errors?

Thanks in advance.
--
Posted via http://www.ruby-....

3 Answers

Stefano Crocco

10/18/2008 6:33:00 PM

0

Alle Saturday 18 October 2008, Adam Penny ha scritto:
> Hi there,
>
> I wrote my own Wake on Lan Script in ruby that uses a Plist to look up
> either target Mac address by selected printer or target MAC address by
> server and I use option parser for this.
>
> It all works fine if the arguments are input correctly, but if the
> arguments are off you get something like this. -x is not declared in my
> options and was a test to see how it did when it got something that it
> didn't recognize.
>
> ruby test.rb -x
> /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/opt
>parse.rb:1443:in `complete': invalid option: -x
> (OptionParser::InvalidOption)
> from
> /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/opt
>parse.rb:1441:in `catch'
> from
> /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/opt
>parse.rb:1441:in `complete'
> from
> /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/opt
>parse.rb:1280:in `parse_in_order'
> from
> /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/opt
>parse.rb:1247:in `catch'
> from
> /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/opt
>parse.rb:1247:in `parse_in_order'
> from
> /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/opt
>parse.rb:1241:in `order!'
> from
> /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/opt
>parse.rb:1332:in `permute!'
> from
> /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/opt
>parse.rb:1353:in `parse!'
> from test.rb:4
>
> It's not the end of the world as if the options are wrong then it won't
> do anything anyway, but I was wondering if anyone could tell me if
> there's any method in the class or extra thing I could do to catch dodgy
> arguments and output the help file or the banner rather than this
> inelegant list of errors?
>
> Thanks in advance.

I guess you can enclose the call to OptionParser#parse! in a begin/rescue
expression, rescuing OptionParser::InvalidOption or OptionParser::ParseError
(which is the parent of InvalidOption) and display an error message. Here's a
simple example of how you can do it:

require 'optparse'

o = OptionParser.new do |o|
o.banner = "test usage: test [OPTIONS]"
o.on('-o', '--option', 'this option does nothing '){}
end

begin o.parse! ARGV
rescue OptionParser::InvalidOption => e
puts e
puts o
exit 1
end

the line

puts e

will display a message like

invalid option -a

(where -a is the invalid option passed to the script). The line

puts o

will display the usage message.

exit 1

will make the script exit with the error code 1.

I hope this helps

Stefano

Adam Penny

10/18/2008 6:39:00 PM

0

Stefano, that's a very big help, thank you very much!

Adam

--
Posted via http://www.ruby-....

Adam Penny

10/18/2008 6:49:00 PM

0

PennyMBP:~ adam$ wopol -l
invalid option: -l
Usage: wopol [options]
-s, --server SERVERNAME
-p, --printer PRINTERNAME
-w, --wait SECONDS

Thought you might like to see the result after implementing your
suggestion. :-D Thanks again.

--
Posted via http://www.ruby-....