OptionParser question

J

Jos Backus

OptionParser is still a mystery to me. In the example below, how do I
distinguish between a -c flag without an argument (which is a fatal syntax
error) and no -c flag at all? In both cases client equals nil. There doesn't
seem to be a way to intercept the `missing argument' message, or if there is,
I can't seem to find it in the documentation :-/

require 'optparse'

client = nil
ARGV.options do |opt|
opt.on("-c CLIENT", String) do |s|
puts "client: #{s}"
client = s
end
opt.parse!
end

puts "client is #{client}" if client

puts "done"

Running the example yields:

lizzy:~% ruby op -c foo
client: foo
client is foo
done
lizzy:~% ruby op -c
op: missing argument: -c
done
lizzy:~% ruby op
done
lizzy:~%
 
W

Wybo Dekker

OptionParser is still a mystery to me. In the example below, how do I
distinguish between a -c flag without an argument (which is a fatal syntax
error) and no -c flag at all? In both cases client equals nil. There doesn't
seem to be a way to intercept the `missing argument' message, or if there is,
I can't seem to find it in the documentation :-/

require 'optparse'

client = nil
ARGV.options do |opt|
opt.on("-c CLIENT", String) do |s|
puts "client: #{s}"
client = s
end
opt.parse!
end

puts "client is #{client}" if client

puts "done"

Running the example yields:

lizzy:~% ruby op -c foo
client: foo
client is foo
done
lizzy:~% ruby op -c
op: missing argument: -c
done
lizzy:~% ruby op
done
lizzy:~%

Try [CLIENT] instead of CLIENT:

require 'optparse'

client = nil
ARGV.options do |opt|
opt.on("-c [CLIENT]", String) do |s|
puts "client: #{s}"
client = s
end
opt.parse!
end

puts "client is #{client}" if client

puts "done"
 
J

Jos Backus

require 'optparse'

client = nil
ARGV.options do |opt|
opt.on("-c [CLIENT]", String) do |s|
puts "client: #{s}"
client = s
end
opt.parse!
end

puts "client is #{client}" if client

puts "done"

Thanks, but the problem with this is that when one adds the line

opt.on("-h") do puts opt; exit 64 end

to the example, the help message will show `-c [CLIENT]', suggesting that the
CLIENT value is optional because of the brackets.
 
J

Jos Backus

opt.parse! returns the remaining non-option arguments (which is in fact
just ARGV at this stage, with options removed), or nil upon failure. This
is propagated to the return value of ARGV.options. A common idiom seems to
be:

ARGV.options do |opt|
...
...
opt.parse!
end or exit 1
# ^^^^^^^^^

I might use this. The downside here is that one doesn't know which option
failed so it's hard to give a more specific error message. Perhaps the author
will address this issue in due time.

Thanks!
 
N

nobu.nokada

Hi,

At Wed, 16 Mar 2005 05:46:04 +0900,
Jos Backus wrote in [ruby-talk:133774]:
I might use this. The downside here is that one doesn't know which option
failed so it's hard to give a more specific error message. Perhaps the author
will address this issue in due time.

Actually, #parse! just raises a exception, subclass of
OptionParser::Error, and ARGV.options does rescue it in the
given block and converts to nil.

require 'optparse'

client = nil
ARGV.options do |opt|
opt.on("-c CLIENT", String) do |s|
puts "client: #{s}"
client = s
end
begin
opt.parse!
rescue OptionParser::MissingArgument
p $!.args # => "-c"
abort
end
end

puts "client is #{client}" if client

puts "done"
 
J

Jos Backus

Hello,

Actually, #parse! just raises a exception, subclass of
OptionParser::Error, and ARGV.options does rescue it in the
given block and converts to nil.
[example snipped]

Neat. I would never have figured that one out :-/

Thanks for the example, Nobu, that's the type of control I'm looking for.
Maybe this can be added to the examples in the documentation...

Cheers,
 

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

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top