Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 32 additions & 13 deletions lib/optparse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@
# 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!
Expand All @@ -96,15 +96,15 @@
# 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
Expand Down Expand Up @@ -241,10 +241,10 @@
# 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
Expand Down Expand Up @@ -1310,13 +1310,16 @@ def notwice(obj, prv, msg)
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:
Expand Down Expand Up @@ -1503,11 +1506,16 @@ def make_switch(opts, block = nil)
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.
Expand All @@ -1518,11 +1526,16 @@ def on(*opts, &block)
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.
#
Expand All @@ -1532,11 +1545,17 @@ def on_head(*opts, &block)
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.
#
Expand Down
3 changes: 3 additions & 0 deletions lib/optparse/kwargs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
require 'optparse'

class OptionParser
# :call-seq:
# define_by_keywords(options, method, **params)
#
def define_by_keywords(options, meth, **opts)
meth.parameters.each do |type, name|
case type
Expand Down