lib/optparse.rb in optparse-0.1.0 vs lib/optparse.rb in optparse-0.1.1
- old
+ new
@@ -70,14 +70,14 @@
# === Minimal example
#
# require 'optparse'
#
# options = {}
-# OptionParser.new do |opts|
-# opts.banner = "Usage: example.rb [options]"
+# OptionParser.new do |parser|
+# parser.banner = "Usage: example.rb [options]"
#
-# opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
+# parser.on("-v", "--[no-]verbose", "Run verbosely") do |v|
# options[:verbose] = v
# end
# end.parse!
#
# p options
@@ -94,19 +94,19 @@
#
# class Parser
# def self.parse(options)
# args = Options.new("world")
#
-# opt_parser = OptionParser.new do |opts|
-# opts.banner = "Usage: example.rb [options]"
+# opt_parser = OptionParser.new do |parser|
+# parser.banner = "Usage: example.rb [options]"
#
-# opts.on("-nNAME", "--name=NAME", "Name to say hello to") do |n|
+# parser.on("-nNAME", "--name=NAME", "Name to say hello to") do |n|
# args.name = n
# end
#
-# opts.on("-h", "--help", "Prints this help") do
-# puts opts
+# parser.on("-h", "--help", "Prints this help") do
+# puts parser
# exit
# end
# end
#
# opt_parser.parse!(options)
@@ -239,14 +239,14 @@
# The +into+ option of +order+, +parse+ and so on methods stores command line options into a Hash.
#
# require 'optparse'
#
# params = {}
-# OptionParser.new do |opts|
-# opts.on('-a')
-# opts.on('-b NUM', Integer)
-# opts.on('-v', '--verbose')
+# OptionParser.new do |parser|
+# parser.on('-a')
+# parser.on('-b NUM', Integer)
+# parser.on('-v', '--verbose')
# end.parse!(into: params)
#
# p params
#
# Used:
@@ -417,11 +417,11 @@
#
# The above examples should be enough to learn how to use this class. If you
# have any questions, file a ticket at http://bugs.ruby-lang.org.
#
class OptionParser
- OptionParser::Version = "0.1.0"
+ OptionParser::Version = "0.1.1"
# :stopdoc:
NoArgument = [NO_ARGUMENT = :NONE, nil].freeze
RequiredArgument = [REQUIRED_ARGUMENT = :REQUIRED, true].freeze
OptionalArgument = [OPTIONAL_ARGUMENT = :OPTIONAL, false].freeze
@@ -1089,10 +1089,11 @@
@program_name = nil
@banner = banner
@summary_width = width
@summary_indent = indent
@default_argv = ARGV
+ @require_exact = false
add_officious
yield self if block_given?
end
def add_officious # :nodoc:
@@ -1162,10 +1163,14 @@
attr_accessor :summary_indent
# Strings to be parsed in default.
attr_accessor :default_argv
+ # Whether to require that options match exactly (disallows providing
+ # abbreviated long option as short option).
+ attr_accessor :require_exact
+
#
# Heading banner preceding summary.
#
def banner
unless @banner
@@ -1303,17 +1308,20 @@
obj
end
private :notwice
SPLAT_PROC = proc {|*a| a.length <= 1 ? a.first : a} # :nodoc:
+
+ # :call-seq:
+ # make_switch(params, block = nil)
#
# Creates an OptionParser::Switch from the parameters. The parsed argument
# value is passed to the given block, where it can be processed.
#
# See at the beginning of OptionParser for some full examples.
#
- # +opts+ can include the following elements:
+ # +params+ can include the following elements:
#
# [Argument style:]
# One of the following:
# :NONE, :REQUIRED, :OPTIONAL
#
@@ -1496,45 +1504,61 @@
return s, short, long,
(not_style.new(not_pattern, not_conv, sdesc, ldesc, nil, desc, block) if not_style),
nolong
end
+ # :call-seq:
+ # define(*params, &block)
+ #
def define(*opts, &block)
top.append(*(sw = make_switch(opts, block)))
sw[0]
end
+ # :call-seq:
+ # on(*params, &block)
#
# Add option switch and handler. See #make_switch for an explanation of
# parameters.
#
def on(*opts, &block)
define(*opts, &block)
self
end
alias def_option define
+ # :call-seq:
+ # define_head(*params, &block)
+ #
def define_head(*opts, &block)
top.prepend(*(sw = make_switch(opts, block)))
sw[0]
end
+ # :call-seq:
+ # on_head(*params, &block)
#
# Add option switch like with #on, but at head of summary.
#
def on_head(*opts, &block)
define_head(*opts, &block)
self
end
alias def_head_option define_head
+ # :call-seq:
+ # define_tail(*params, &block)
+ #
def define_tail(*opts, &block)
base.append(*(sw = make_switch(opts, block)))
sw[0]
end
#
+ # :call-seq:
+ # on_tail(*params, &block)
+ #
# Add option switch like with #on, but at tail of summary.
#
def on_tail(*opts, &block)
define_tail(*opts, &block)
self
@@ -1581,10 +1605,13 @@
when /\A--([^=]*)(?:=(.*))?/m
opt, rest = $1, $2
opt.tr!('_', '-')
begin
sw, = complete(:long, opt, true)
+ if require_exact && !sw.long.include?(arg)
+ raise InvalidOption, arg
+ end
rescue ParseError
raise $!.set_option(arg, true)
end
begin
opt, cb, val = sw.parse(rest, argv) {|*exc| raise(*exc)}
@@ -1605,10 +1632,11 @@
sw, = complete(:short, opt)
# short option matched.
val = arg.delete_prefix('-')
has_arg = true
rescue InvalidOption
+ raise if require_exact
# if no short options match, try completion with long
# options.
sw, = complete(:long, opt)
eq ||= !rest
end
@@ -1616,11 +1644,16 @@
rescue ParseError
raise $!.set_option(arg, true)
end
begin
opt, cb, val = sw.parse(val, argv) {|*exc| raise(*exc) if eq}
+ rescue ParseError
+ raise $!.set_option(arg, arg.length > 2)
+ else
raise InvalidOption, arg if has_arg and !eq and arg == "-#{opt}"
+ end
+ begin
argv.unshift(opt) if opt and (!rest or (opt = opt.sub(/\A-*/, '-')) != '-')
val = cb.call(val) if cb
setter.call(sw.switch_name, val) if setter
rescue ParseError
raise $!.set_option(arg, arg.length > 2)
@@ -1747,10 +1780,10 @@
# Traverses @stack, sending each element method +id+ with +args+ and
# +block+.
#
def visit(id, *args, &block)
@stack.reverse_each do |el|
- el.send(id, *args, &block)
+ el.__send__(id, *args, &block)
end
nil
end
private :visit