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:~%
···
--
Jos Backus
jos at catnook.com
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"
···
On Tue, 15 Mar 2005, Jos Backus wrote:
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:~%
--
Wybo
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
# ^^^^^^^^^
···
On Tue, 15 Mar 2005 10:48:36 +0900, Jos Backus wrote:
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 :-/
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.
···
On Tue, Mar 15, 2005 at 07:51:16PM +0900, Wybo Dekker wrote:
On Tue, 15 Mar 2005, Jos Backus wrote:
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"
--
Jos Backus
jos at catnook.com
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!
···
On Tue, Mar 15, 2005 at 11:02:21PM +0900, Jonathan Paisley wrote:
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
# ^^^^^^^^^
--
Jos Backus
jos at catnook.com
Hi,
At Wed, 16 Mar 2005 05:46:04 +0900,
Jos Backus wrote in [ruby-talk:133774]:
> 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.
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"
···
--
Nobu Nakada