[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

GetoptLong#.quiet not doing what expected

edward.og@gmail.com

7/14/2005 3:59:00 PM

(Writing this message for the second time - curse browsers for not
saving the
contents of input fields like these...)

I'm a very beginner Ruby user. It's a pretty, poetic language. I'm
using it
to write a Nagios plugin.

I took the synopsis that comes in the GetoptLong documentation, and
turned on
the quiet flag.

################################################################################

require 'getoptlong'


# specify the options we accept and initialize
# the option parser

opts.quiet = true

opts = GetoptLong.new(
[ "--size", "-s", GetoptLong::REQUIRED_ARGUMENT ],
[ "--verbose", "-v", GetoptLong::NO_ARGUMENT ],
[ "--query", "-q", GetoptLong::NO_ARGUMENT ],
[ "--check", "--valid", "-c", GetoptLong::NO_ARGUMENT ]
)


# process the parsed options


opts.each do |opt, arg|
puts "Option: #{opt}, arg #{arg.inspect}"
end


puts "Remaining args: #{ARGV.join(', ')}"
################################################################################

The documentation says that the quiet flag makes the opt.each (in this
case)
stop when it reaches an invalid flag because GetoptLong#.get will have
passed
nil to the opts.each loop on reaching an invalid flag.

I tried this:
$ ruby getoptslongExample.rb -not -a -real -option
getoptslongExample.rb: invalid option -- n
/usr/lib/ruby/1.8/getoptlong.rb:265:in `set_error': invalid option -- n
(GetoptLong::InvalidOption) from
/usr/lib/ruby/1.8/getoptlong.rb:434:in `get_option'
from /usr/lib/ruby/1.8/getoptlong.rb:458:in `each'
from /usr/lib/ruby/1.8/getoptlong.rb:457:in `loop'
from /usr/lib/ruby/1.8/getoptlong.rb:457:in `each'
from getoptslongExample.rb:25

This doesn't look very quiet.

I've caught the raised error with the following code:

################################################################################

#snip#

# process the parsed options

begin
opts.each do |opt, arg|
puts "Option: #{opt}, arg #{arg.inspect}"
end
rescue
print "Chill, it's ok.\n"
exit
end

################################################################################

This works nicely, but I think I'd rather have the quiet version that I
expected. Actually, what I really like to have is a version that could
tell
the user that *badOption* is invalid.

How would I implement this, and why is the quiet option not doing
anything?

Thanks very much,
Edward Ocampo-Gooding

2 Answers

Jim Freeze

7/14/2005 8:45:00 PM

0

Hi Edward

You could give the commandline gem a try (I wrote it)

% gem install -r commandline -v 0.7.1

% cat myapp.rb
require 'rubygems'
require 'commandline'

class MyApp < CommandLine::Application
def initialize
option :names => %w(--size -s)
option :verbose
option :flag, :names => %w(--query -q)
option :flag, :names => %w(--check --valid -c)
end
end

% ruby myapp.rb -not -a -real -option
ERROR: Unknown option '-not'.


* edward.og@gmail.com <edward.og@gmail.com> [2005-07-15 01:00:52 +0900]:

> I took the synopsis that comes in the GetoptLong documentation, and
> turned on
> the quiet flag.
>
> ################################################################################
>
> require 'getoptlong'
>
>
> # specify the options we accept and initialize
> # the option parser
>
> opts.quiet = true
>
> opts = GetoptLong.new(
> [ "--size", "-s", GetoptLong::REQUIRED_ARGUMENT ],
> [ "--verbose", "-v", GetoptLong::NO_ARGUMENT ],
> [ "--query", "-q", GetoptLong::NO_ARGUMENT ],
> [ "--check", "--valid", "-c", GetoptLong::NO_ARGUMENT ]
> )
>
>
> # process the parsed options
>
>
> opts.each do |opt, arg|
> puts "Option: #{opt}, arg #{arg.inspect}"
> end
>
>
> puts "Remaining args: #{ARGV.join(', ')}"
> ################################################################################
>
> The documentation says that the quiet flag makes the opt.each (in this
> case)
> stop when it reaches an invalid flag because GetoptLong#.get will have
> passed
> nil to the opts.each loop on reaching an invalid flag.
>
> I tried this:
> $ ruby getoptslongExample.rb -not -a -real -option
> getoptslongExample.rb: invalid option -- n
> /usr/lib/ruby/1.8/getoptlong.rb:265:in `set_error': invalid option -- n
> (GetoptLong::InvalidOption) from
> /usr/lib/ruby/1.8/getoptlong.rb:434:in `get_option'
> from /usr/lib/ruby/1.8/getoptlong.rb:458:in `each'
> from /usr/lib/ruby/1.8/getoptlong.rb:457:in `loop'
> from /usr/lib/ruby/1.8/getoptlong.rb:457:in `each'
> from getoptslongExample.rb:25
>
> This doesn't look very quiet.
>
> I've caught the raised error with the following code:
>
> ################################################################################
>
> #snip#
>
> # process the parsed options
>
> begin
> opts.each do |opt, arg|
> puts "Option: #{opt}, arg #{arg.inspect}"
> end
> rescue
> print "Chill, it's ok.\n"
> exit
> end
>
> ################################################################################
>
> This works nicely, but I think I'd rather have the quiet version that I
> expected. Actually, what I really like to have is a version that could
> tell
> the user that *badOption* is invalid.
>
> How would I implement this, and why is the quiet option not doing
> anything?
>

--
Jim Freeze


edward.og@gmail.com

7/15/2005 7:40:00 AM

0

That looks very shiny Jim.

I'll give it a whirl. Thanks!