GetoptLong#.quiet not doing what expected

E

edward.og

(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
 
J

Jim Freeze

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'.


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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top