2003-12-23 08:27:42 -05:00
|
|
|
#
|
|
|
|
# optparse.rb - command-line option analysis with the OptionParser class.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-12-23 08:27:42 -05:00
|
|
|
# Author:: Nobu Nakada
|
|
|
|
# Documentation:: Nobu Nakada and Gavin Sinclair.
|
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
# See OptionParser for documentation.
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
2003-12-23 08:27:42 -05:00
|
|
|
|
|
|
|
|
2012-12-17 12:44:34 -05:00
|
|
|
#--
|
2009-03-05 22:56:38 -05:00
|
|
|
# == Developer Documentation (not for RDoc output)
|
|
|
|
#
|
2003-12-23 08:27:42 -05:00
|
|
|
# === Class tree
|
|
|
|
#
|
|
|
|
# - OptionParser:: front end
|
|
|
|
# - OptionParser::Switch:: each switches
|
|
|
|
# - OptionParser::List:: options list
|
|
|
|
# - OptionParser::ParseError:: errors on parsing
|
|
|
|
# - OptionParser::AmbiguousOption
|
|
|
|
# - OptionParser::NeedlessArgument
|
|
|
|
# - OptionParser::MissingArgument
|
|
|
|
# - OptionParser::InvalidOption
|
|
|
|
# - OptionParser::InvalidArgument
|
|
|
|
# - OptionParser::AmbiguousArgument
|
|
|
|
#
|
|
|
|
# === Object relationship diagram
|
|
|
|
#
|
2006-08-04 14:05:50 -04:00
|
|
|
# +--------------+
|
|
|
|
# | OptionParser |<>-----+
|
|
|
|
# +--------------+ | +--------+
|
|
|
|
# | ,-| Switch |
|
|
|
|
# on_head -------->+---------------+ / +--------+
|
|
|
|
# accept/reject -->| List |<|>-
|
|
|
|
# | |<|>- +----------+
|
|
|
|
# on ------------->+---------------+ `-| argument |
|
|
|
|
# : : | class |
|
|
|
|
# +---------------+ |==========|
|
|
|
|
# on_tail -------->| | |pattern |
|
|
|
|
# +---------------+ |----------|
|
|
|
|
# OptionParser.accept ->| DefaultList | |converter |
|
|
|
|
# reject |(shared between| +----------+
|
|
|
|
# | all instances)|
|
|
|
|
# +---------------+
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
2012-12-17 12:44:34 -05:00
|
|
|
#++
|
|
|
|
#
|
2003-12-28 12:31:10 -05:00
|
|
|
# == OptionParser
|
|
|
|
#
|
|
|
|
# === Introduction
|
|
|
|
#
|
|
|
|
# OptionParser is a class for command-line option analysis. It is much more
|
|
|
|
# advanced, yet also easier to use, than GetoptLong, and is a more Ruby-oriented
|
|
|
|
# solution.
|
|
|
|
#
|
|
|
|
# === Features
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2006-06-28 17:56:01 -04:00
|
|
|
# 1. The argument specification and the code to handle it are written in the
|
|
|
|
# same place.
|
2003-12-28 12:31:10 -05:00
|
|
|
# 2. It can output an option summary; you don't need to maintain this string
|
|
|
|
# separately.
|
|
|
|
# 3. Optional and mandatory arguments are specified very gracefully.
|
|
|
|
# 4. Arguments can be automatically converted to a specified class.
|
|
|
|
# 5. Arguments can be restricted to a certain set.
|
|
|
|
#
|
2011-05-16 17:57:22 -04:00
|
|
|
# All of these features are demonstrated in the examples below. See
|
|
|
|
# #make_switch for full documentation.
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
2006-06-28 17:56:01 -04:00
|
|
|
# === Minimal example
|
|
|
|
#
|
|
|
|
# require 'optparse'
|
|
|
|
#
|
|
|
|
# options = {}
|
|
|
|
# OptionParser.new do |opts|
|
|
|
|
# opts.banner = "Usage: example.rb [options]"
|
|
|
|
#
|
|
|
|
# opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
|
|
|
|
# options[:verbose] = v
|
|
|
|
# end
|
|
|
|
# end.parse!
|
|
|
|
#
|
|
|
|
# p options
|
|
|
|
# p ARGV
|
|
|
|
#
|
|
|
|
# === Complete example
|
2003-12-23 08:27:42 -05:00
|
|
|
#
|
|
|
|
# The following example is a complete Ruby program. You can run it and see the
|
2003-12-28 12:31:10 -05:00
|
|
|
# effect of specifying various options. This is probably the best way to learn
|
|
|
|
# the features of +optparse+.
|
2003-12-23 08:27:42 -05:00
|
|
|
#
|
|
|
|
# require 'optparse'
|
|
|
|
# require 'optparse/time'
|
|
|
|
# require 'ostruct'
|
|
|
|
# require 'pp'
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-12-23 08:27:42 -05:00
|
|
|
# class OptparseExample
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-12-23 08:27:42 -05:00
|
|
|
# CODES = %w[iso-2022-jp shift_jis euc-jp utf8 binary]
|
2003-12-28 12:31:10 -05:00
|
|
|
# CODE_ALIASES = { "jis" => "iso-2022-jp", "sjis" => "shift_jis" }
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-12-23 08:27:42 -05:00
|
|
|
# #
|
|
|
|
# # Return a structure describing the options.
|
|
|
|
# #
|
|
|
|
# def self.parse(args)
|
|
|
|
# # The options specified on the command line will be collected in *options*.
|
|
|
|
# # We set default values here.
|
|
|
|
# options = OpenStruct.new
|
|
|
|
# options.library = []
|
|
|
|
# options.inplace = false
|
|
|
|
# options.encoding = "utf8"
|
|
|
|
# options.transfer_type = :auto
|
|
|
|
# options.verbose = false
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2012-12-22 11:36:30 -05:00
|
|
|
# opt_parser = OptionParser.new do |opts|
|
2003-12-23 08:27:42 -05:00
|
|
|
# opts.banner = "Usage: example.rb [options]"
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-12-23 08:27:42 -05:00
|
|
|
# opts.separator ""
|
|
|
|
# opts.separator "Specific options:"
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-12-23 08:27:42 -05:00
|
|
|
# # Mandatory argument.
|
|
|
|
# opts.on("-r", "--require LIBRARY",
|
|
|
|
# "Require the LIBRARY before executing your script") do |lib|
|
|
|
|
# options.library << lib
|
|
|
|
# end
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-12-23 08:27:42 -05:00
|
|
|
# # Optional argument; multi-line description.
|
|
|
|
# opts.on("-i", "--inplace [EXTENSION]",
|
|
|
|
# "Edit ARGV files in place",
|
|
|
|
# " (make backup if EXTENSION supplied)") do |ext|
|
|
|
|
# options.inplace = true
|
|
|
|
# options.extension = ext || ''
|
|
|
|
# options.extension.sub!(/\A\.?(?=.)/, ".") # Ensure extension begins with dot.
|
|
|
|
# end
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-12-23 08:27:42 -05:00
|
|
|
# # Cast 'delay' argument to a Float.
|
|
|
|
# opts.on("--delay N", Float, "Delay N seconds before executing") do |n|
|
|
|
|
# options.delay = n
|
|
|
|
# end
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-12-23 08:27:42 -05:00
|
|
|
# # Cast 'time' argument to a Time object.
|
|
|
|
# opts.on("-t", "--time [TIME]", Time, "Begin execution at given time") do |time|
|
|
|
|
# options.time = time
|
|
|
|
# end
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-12-23 08:27:42 -05:00
|
|
|
# # Cast to octal integer.
|
|
|
|
# opts.on("-F", "--irs [OCTAL]", OptionParser::OctalInteger,
|
|
|
|
# "Specify record separator (default \\0)") do |rs|
|
|
|
|
# options.record_separator = rs
|
|
|
|
# end
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-12-23 08:27:42 -05:00
|
|
|
# # List of arguments.
|
|
|
|
# opts.on("--list x,y,z", Array, "Example 'list' of arguments") do |list|
|
|
|
|
# options.list = list
|
|
|
|
# end
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-12-23 08:27:42 -05:00
|
|
|
# # Keyword completion. We are specifying a specific set of arguments (CODES
|
|
|
|
# # and CODE_ALIASES - notice the latter is a Hash), and the user may provide
|
|
|
|
# # the shortest unambiguous text.
|
|
|
|
# code_list = (CODE_ALIASES.keys + CODES).join(',')
|
|
|
|
# opts.on("--code CODE", CODES, CODE_ALIASES, "Select encoding",
|
|
|
|
# " (#{code_list})") do |encoding|
|
|
|
|
# options.encoding = encoding
|
|
|
|
# end
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-12-23 08:27:42 -05:00
|
|
|
# # Optional argument with keyword completion.
|
2003-12-28 12:31:10 -05:00
|
|
|
# opts.on("--type [TYPE]", [:text, :binary, :auto],
|
|
|
|
# "Select transfer type (text, binary, auto)") do |t|
|
2003-12-23 08:27:42 -05:00
|
|
|
# options.transfer_type = t
|
|
|
|
# end
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-12-23 08:27:42 -05:00
|
|
|
# # Boolean switch.
|
|
|
|
# opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
|
|
|
|
# options.verbose = v
|
|
|
|
# end
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-12-23 08:27:42 -05:00
|
|
|
# opts.separator ""
|
|
|
|
# opts.separator "Common options:"
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-12-23 08:27:42 -05:00
|
|
|
# # No argument, shows at tail. This will print an options summary.
|
|
|
|
# # Try it and see!
|
|
|
|
# opts.on_tail("-h", "--help", "Show this message") do
|
|
|
|
# puts opts
|
|
|
|
# exit
|
|
|
|
# end
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-12-23 08:27:42 -05:00
|
|
|
# # Another typical switch to print the version.
|
|
|
|
# opts.on_tail("--version", "Show version") do
|
2013-09-08 08:37:29 -04:00
|
|
|
# puts ::Version.join('.')
|
2003-12-23 08:27:42 -05:00
|
|
|
# exit
|
|
|
|
# end
|
|
|
|
# end
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2012-12-22 11:36:30 -05:00
|
|
|
# opt_parser.parse!(args)
|
2003-12-23 08:27:42 -05:00
|
|
|
# options
|
|
|
|
# end # parse()
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-12-23 08:27:42 -05:00
|
|
|
# end # class OptparseExample
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-12-23 08:27:42 -05:00
|
|
|
# options = OptparseExample.parse(ARGV)
|
|
|
|
# pp options
|
2012-12-22 11:36:30 -05:00
|
|
|
# pp ARGV
|
2003-12-23 08:27:42 -05:00
|
|
|
#
|
2010-11-19 06:26:54 -05:00
|
|
|
# === Shell Completion
|
|
|
|
#
|
|
|
|
# For modern shells (e.g. bash, zsh, etc.), you can use shell
|
|
|
|
# completion for command line options.
|
|
|
|
#
|
2003-12-28 12:31:10 -05:00
|
|
|
# === Further documentation
|
|
|
|
#
|
2006-06-28 17:56:01 -04:00
|
|
|
# The above examples should be enough to learn how to use this class. If you
|
2012-09-12 22:25:37 -04:00
|
|
|
# have any questions, file a ticket at http://bugs.ruby-lang.org.
|
2003-12-23 08:27:42 -05:00
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
class OptionParser
|
2003-12-23 08:27:42 -05:00
|
|
|
# :stopdoc:
|
2002-07-12 12:34:02 -04:00
|
|
|
NoArgument = [NO_ARGUMENT = :NONE, nil].freeze
|
|
|
|
RequiredArgument = [REQUIRED_ARGUMENT = :REQUIRED, true].freeze
|
|
|
|
OptionalArgument = [OPTIONAL_ARGUMENT = :OPTIONAL, false].freeze
|
2003-12-23 08:27:42 -05:00
|
|
|
# :startdoc:
|
|
|
|
|
|
|
|
#
|
|
|
|
# Keyword completion module. This allows partial arguments to be specified
|
2003-12-28 12:31:10 -05:00
|
|
|
# and resolved against a list of acceptable values.
|
2003-12-23 08:27:42 -05:00
|
|
|
#
|
2003-12-28 12:31:10 -05:00
|
|
|
module Completion
|
2010-11-19 06:26:54 -05:00
|
|
|
def self.regexp(key, icase)
|
|
|
|
Regexp.new('\A' + Regexp.quote(key).gsub(/\w+\b/, '\&\w*'), icase)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.candidate(key, icase = false, pat = nil, &block)
|
|
|
|
pat ||= Completion.regexp(key, icase)
|
2003-11-16 20:50:31 -05:00
|
|
|
candidates = []
|
2010-11-19 06:26:54 -05:00
|
|
|
block.call do |k, *v|
|
2003-12-23 08:27:42 -05:00
|
|
|
(if Regexp === k
|
|
|
|
kn = nil
|
|
|
|
k === key
|
|
|
|
else
|
|
|
|
kn = defined?(k.id2name) ? k.id2name : k
|
|
|
|
pat === kn
|
|
|
|
end) or next
|
|
|
|
v << k if v.empty?
|
2003-11-16 20:50:31 -05:00
|
|
|
candidates << [k, v, kn]
|
|
|
|
end
|
2010-11-19 06:26:54 -05:00
|
|
|
candidates
|
|
|
|
end
|
|
|
|
|
|
|
|
def candidate(key, icase = false, pat = nil)
|
|
|
|
Completion.candidate(key, icase, pat, &method(:each))
|
|
|
|
end
|
|
|
|
|
|
|
|
public
|
|
|
|
def complete(key, icase = false, pat = nil)
|
|
|
|
candidates = candidate(key, icase, pat, &method(:each)).sort_by {|k, v, kn| kn.size}
|
2003-11-16 20:50:31 -05:00
|
|
|
if candidates.size == 1
|
|
|
|
canon, sw, * = candidates[0]
|
|
|
|
elsif candidates.size > 1
|
|
|
|
canon, sw, cn = candidates.shift
|
|
|
|
candidates.each do |k, v, kn|
|
|
|
|
next if sw == v
|
2003-12-23 08:27:42 -05:00
|
|
|
if String === cn and String === kn
|
|
|
|
if cn.rindex(kn, 0)
|
|
|
|
canon, sw, cn = k, v, kn
|
|
|
|
next
|
|
|
|
elsif kn.rindex(cn, 0)
|
|
|
|
next
|
|
|
|
end
|
|
|
|
end
|
|
|
|
throw :ambiguous, key
|
|
|
|
end
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
if canon
|
2003-12-23 08:27:42 -05:00
|
|
|
block_given? or return key, *sw
|
|
|
|
yield(key, *sw)
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2002-08-07 09:00:10 -04:00
|
|
|
def convert(opt = nil, val = nil, *)
|
2002-07-12 12:34:02 -04:00
|
|
|
val
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-12-23 08:27:42 -05:00
|
|
|
|
|
|
|
#
|
|
|
|
# Map from option/keyword string to object with completion.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
class OptionMap < Hash
|
|
|
|
include Completion
|
|
|
|
end
|
|
|
|
|
2003-12-23 08:27:42 -05:00
|
|
|
|
|
|
|
#
|
2003-12-28 12:31:10 -05:00
|
|
|
# Individual switch class. Not important to the user.
|
|
|
|
#
|
|
|
|
# Defined within Switch are several Switch-derived classes: NoArgument,
|
2009-03-05 22:56:38 -05:00
|
|
|
# RequiredArgument, etc.
|
2003-12-23 08:27:42 -05:00
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
class Switch
|
|
|
|
attr_reader :pattern, :conv, :short, :long, :arg, :desc, :block
|
|
|
|
|
2003-12-23 08:27:42 -05:00
|
|
|
#
|
|
|
|
# Guesses argument style from +arg+. Returns corresponding
|
|
|
|
# OptionParser::Switch class (OptionalArgument, etc.).
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def self.guess(arg)
|
|
|
|
case arg
|
|
|
|
when ""
|
2003-01-30 03:11:21 -05:00
|
|
|
t = self
|
|
|
|
when /\A=?\[/
|
|
|
|
t = Switch::OptionalArgument
|
|
|
|
when /\A\s+\[/
|
|
|
|
t = Switch::PlacedArgument
|
2002-07-12 12:34:02 -04:00
|
|
|
else
|
2003-01-30 03:11:21 -05:00
|
|
|
t = Switch::RequiredArgument
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
2003-01-30 03:11:21 -05:00
|
|
|
self >= t or incompatible_argument_styles(arg, t)
|
|
|
|
t
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.incompatible_argument_styles(arg, t)
|
2008-10-09 11:21:42 -04:00
|
|
|
raise(ArgumentError, "#{arg}: incompatible argument styles\n #{self}, #{t}",
|
|
|
|
ParseError.filter_backtrace(caller(2)))
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
|
2003-01-30 19:41:40 -05:00
|
|
|
def self.pattern
|
|
|
|
NilClass
|
|
|
|
end
|
|
|
|
|
2002-07-12 12:34:02 -04:00
|
|
|
def initialize(pattern = nil, conv = nil,
|
2003-12-23 08:27:42 -05:00
|
|
|
short = nil, long = nil, arg = nil,
|
|
|
|
desc = ([] if short or long), block = Proc.new)
|
2003-10-29 12:47:24 -05:00
|
|
|
raise if Array === pattern
|
2002-07-12 12:34:02 -04:00
|
|
|
@pattern, @conv, @short, @long, @arg, @desc, @block =
|
2003-12-23 08:27:42 -05:00
|
|
|
pattern, conv, short, long, arg, desc, block
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
2006-06-28 17:56:01 -04:00
|
|
|
# Parses +arg+ and returns rest of +arg+ and matched portion to the
|
|
|
|
# argument pattern. Yields when the pattern doesn't match substring.
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def parse_arg(arg)
|
2006-12-12 09:34:26 -05:00
|
|
|
pattern or return nil, [arg]
|
2002-07-12 12:34:02 -04:00
|
|
|
unless m = pattern.match(arg)
|
2003-12-23 08:27:42 -05:00
|
|
|
yield(InvalidArgument, arg)
|
2006-12-12 09:34:26 -05:00
|
|
|
return arg, []
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
if String === m
|
2003-12-23 08:27:42 -05:00
|
|
|
m = [s = m]
|
2002-07-12 12:34:02 -04:00
|
|
|
else
|
2003-12-23 08:27:42 -05:00
|
|
|
m = m.to_a
|
|
|
|
s = m[0]
|
|
|
|
return nil, m unless String === s
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
raise InvalidArgument, arg unless arg.rindex(s, 0)
|
2003-05-30 01:55:50 -04:00
|
|
|
return nil, m if s.length == arg.length
|
2002-07-12 12:34:02 -04:00
|
|
|
yield(InvalidArgument, arg) # didn't match whole arg
|
2003-05-30 01:55:50 -04:00
|
|
|
return arg[s.length..-1], m
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
private :parse_arg
|
|
|
|
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
2006-06-28 17:56:01 -04:00
|
|
|
# Parses argument, converts and returns +arg+, +block+ and result of
|
|
|
|
# conversion. Yields at semi-error condition instead of raising an
|
|
|
|
# exception.
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
2006-12-12 09:34:26 -05:00
|
|
|
def conv_arg(arg, val = [])
|
2006-09-12 12:14:50 -04:00
|
|
|
if conv
|
2006-09-26 10:57:48 -04:00
|
|
|
val = conv.call(*val)
|
2002-07-12 12:34:02 -04:00
|
|
|
else
|
2006-10-09 10:41:24 -04:00
|
|
|
val = proc {|v| v}.call(*val)
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
2006-09-12 12:14:50 -04:00
|
|
|
return arg, block, val
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
2003-05-30 01:55:50 -04:00
|
|
|
private :conv_arg
|
2002-07-12 12:34:02 -04:00
|
|
|
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
2006-06-28 17:56:01 -04:00
|
|
|
# Produces the summary text. Each line of the summary is yielded to the
|
|
|
|
# block (without newline).
|
|
|
|
#
|
|
|
|
# +sdone+:: Already summarized short style options keyed hash.
|
|
|
|
# +ldone+:: Already summarized long style options keyed hash.
|
|
|
|
# +width+:: Width of left side (option part). In other words, the right
|
|
|
|
# side (description part) starts after +width+ columns.
|
|
|
|
# +max+:: Maximum width of left side -> the options are filled within
|
|
|
|
# +max+ columns.
|
|
|
|
# +indent+:: Prefix string indents all summarized lines.
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def summarize(sdone = [], ldone = [], width = 1, max = width - 1, indent = "")
|
2006-10-09 10:41:24 -04:00
|
|
|
sopts, lopts = [], [], nil
|
2002-07-12 12:34:02 -04:00
|
|
|
@short.each {|s| sdone.fetch(s) {sopts << s}; sdone[s] = true} if @short
|
|
|
|
@long.each {|s| ldone.fetch(s) {lopts << s}; ldone[s] = true} if @long
|
|
|
|
return if sopts.empty? and lopts.empty? # completely hidden
|
|
|
|
|
|
|
|
left = [sopts.join(', ')]
|
|
|
|
right = desc.dup
|
|
|
|
|
|
|
|
while s = lopts.shift
|
2003-12-23 08:27:42 -05:00
|
|
|
l = left[-1].length + s.length
|
|
|
|
l += arg.length if left.size == 1 && arg
|
2007-11-04 15:17:06 -05:00
|
|
|
l < max or sopts.empty? or left << ''
|
2003-12-23 08:27:42 -05:00
|
|
|
left[-1] << if left[-1].empty? then ' ' * 4 else ', ' end << s
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
|
2009-02-01 02:10:08 -05:00
|
|
|
if arg
|
|
|
|
left[0] << (left[1] ? arg.sub(/\A(\[?)=/, '\1') + ',' : arg)
|
|
|
|
end
|
2006-10-09 10:41:24 -04:00
|
|
|
mlen = left.collect {|ss| ss.length}.max.to_i
|
2002-07-12 12:34:02 -04:00
|
|
|
while mlen > width and l = left.shift
|
2006-10-09 10:41:24 -04:00
|
|
|
mlen = left.collect {|ss| ss.length}.max.to_i if l.length == mlen
|
2009-02-01 02:10:08 -05:00
|
|
|
if l.length < width and (r = right[0]) and !r.empty?
|
|
|
|
l = l.to_s.ljust(width) + ' ' + r
|
|
|
|
right.shift
|
|
|
|
end
|
2003-12-23 08:27:42 -05:00
|
|
|
yield(indent + l)
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
|
2004-03-20 11:45:41 -05:00
|
|
|
while begin l = left.shift; r = right.shift; l or r end
|
2003-12-23 08:27:42 -05:00
|
|
|
l = l.to_s.ljust(width) + ' ' + r if r and !r.empty?
|
|
|
|
yield(indent + l)
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2006-12-08 04:13:04 -05:00
|
|
|
def add_banner(to) # :nodoc:
|
2005-04-13 09:12:24 -04:00
|
|
|
unless @short or @long
|
2005-04-12 06:30:45 -04:00
|
|
|
s = desc.join
|
|
|
|
to << " [" + s + "]..." unless s.empty?
|
|
|
|
end
|
|
|
|
to
|
|
|
|
end
|
|
|
|
|
2006-12-08 04:13:04 -05:00
|
|
|
def match_nonswitch?(str) # :nodoc:
|
2005-04-13 09:12:24 -04:00
|
|
|
@pattern =~ str unless @short or @long
|
|
|
|
end
|
|
|
|
|
2006-09-12 12:14:50 -04:00
|
|
|
#
|
|
|
|
# Main name of the switch.
|
|
|
|
#
|
|
|
|
def switch_name
|
2006-09-26 10:57:48 -04:00
|
|
|
(long.first || short.first).sub(/\A-+(?:\[no-\])?/, '')
|
2006-09-12 12:14:50 -04:00
|
|
|
end
|
|
|
|
|
2010-11-19 07:07:09 -05:00
|
|
|
def compsys(sdone, ldone) # :nodoc:
|
2010-12-07 08:29:49 -05:00
|
|
|
sopts, lopts = [], []
|
2010-11-19 07:07:09 -05:00
|
|
|
@short.each {|s| sdone.fetch(s) {sopts << s}; sdone[s] = true} if @short
|
|
|
|
@long.each {|s| ldone.fetch(s) {lopts << s}; ldone[s] = true} if @long
|
|
|
|
return if sopts.empty? and lopts.empty? # completely hidden
|
|
|
|
|
|
|
|
(sopts+lopts).each do |opt|
|
|
|
|
# "(-x -c -r)-l[left justify]" \
|
2013-11-28 02:34:15 -05:00
|
|
|
if /^--\[no-\](.+)$/ =~ opt
|
2010-11-19 07:07:09 -05:00
|
|
|
o = $1
|
|
|
|
yield("--#{o}", desc.join(""))
|
|
|
|
yield("--no-#{o}", desc.join(""))
|
|
|
|
else
|
|
|
|
yield("#{opt}", desc.join(""))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
|
|
|
# Switch that takes no arguments.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
class NoArgument < self
|
2006-06-28 17:56:01 -04:00
|
|
|
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
|
|
|
# Raises an exception if any arguments given.
|
|
|
|
#
|
2006-10-01 10:13:16 -04:00
|
|
|
def parse(arg, argv)
|
2003-12-23 08:27:42 -05:00
|
|
|
yield(NeedlessArgument, arg) if arg
|
|
|
|
conv_arg(arg)
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
2006-06-28 17:56:01 -04:00
|
|
|
|
2003-01-30 03:11:21 -05:00
|
|
|
def self.incompatible_argument_styles(*)
|
|
|
|
end
|
2006-06-28 17:56:01 -04:00
|
|
|
|
2003-01-30 19:41:40 -05:00
|
|
|
def self.pattern
|
2003-12-23 08:27:42 -05:00
|
|
|
Object
|
2003-01-30 19:41:40 -05:00
|
|
|
end
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
|
|
|
# Switch that takes an argument.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
class RequiredArgument < self
|
2006-06-28 17:56:01 -04:00
|
|
|
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
|
|
|
# Raises an exception if argument is not present.
|
|
|
|
#
|
2006-09-26 10:57:48 -04:00
|
|
|
def parse(arg, argv)
|
2005-09-26 08:44:47 -04:00
|
|
|
unless arg
|
|
|
|
raise MissingArgument if argv.empty?
|
|
|
|
arg = argv.shift
|
2003-12-23 08:27:42 -05:00
|
|
|
end
|
2006-10-09 10:41:24 -04:00
|
|
|
conv_arg(*parse_arg(arg, &method(:raise)))
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
|
|
|
# Switch that can omit argument.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
class OptionalArgument < self
|
2006-06-28 17:56:01 -04:00
|
|
|
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
|
|
|
# Parses argument if given, or uses default value.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def parse(arg, argv, &error)
|
2003-12-23 08:27:42 -05:00
|
|
|
if arg
|
|
|
|
conv_arg(*parse_arg(arg, &error))
|
|
|
|
else
|
|
|
|
conv_arg(arg)
|
|
|
|
end
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
2003-01-30 03:11:21 -05:00
|
|
|
end
|
|
|
|
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
2005-09-25 18:56:15 -04:00
|
|
|
# Switch that takes an argument, which does not begin with '-'.
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
2005-09-26 08:44:47 -04:00
|
|
|
class PlacedArgument < self
|
2006-06-28 17:56:01 -04:00
|
|
|
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
2005-09-25 18:56:15 -04:00
|
|
|
# Returns nil if argument is not present or begins with '-'.
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
2003-01-30 03:11:21 -05:00
|
|
|
def parse(arg, argv, &error)
|
2003-12-23 08:27:42 -05:00
|
|
|
if !(val = arg) and (argv.empty? or /\A-/ =~ (val = argv[0]))
|
|
|
|
return nil, block, nil
|
|
|
|
end
|
2005-09-26 08:44:47 -04:00
|
|
|
opt = (val = parse_arg(val, &error))[1]
|
|
|
|
val = conv_arg(*val)
|
|
|
|
if opt and !arg
|
|
|
|
argv.shift
|
|
|
|
else
|
|
|
|
val[0] = nil
|
|
|
|
end
|
|
|
|
val
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
|
|
|
# Simple option list providing mapping from short and/or long option
|
2006-06-28 17:56:01 -04:00
|
|
|
# string to OptionParser::Switch and mapping from acceptable argument to
|
|
|
|
# matching pattern and converter pair. Also provides summary feature.
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
class List
|
2003-12-28 12:31:10 -05:00
|
|
|
# Map from acceptable argument types to pattern and converter pairs.
|
|
|
|
attr_reader :atype
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-12-28 12:31:10 -05:00
|
|
|
# Map from short style option switches to actual switch objects.
|
|
|
|
attr_reader :short
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-12-28 12:31:10 -05:00
|
|
|
# Map from long style option switches to actual switch objects.
|
|
|
|
attr_reader :long
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-12-28 12:31:10 -05:00
|
|
|
# List of all switches and summary string.
|
|
|
|
attr_reader :list
|
|
|
|
|
|
|
|
#
|
|
|
|
# Just initializes all instance variables.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def initialize
|
|
|
|
@atype = {}
|
|
|
|
@short = OptionMap.new
|
2004-12-05 05:39:58 -05:00
|
|
|
@long = OptionMap.new
|
2002-07-12 12:34:02 -04:00
|
|
|
@list = []
|
|
|
|
end
|
|
|
|
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
|
|
|
# See OptionParser.accept.
|
|
|
|
#
|
2009-01-31 04:03:43 -05:00
|
|
|
def accept(t, pat = /.*/m, &block)
|
2002-07-12 12:34:02 -04:00
|
|
|
if pat
|
2008-10-09 11:21:42 -04:00
|
|
|
pat.respond_to?(:match) or
|
|
|
|
raise TypeError, "has no `match'", ParseError.filter_backtrace(caller(2))
|
2002-07-12 12:34:02 -04:00
|
|
|
else
|
2003-12-23 08:27:42 -05:00
|
|
|
pat = t if t.respond_to?(:match)
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
unless block
|
2003-12-23 08:27:42 -05:00
|
|
|
block = pat.method(:convert).to_proc if pat.respond_to?(:convert)
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
@atype[t] = [pat, block]
|
|
|
|
end
|
|
|
|
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
|
|
|
# See OptionParser.reject.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def reject(t)
|
|
|
|
@atype.delete(t)
|
|
|
|
end
|
|
|
|
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
2006-06-28 17:56:01 -04:00
|
|
|
# Adds +sw+ according to +sopts+, +lopts+ and +nlopts+.
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
2006-06-28 17:56:01 -04:00
|
|
|
# +sw+:: OptionParser::Switch instance to be added.
|
|
|
|
# +sopts+:: Short style option list.
|
|
|
|
# +lopts+:: Long style option list.
|
|
|
|
# +nlopts+:: Negated long style options list.
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def update(sw, sopts, lopts, nsw = nil, nlopts = nil)
|
|
|
|
sopts.each {|o| @short[o] = sw} if sopts
|
|
|
|
lopts.each {|o| @long[o] = sw} if lopts
|
|
|
|
nlopts.each {|o| @long[o] = nsw} if nsw and nlopts
|
|
|
|
used = @short.invert.update(@long.invert)
|
|
|
|
@list.delete_if {|o| Switch === o and !used[o]}
|
|
|
|
end
|
|
|
|
private :update
|
|
|
|
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
2006-06-28 17:56:01 -04:00
|
|
|
# Inserts +switch+ at the head of the list, and associates short, long
|
|
|
|
# and negated long options. Arguments are:
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2006-06-28 17:56:01 -04:00
|
|
|
# +switch+:: OptionParser::Switch instance to be inserted.
|
|
|
|
# +short_opts+:: List of short style options.
|
|
|
|
# +long_opts+:: List of long style options.
|
|
|
|
# +nolong_opts+:: List of long style options with "no-" prefix.
|
|
|
|
#
|
|
|
|
# prepend(switch, short_opts, long_opts, nolong_opts)
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def prepend(*args)
|
|
|
|
update(*args)
|
|
|
|
@list.unshift(args[0])
|
|
|
|
end
|
|
|
|
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
2006-06-28 17:56:01 -04:00
|
|
|
# Appends +switch+ at the tail of the list, and associates short, long
|
|
|
|
# and negated long options. Arguments are:
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2006-06-28 17:56:01 -04:00
|
|
|
# +switch+:: OptionParser::Switch instance to be inserted.
|
|
|
|
# +short_opts+:: List of short style options.
|
|
|
|
# +long_opts+:: List of long style options.
|
|
|
|
# +nolong_opts+:: List of long style options with "no-" prefix.
|
|
|
|
#
|
|
|
|
# append(switch, short_opts, long_opts, nolong_opts)
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def append(*args)
|
|
|
|
update(*args)
|
|
|
|
@list.push(args[0])
|
|
|
|
end
|
|
|
|
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
2006-06-28 17:56:01 -04:00
|
|
|
# Searches +key+ in +id+ list. The result is returned or yielded if a
|
|
|
|
# block is given. If it isn't found, nil is returned.
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def search(id, key)
|
|
|
|
if list = __send__(id)
|
2003-12-23 08:27:42 -05:00
|
|
|
val = list.fetch(key) {return nil}
|
2006-10-01 10:13:16 -04:00
|
|
|
block_given? ? yield(val) : val
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
2006-06-28 17:56:01 -04:00
|
|
|
# Searches list +id+ for +opt+ and the optional patterns for completion
|
|
|
|
# +pat+. If +icase+ is true, the search is case insensitive. The result
|
|
|
|
# is returned or yielded if a block is given. If it isn't found, nil is
|
|
|
|
# returned.
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
2004-12-05 05:39:58 -05:00
|
|
|
def complete(id, opt, icase = false, *pat, &block)
|
|
|
|
__send__(id).complete(opt, icase, *pat, &block)
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
|
2005-04-12 06:30:45 -04:00
|
|
|
#
|
2006-06-28 17:56:01 -04:00
|
|
|
# Iterates over each option, passing the option to the +block+.
|
2005-04-12 06:30:45 -04:00
|
|
|
#
|
|
|
|
def each_option(&block)
|
|
|
|
list.each(&block)
|
|
|
|
end
|
|
|
|
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
2006-06-28 17:56:01 -04:00
|
|
|
# Creates the summary table, passing each line to the +block+ (without
|
|
|
|
# newline). The arguments +args+ are passed along to the summarize
|
|
|
|
# method which is called on every option.
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def summarize(*args, &block)
|
2008-12-26 01:50:55 -05:00
|
|
|
sum = []
|
|
|
|
list.reverse_each do |opt|
|
2003-12-23 08:27:42 -05:00
|
|
|
if opt.respond_to?(:summarize) # perhaps OptionParser::Switch
|
2008-12-26 01:50:55 -05:00
|
|
|
s = []
|
|
|
|
opt.summarize(*args) {|l| s << l}
|
|
|
|
sum.concat(s.reverse)
|
2008-09-19 22:44:49 -04:00
|
|
|
elsif !opt or opt.empty?
|
2008-12-26 01:50:55 -05:00
|
|
|
sum << ""
|
2007-09-21 17:02:13 -04:00
|
|
|
elsif opt.respond_to?(:each_line)
|
2008-12-26 01:50:55 -05:00
|
|
|
sum.concat([*opt.each_line].reverse)
|
2003-12-23 08:27:42 -05:00
|
|
|
else
|
2008-12-26 01:50:55 -05:00
|
|
|
sum.concat([*opt.each].reverse)
|
2003-12-23 08:27:42 -05:00
|
|
|
end
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
2008-12-26 01:50:55 -05:00
|
|
|
sum.reverse_each(&block)
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
2005-04-12 06:30:45 -04:00
|
|
|
|
2006-12-08 04:13:04 -05:00
|
|
|
def add_banner(to) # :nodoc:
|
2005-04-12 06:30:45 -04:00
|
|
|
list.each do |opt|
|
|
|
|
if opt.respond_to?(:add_banner)
|
|
|
|
opt.add_banner(to)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
to
|
|
|
|
end
|
2010-11-19 07:07:09 -05:00
|
|
|
|
|
|
|
def compsys(*args, &block) # :nodoc:
|
|
|
|
list.each do |opt|
|
|
|
|
if opt.respond_to?(:compsys)
|
|
|
|
opt.compsys(*args, &block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
2006-06-28 17:56:01 -04:00
|
|
|
# Hash with completion search feature. See OptionParser::Completion.
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
class CompletingHash < Hash
|
|
|
|
include Completion
|
|
|
|
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
|
|
|
# Completion for hash key.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def match(key)
|
2006-12-04 09:49:47 -05:00
|
|
|
*values = fetch(key) {
|
2003-12-23 08:27:42 -05:00
|
|
|
raise AmbiguousArgument, catch(:ambiguous) {return complete(key)}
|
2003-11-12 21:31:17 -05:00
|
|
|
}
|
2006-12-04 09:49:47 -05:00
|
|
|
return key, *values
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
# :stopdoc:
|
|
|
|
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
2006-06-28 17:56:01 -04:00
|
|
|
# Enumeration of acceptable argument styles. Possible values are:
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
2006-06-28 17:56:01 -04:00
|
|
|
# NO_ARGUMENT:: The switch takes no arguments. (:NONE)
|
|
|
|
# REQUIRED_ARGUMENT:: The switch requires an argument. (:REQUIRED)
|
|
|
|
# OPTIONAL_ARGUMENT:: The switch requires an optional argument. (:OPTIONAL)
|
|
|
|
#
|
|
|
|
# Use like --switch=argument (long style) or -Xargument (short style). For
|
2013-05-18 23:10:21 -04:00
|
|
|
# short style, only portion matched to argument pattern is treated as
|
2006-06-28 17:56:01 -04:00
|
|
|
# argument.
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
ArgumentStyle = {}
|
|
|
|
NoArgument.each {|el| ArgumentStyle[el] = Switch::NoArgument}
|
|
|
|
RequiredArgument.each {|el| ArgumentStyle[el] = Switch::RequiredArgument}
|
|
|
|
OptionalArgument.each {|el| ArgumentStyle[el] = Switch::OptionalArgument}
|
|
|
|
ArgumentStyle.freeze
|
|
|
|
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
|
|
|
# Switches common used such as '--', and also provides default
|
|
|
|
# argument classes
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
DefaultList = List.new
|
|
|
|
DefaultList.short['-'] = Switch::NoArgument.new {}
|
|
|
|
DefaultList.long[''] = Switch::NoArgument.new {throw :terminate}
|
|
|
|
|
2010-11-19 07:07:09 -05:00
|
|
|
|
|
|
|
COMPSYS_HEADER = <<'XXX' # :nodoc:
|
|
|
|
|
|
|
|
typeset -A opt_args
|
|
|
|
local context state line
|
|
|
|
|
|
|
|
_arguments -s -S \
|
|
|
|
XXX
|
|
|
|
|
|
|
|
def compsys(to, name = File.basename($0)) # :nodoc:
|
|
|
|
to << "#compdef #{name}\n"
|
|
|
|
to << COMPSYS_HEADER
|
|
|
|
visit(:compsys, {}, {}) {|o, d|
|
2010-12-26 18:30:33 -05:00
|
|
|
to << %Q[ "#{o}[#{d.gsub(/[\"\[\]]/, '\\\\\&')}]" \\\n]
|
2010-11-19 07:07:09 -05:00
|
|
|
}
|
|
|
|
to << " '*:file:_files' && return 0\n"
|
|
|
|
end
|
|
|
|
|
2003-12-28 12:31:10 -05:00
|
|
|
#
|
2004-11-09 00:27:58 -05:00
|
|
|
# Default options for ARGV, which never appear in option summary.
|
|
|
|
#
|
|
|
|
Officious = {}
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# --help
|
|
|
|
# Shows option summary.
|
|
|
|
#
|
2010-12-07 08:31:11 -05:00
|
|
|
Officious['help'] = proc do |parser|
|
|
|
|
Switch::NoArgument.new do |arg|
|
|
|
|
puts parser.help
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-12-12 06:40:29 -05:00
|
|
|
#
|
|
|
|
# --*-completion-bash=WORD
|
2010-11-19 06:26:54 -05:00
|
|
|
# Shows candidates for command line completion.
|
|
|
|
#
|
2010-12-07 08:31:11 -05:00
|
|
|
Officious['*-completion-bash'] = proc do |parser|
|
|
|
|
Switch::RequiredArgument.new do |arg|
|
|
|
|
puts parser.candidate(arg)
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# --*-completion-zsh[=NAME:FILE]
|
2010-11-19 07:07:09 -05:00
|
|
|
# Creates zsh completion file.
|
|
|
|
#
|
2010-12-07 08:31:11 -05:00
|
|
|
Officious['*-completion-zsh'] = proc do |parser|
|
2010-11-19 06:26:54 -05:00
|
|
|
Switch::OptionalArgument.new do |arg|
|
2010-12-07 08:31:11 -05:00
|
|
|
parser.compsys(STDOUT, arg)
|
2004-11-09 00:27:58 -05:00
|
|
|
exit
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# --version
|
|
|
|
# Shows version string if Version is defined.
|
|
|
|
#
|
2004-11-09 00:27:58 -05:00
|
|
|
Officious['version'] = proc do |parser|
|
|
|
|
Switch::OptionalArgument.new do |pkg|
|
|
|
|
if pkg
|
|
|
|
begin
|
|
|
|
require 'optparse/version'
|
|
|
|
rescue LoadError
|
|
|
|
else
|
|
|
|
show_version(*pkg.split(/,/)) or
|
|
|
|
abort("#{parser.program_name}: no version found in package #{pkg}")
|
|
|
|
exit
|
|
|
|
end
|
2003-08-30 23:25:11 -04:00
|
|
|
end
|
2004-11-09 00:27:58 -05:00
|
|
|
v = parser.ver or abort("#{parser.program_name}: version unknown")
|
|
|
|
puts v
|
|
|
|
exit
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-12-28 12:31:10 -05:00
|
|
|
# :startdoc:
|
|
|
|
|
|
|
|
#
|
|
|
|
# Class methods
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Initializes a new instance and evaluates the optional block in context
|
|
|
|
# of the instance. Arguments +args+ are passed to #new, see there for
|
|
|
|
# description of parameters.
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2006-06-28 17:56:01 -04:00
|
|
|
# This method is *deprecated*, its behavior corresponds to the older #new
|
|
|
|
# method.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def self.with(*args, &block)
|
|
|
|
opts = new(*args)
|
|
|
|
opts.instance_eval(&block)
|
|
|
|
opts
|
|
|
|
end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Returns an incremented value of +default+ according to +arg+.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def self.inc(arg, default = nil)
|
|
|
|
case arg
|
|
|
|
when Integer
|
|
|
|
arg.nonzero?
|
|
|
|
when nil
|
|
|
|
default.to_i + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
def inc(*args)
|
2002-10-02 12:45:35 -04:00
|
|
|
self.class.inc(*args)
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Initializes the instance and yields itself if called with a block.
|
|
|
|
#
|
|
|
|
# +banner+:: Banner message.
|
|
|
|
# +width+:: Summary width.
|
|
|
|
# +indent+:: Summary indent.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def initialize(banner = nil, width = 32, indent = ' ' * 4)
|
|
|
|
@stack = [DefaultList, List.new, List.new]
|
|
|
|
@program_name = nil
|
|
|
|
@banner = banner
|
|
|
|
@summary_width = width
|
|
|
|
@summary_indent = indent
|
2005-09-20 18:45:06 -04:00
|
|
|
@default_argv = ARGV
|
2004-11-09 00:27:58 -05:00
|
|
|
add_officious
|
2002-07-12 12:34:02 -04:00
|
|
|
yield self if block_given?
|
|
|
|
end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
def add_officious # :nodoc:
|
2004-11-09 00:27:58 -05:00
|
|
|
list = base()
|
* sprintf.c (rb_str_format): allow %c to print one character
string (e.g. ?x).
* lib/tempfile.rb (Tempfile::make_tmpname): put dot between
basename and pid. [ruby-talk:196272]
* parse.y (do_block): remove -> style block.
* parse.y (parser_yylex): remove tLAMBDA_ARG.
* eval.c (rb_call0): binding for the return event hook should have
consistent scope. [ruby-core:07928]
* eval.c (proc_invoke): return behavior should depend whether it
is surrounded by a lambda or a mere block.
* eval.c (formal_assign): handles post splat arguments.
* eval.c (rb_call0): ditto.
* st.c (strhash): use FNV-1a hash.
* parse.y (parser_yylex): removed experimental ';;' terminator.
* eval.c (rb_node_arity): should be aware of post splat arguments.
* eval.c (rb_proc_arity): ditto.
* parse.y (f_args): syntax rule enhanced to support arguments
after the splat.
* parse.y (block_param): ditto for block parameters.
* parse.y (f_post_arg): mandatory formal arguments after the splat
argument.
* parse.y (new_args_gen): generate nodes for mandatory formal
arguments after the splat argument.
* eval.c (rb_eval): dispatch mandatory formal arguments after the
splat argument.
* parse.y (args): allow more than one splat in the argument list.
* parse.y (method_call): allow aref [] to accept all kind of
method argument, including assocs, splat, and block argument.
* eval.c (SETUP_ARGS0): prepare block argument as well.
* lib/mathn.rb (Integer): remove Integer#gcd2. [ruby-core:07931]
* eval.c (error_line): print receivers true/false/nil specially.
* eval.c (rb_proc_yield): handles parameters in yield semantics.
* eval.c (nil_yield): gives LocalJumpError to denote no block
error.
* io.c (rb_io_getc): now takes one-character string.
* string.c (rb_str_hash): use FNV-1a hash from Fowler/Noll/Vo
hashing algorithm.
* string.c (rb_str_aref): str[0] now returns 1 character string,
instead of a fixnum. [Ruby2]
* parse.y (parser_yylex): ?c now returns 1 character string,
instead of a fixnum. [Ruby2]
* string.c (rb_str_aset): no longer support fixnum insertion.
* eval.c (umethod_bind): should not update original class.
[ruby-dev:28636]
* eval.c (ev_const_get): should support constant access from
within instance_eval(). [ruby-dev:28327]
* time.c (time_timeval): should round for usec floating
number. [ruby-core:07896]
* time.c (time_add): ditto.
* dir.c (sys_warning): should not call a vararg function
rb_sys_warning() indirectly. [ruby-core:07886]
* numeric.c (flo_divmod): the first element of Float#divmod should
be an integer. [ruby-dev:28589]
* test/ruby/test_float.rb: add tests for divmod, div, modulo and remainder.
* re.c (rb_reg_initialize): should not allow modifying literal
regexps. frozen check moved from rb_reg_initialize_m as well.
* re.c (rb_reg_initialize): should not modify untainted objects in
safe levels higher than 3.
* re.c (rb_memcmp): type change from char* to const void*.
* dir.c (dir_close): should not close untainted dir stream.
* dir.c (GetDIR): add tainted/frozen check for each dir operation.
* lib/rdoc/parsers/parse_rb.rb (RDoc::RubyParser::parse_symbol_arg):
typo fixed. a patch from Florian Gross <florg at florg.net>.
* eval.c (EXEC_EVENT_HOOK): trace_func may remove itself from
event_hooks. no guarantee for arbitrary hook deletion.
[ruby-dev:28632]
* util.c (ruby_strtod): differ addition to minimize error.
[ruby-dev:28619]
* util.c (ruby_strtod): should not raise ERANGE when the input
string does not have any digits. [ruby-dev:28629]
* eval.c (proc_invoke): should restore old ruby_frame->block.
thanks to ts <decoux at moulon.inra.fr>. [ruby-core:07833]
also fix [ruby-dev:28614] as well.
* signal.c (trap): sig should be less then NSIG. Coverity found
this bug. a patch from Kevin Tew <tewk at tewk.com>.
[ruby-core:07823]
* math.c (math_log2): add new method inspired by
[ruby-talk:191237].
* math.c (math_log): add optional base argument to Math::log().
[ruby-talk:191308]
* ext/syck/emitter.c (syck_scan_scalar): avoid accessing
uninitialized array element. a patch from Pat Eyler
<rubypate at gmail.com>. [ruby-core:07809]
* array.c (rb_ary_fill): initialize local variables first. a
patch from Pat Eyler <rubypate at gmail.com>. [ruby-core:07810]
* ext/syck/yaml2byte.c (syck_yaml2byte_handler): need to free
type_tag. a patch from Pat Eyler <rubypate at gmail.com>.
[ruby-core:07808]
* ext/socket/socket.c (make_hostent_internal): accept ai_family
check from Sam Roberts <sroberts at uniserve.com>.
[ruby-core:07691]
* util.c (ruby_strtod): should not cut off 18 digits for no
reason. [ruby-core:07796]
* array.c (rb_ary_fill): internalize local variable "beg" to
pacify Coverity. [ruby-core:07770]
* pack.c (pack_unpack): now supports CRLF newlines. a patch from
<tommy at tmtm.org>. [ruby-dev:28601]
* applied code clean-up patch from Stefan Huehner
<stefan at huehner.org>. [ruby-core:07764]
* lib/jcode.rb (String::tr_s): should have translated non
squeezing character sequence (i.e. a character) as well. thanks
to Hiroshi Ichikawa <gimite at gimite.ddo.jp> [ruby-list:42090]
* ext/socket/socket.c: document update patch from Sam Roberts
<sroberts at uniserve.com>. [ruby-core:07701]
* lib/mathn.rb (Integer): need not to remove gcd2. a patch from
NARUSE, Yui <naruse at airemix.com>. [ruby-dev:28570]
* parse.y (arg): too much NEW_LIST()
* eval.c (SETUP_ARGS0): remove unnecessary access to nd_alen.
* eval.c (rb_eval): use ARGSCAT for NODE_OP_ASGN1.
[ruby-dev:28585]
* parse.y (arg): use NODE_ARGSCAT for placeholder.
* lib/getoptlong.rb (GetoptLong::get): RDoc update patch from
mathew <meta at pobox.com>. [ruby-core:07738]
* variable.c (rb_const_set): raise error when no target klass is
supplied. [ruby-dev:28582]
* prec.c (prec_prec_f): documentation patch from
<gerardo.santana at gmail.com>. [ruby-core:07689]
* bignum.c (rb_big_pow): second operand may be too big even if
it's a Fixnum. [ruby-talk:187984]
* README.EXT: update symbol description. [ruby-talk:188104]
* COPYING: explicitly note GPLv2. [ruby-talk:187922]
* parse.y: remove some obsolete syntax rules (unparenthesized
method calls in argument list).
* eval.c (rb_call0): insecure calling should be checked for non
NODE_SCOPE method invocations too.
* eval.c (rb_alias): should preserve the current safe level as
well as method definition.
* process.c (rb_f_sleep): remove RDoc description about SIGALRM
which is not valid on the current implementation. [ruby-dev:28464]
Thu Mar 23 21:40:47 2006 K.Kosako <sndgk393 AT ybb.ne.jp>
* eval.c (method_missing): should support argument splat in
super. a bug in combination of super, splat and
method_missing. [ruby-talk:185438]
* configure.in: Solaris SunPro compiler -rapth patch from
<kuwa at labs.fujitsu.com>. [ruby-dev:28443]
* configure.in: remove enable_rpath=no for Solaris.
[ruby-dev:28440]
* ext/win32ole/win32ole.c (ole_val2olevariantdata): change behavior
of converting OLE Variant object with VT_ARRAY|VT_UI1 and Ruby
String object.
* ruby.1: a clarification patch from David Lutterkort
<dlutter at redhat.com>. [ruby-core:7508]
* lib/rdoc/ri/ri_paths.rb (RI::Paths): adding paths from rubygems
directories. a patch from Eric Hodel <drbrain at segment7.net>.
[ruby-core:07423]
* eval.c (rb_clear_cache_by_class): clearing wrong cache.
* ext/extmk.rb: use :remove_destination to install extension libraries
to avoid SEGV. [ruby-dev:28417]
* eval.c (rb_thread_fd_writable): should not re-schedule output
from KILLED thread (must be error printing).
* array.c (rb_ary_flatten_bang): allow specifying recursion
level. [ruby-talk:182170]
* array.c (rb_ary_flatten): ditto.
* gc.c (add_heap): a heap_slots may overflow. a patch from Stefan
Weil <weil at mail.berlios.de>.
* eval.c (rb_call): use separate cache for fcall/vcall
invocation.
* eval.c (rb_eval): NODE_FCALL, NODE_VCALL can call local
functions.
* eval.c (rb_mod_local): a new method to specify newly added
visibility "local".
* eval.c (search_method): search for local methods which are
visible only from the current class.
* class.c (rb_class_local_methods): a method to list local methods.
* object.c (Init_Object): add BasicObject class as a top level
BlankSlate class.
* ruby.h (SYM2ID): should not cast to signed long.
[ruby-core:07414]
* class.c (rb_include_module): allow module duplication.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10235 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2006-06-09 17:20:17 -04:00
|
|
|
Officious.each do |opt, block|
|
2006-09-07 19:25:56 -04:00
|
|
|
list.long[opt] ||= block.call(self)
|
2004-11-09 00:27:58 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Terminates option parsing. Optional parameter +arg+ is a string pushed
|
|
|
|
# back to be the first non-option argument.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def terminate(arg = nil)
|
2002-10-02 12:45:35 -04:00
|
|
|
self.class.terminate(arg)
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
def self.terminate(arg = nil)
|
|
|
|
throw :terminate, arg
|
|
|
|
end
|
|
|
|
|
|
|
|
@stack = [DefaultList]
|
|
|
|
def self.top() DefaultList end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Directs to accept specified class +t+. The argument string is passed to
|
|
|
|
# the block in which it should be converted to the desired class.
|
|
|
|
#
|
|
|
|
# +t+:: Argument class specifier, any object including Class.
|
|
|
|
# +pat+:: Pattern for argument, defaults to +t+ if it responds to match.
|
|
|
|
#
|
|
|
|
# accept(t, pat, &block)
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def accept(*args, &blk) top.accept(*args, &blk) end
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# See #accept.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def self.accept(*args, &blk) top.accept(*args, &blk) end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Directs to reject specified class argument.
|
|
|
|
#
|
2008-06-04 05:37:38 -04:00
|
|
|
# +t+:: Argument class specifier, any object including Class.
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# reject(t)
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def reject(*args, &blk) top.reject(*args, &blk) end
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# See #reject.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def self.reject(*args, &blk) top.reject(*args, &blk) end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Instance methods
|
|
|
|
#
|
|
|
|
|
|
|
|
# Heading banner preceding summary.
|
|
|
|
attr_writer :banner
|
|
|
|
|
|
|
|
# Program name to be emitted in error message and default banner,
|
|
|
|
# defaults to $0.
|
|
|
|
attr_writer :program_name
|
|
|
|
|
|
|
|
# Width for option list portion of summary. Must be Numeric.
|
|
|
|
attr_accessor :summary_width
|
|
|
|
|
|
|
|
# Indentation for summary. Must be String (or have + String method).
|
|
|
|
attr_accessor :summary_indent
|
|
|
|
|
|
|
|
# Strings to be parsed in default.
|
2005-09-20 18:45:06 -04:00
|
|
|
attr_accessor :default_argv
|
2002-07-12 12:34:02 -04:00
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Heading banner preceding summary.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def banner
|
2005-04-12 06:30:45 -04:00
|
|
|
unless @banner
|
|
|
|
@banner = "Usage: #{program_name} [options]"
|
2006-09-26 10:57:48 -04:00
|
|
|
visit(:add_banner, @banner)
|
2005-04-12 06:30:45 -04:00
|
|
|
end
|
|
|
|
@banner
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Program name to be emitted in error message and default banner, defaults
|
|
|
|
# to $0.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def program_name
|
|
|
|
@program_name || File.basename($0, '.*')
|
|
|
|
end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
# for experimental cascading :-)
|
2002-07-12 12:34:02 -04:00
|
|
|
alias set_banner banner=
|
|
|
|
alias set_program_name program_name=
|
|
|
|
alias set_summary_width summary_width=
|
|
|
|
alias set_summary_indent summary_indent=
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
# Version
|
|
|
|
attr_writer :version
|
|
|
|
# Release code
|
|
|
|
attr_writer :release
|
2002-07-12 12:34:02 -04:00
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Version
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def version
|
2004-11-09 00:27:58 -05:00
|
|
|
@version || (defined?(::Version) && ::Version)
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Release code
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def release
|
2003-09-08 07:00:17 -04:00
|
|
|
@release || (defined?(::Release) && ::Release) || (defined?(::RELEASE) && ::RELEASE)
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Returns version string from program_name, version and release.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def ver
|
|
|
|
if v = version
|
|
|
|
str = "#{program_name} #{[v].join('.')}"
|
|
|
|
str << " (#{v})" if v = release
|
|
|
|
str
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-09-05 11:15:43 -04:00
|
|
|
def warn(mesg = $!)
|
2004-07-15 10:54:01 -04:00
|
|
|
super("#{program_name}: #{mesg}")
|
2003-09-05 11:15:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def abort(mesg = $!)
|
2004-07-15 10:54:01 -04:00
|
|
|
super("#{program_name}: #{mesg}")
|
2003-09-05 11:15:43 -04:00
|
|
|
end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Subject of #on / #on_head, #accept / #reject
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def top
|
|
|
|
@stack[-1]
|
|
|
|
end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Subject of #on_tail.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def base
|
|
|
|
@stack[1]
|
|
|
|
end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Pushes a new List.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def new
|
|
|
|
@stack.push(List.new)
|
|
|
|
if block_given?
|
|
|
|
yield self
|
|
|
|
else
|
|
|
|
self
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Removes the last List.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def remove
|
|
|
|
@stack.pop
|
|
|
|
end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Puts option summary into +to+ and returns +to+. Yields each line if
|
|
|
|
# a block is given.
|
|
|
|
#
|
|
|
|
# +to+:: Output destination, which must have method <<. Defaults to [].
|
|
|
|
# +width+:: Width of left side, defaults to @summary_width.
|
|
|
|
# +max+:: Maximum length allowed for left side, defaults to +width+ - 1.
|
|
|
|
# +indent+:: Indentation, defaults to @summary_indent.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def summarize(to = [], width = @summary_width, max = width - 1, indent = @summary_indent, &blk)
|
2008-12-26 01:50:55 -05:00
|
|
|
blk ||= proc {|l| to << (l.index($/, -1) ? l : l + $/)}
|
|
|
|
visit(:summarize, {}, {}, width, max, indent, &blk)
|
2002-07-12 12:34:02 -04:00
|
|
|
to
|
|
|
|
end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Returns option summary string.
|
|
|
|
#
|
2012-04-24 01:54:08 -04:00
|
|
|
def help; summarize("#{banner}".sub(/\n?\z/, "\n")) end
|
2003-09-04 22:32:24 -04:00
|
|
|
alias to_s help
|
2002-07-12 12:34:02 -04:00
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Returns option summary list.
|
|
|
|
#
|
2012-04-24 16:46:14 -04:00
|
|
|
def to_a; summarize("#{banner}".split(/^/)) end
|
2002-07-12 12:34:02 -04:00
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Checks if an argument is given twice, in which case an ArgumentError is
|
|
|
|
# raised. Called from OptionParser#switch only.
|
|
|
|
#
|
|
|
|
# +obj+:: New argument.
|
|
|
|
# +prv+:: Previously specified argument.
|
|
|
|
# +msg+:: Exception message.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def notwice(obj, prv, msg)
|
|
|
|
unless !prv or prv == obj
|
2008-10-09 11:21:42 -04:00
|
|
|
raise(ArgumentError, "argument #{msg} given twice: #{obj}",
|
|
|
|
ParseError.filter_backtrace(caller(2)))
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
obj
|
|
|
|
end
|
|
|
|
private :notwice
|
|
|
|
|
2013-05-19 14:36:37 -04:00
|
|
|
SPLAT_PROC = proc {|*a| a.length <= 1 ? a.first : a} # :nodoc:
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# 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:
|
|
|
|
#
|
|
|
|
# [Argument style:]
|
|
|
|
# One of the following:
|
|
|
|
# :NONE, :REQUIRED, :OPTIONAL
|
|
|
|
#
|
|
|
|
# [Argument pattern:]
|
|
|
|
# Acceptable option argument format, must be pre-defined with
|
|
|
|
# OptionParser.accept or OptionParser#accept, or Regexp. This can appear
|
|
|
|
# once or assigned as String if not present, otherwise causes an
|
|
|
|
# ArgumentError. Examples:
|
|
|
|
# Float, Time, Array
|
|
|
|
#
|
|
|
|
# [Possible argument values:]
|
|
|
|
# Hash or Array.
|
|
|
|
# [:text, :binary, :auto]
|
|
|
|
# %w[iso-2022-jp shift_jis euc-jp utf8 binary]
|
|
|
|
# { "jis" => "iso-2022-jp", "sjis" => "shift_jis" }
|
|
|
|
#
|
|
|
|
# [Long style switch:]
|
|
|
|
# Specifies a long style switch which takes a mandatory, optional or no
|
|
|
|
# argument. It's a string of the following form:
|
|
|
|
# "--switch=MANDATORY" or "--switch MANDATORY"
|
|
|
|
# "--switch[=OPTIONAL]"
|
|
|
|
# "--switch"
|
|
|
|
#
|
|
|
|
# [Short style switch:]
|
|
|
|
# Specifies short style switch which takes a mandatory, optional or no
|
|
|
|
# argument. It's a string of the following form:
|
|
|
|
# "-xMANDATORY"
|
|
|
|
# "-x[OPTIONAL]"
|
|
|
|
# "-x"
|
|
|
|
# There is also a special form which matches character range (not full
|
2008-06-04 05:37:38 -04:00
|
|
|
# set of regular expression):
|
2006-06-28 17:56:01 -04:00
|
|
|
# "-[a-z]MANDATORY"
|
2009-03-05 22:56:38 -05:00
|
|
|
# "-[a-z][OPTIONAL]"
|
2006-06-28 17:56:01 -04:00
|
|
|
# "-[a-z]"
|
|
|
|
#
|
|
|
|
# [Argument style and description:]
|
2008-06-04 05:37:38 -04:00
|
|
|
# Instead of specifying mandatory or optional arguments directly in the
|
2006-06-28 17:56:01 -04:00
|
|
|
# switch parameter, this separate parameter can be used.
|
|
|
|
# "=MANDATORY"
|
|
|
|
# "=[OPTIONAL]"
|
|
|
|
#
|
|
|
|
# [Description:]
|
|
|
|
# Description string for the option.
|
|
|
|
# "Run verbosely"
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2006-06-28 17:56:01 -04:00
|
|
|
# [Handler:]
|
|
|
|
# Handler for the parsed argument value. Either give a block or pass a
|
|
|
|
# Proc or Method as an argument.
|
|
|
|
#
|
2006-10-01 10:13:16 -04:00
|
|
|
def make_switch(opts, block = nil)
|
2002-07-12 12:34:02 -04:00
|
|
|
short, long, nolong, style, pattern, conv, not_pattern, not_conv, not_style = [], [], []
|
|
|
|
ldesc, sdesc, desc, arg = [], [], []
|
|
|
|
default_style = Switch::NoArgument
|
|
|
|
default_pattern = nil
|
|
|
|
klass = nil
|
2010-08-10 08:02:52 -04:00
|
|
|
q, a = nil
|
2002-07-12 12:34:02 -04:00
|
|
|
|
|
|
|
opts.each do |o|
|
|
|
|
# argument class
|
|
|
|
next if search(:atype, o) do |pat, c|
|
2003-12-23 08:27:42 -05:00
|
|
|
klass = notwice(o, klass, 'type')
|
|
|
|
if not_style and not_style != Switch::NoArgument
|
|
|
|
not_pattern, not_conv = pat, c
|
|
|
|
else
|
|
|
|
default_pattern, conv = pat, c
|
|
|
|
end
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# directly specified pattern(any object possible to match)
|
2008-11-08 03:17:20 -05:00
|
|
|
if (!(String === o || Symbol === o)) and o.respond_to?(:match)
|
2003-12-23 08:27:42 -05:00
|
|
|
pattern = notwice(o, pattern, 'pattern')
|
2008-11-17 19:42:18 -05:00
|
|
|
if pattern.respond_to?(:convert)
|
|
|
|
conv = pattern.method(:convert).to_proc
|
|
|
|
else
|
|
|
|
conv = SPLAT_PROC
|
|
|
|
end
|
2003-12-23 08:27:42 -05:00
|
|
|
next
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# anything others
|
|
|
|
case o
|
|
|
|
when Proc, Method
|
2003-12-23 08:27:42 -05:00
|
|
|
block = notwice(o, block, 'block')
|
2002-07-12 12:34:02 -04:00
|
|
|
when Array, Hash
|
2003-12-23 08:27:42 -05:00
|
|
|
case pattern
|
|
|
|
when CompletingHash
|
|
|
|
when nil
|
|
|
|
pattern = CompletingHash.new
|
2007-04-19 04:48:03 -04:00
|
|
|
conv = pattern.method(:convert).to_proc if pattern.respond_to?(:convert)
|
2003-12-23 08:27:42 -05:00
|
|
|
else
|
|
|
|
raise ArgumentError, "argument pattern given twice"
|
|
|
|
end
|
2006-10-09 10:41:24 -04:00
|
|
|
o.each {|pat, *v| pattern[pat] = v.fetch(0) {pat}}
|
2002-07-12 12:34:02 -04:00
|
|
|
when Module
|
2008-10-09 11:21:42 -04:00
|
|
|
raise ArgumentError, "unsupported argument type: #{o}", ParseError.filter_backtrace(caller(4))
|
2002-07-12 12:34:02 -04:00
|
|
|
when *ArgumentStyle.keys
|
2003-12-23 08:27:42 -05:00
|
|
|
style = notwice(ArgumentStyle[o], style, 'style')
|
2003-03-21 23:31:24 -05:00
|
|
|
when /^--no-([^\[\]=\s]*)(.+)?/
|
2003-12-23 08:27:42 -05:00
|
|
|
q, a = $1, $2
|
|
|
|
o = notwice(a ? Object : TrueClass, klass, 'type')
|
|
|
|
not_pattern, not_conv = search(:atype, o) unless not_style
|
|
|
|
not_style = (not_style || default_style).guess(arg = a) if a
|
|
|
|
default_style = Switch::NoArgument
|
|
|
|
default_pattern, conv = search(:atype, FalseClass) unless default_pattern
|
|
|
|
ldesc << "--no-#{q}"
|
|
|
|
long << 'no-' + (q = q.downcase)
|
|
|
|
nolong << q
|
2003-03-21 23:31:24 -05:00
|
|
|
when /^--\[no-\]([^\[\]=\s]*)(.+)?/
|
2003-12-23 08:27:42 -05:00
|
|
|
q, a = $1, $2
|
|
|
|
o = notwice(a ? Object : TrueClass, klass, 'type')
|
|
|
|
if a
|
|
|
|
default_style = default_style.guess(arg = a)
|
|
|
|
default_pattern, conv = search(:atype, o) unless default_pattern
|
|
|
|
end
|
|
|
|
ldesc << "--[no-]#{q}"
|
|
|
|
long << (o = q.downcase)
|
|
|
|
not_pattern, not_conv = search(:atype, FalseClass) unless not_style
|
|
|
|
not_style = Switch::NoArgument
|
|
|
|
nolong << 'no-' + o
|
2003-03-21 23:31:24 -05:00
|
|
|
when /^--([^\[\]=\s]*)(.+)?/
|
2003-12-23 08:27:42 -05:00
|
|
|
q, a = $1, $2
|
|
|
|
if a
|
|
|
|
o = notwice(NilClass, klass, 'type')
|
|
|
|
default_style = default_style.guess(arg = a)
|
|
|
|
default_pattern, conv = search(:atype, o) unless default_pattern
|
|
|
|
end
|
|
|
|
ldesc << "--#{q}"
|
|
|
|
long << (o = q.downcase)
|
2002-07-12 12:34:02 -04:00
|
|
|
when /^-(\[\^?\]?(?:[^\\\]]|\\.)*\])(.+)?/
|
2003-12-23 08:27:42 -05:00
|
|
|
q, a = $1, $2
|
|
|
|
o = notwice(Object, klass, 'type')
|
|
|
|
if a
|
|
|
|
default_style = default_style.guess(arg = a)
|
|
|
|
default_pattern, conv = search(:atype, o) unless default_pattern
|
|
|
|
end
|
|
|
|
sdesc << "-#{q}"
|
|
|
|
short << Regexp.new(q)
|
2002-07-12 12:34:02 -04:00
|
|
|
when /^-(.)(.+)?/
|
2003-12-23 08:27:42 -05:00
|
|
|
q, a = $1, $2
|
|
|
|
if a
|
|
|
|
o = notwice(NilClass, klass, 'type')
|
|
|
|
default_style = default_style.guess(arg = a)
|
|
|
|
default_pattern, conv = search(:atype, o) unless default_pattern
|
|
|
|
end
|
|
|
|
sdesc << "-#{q}"
|
|
|
|
short << q
|
2002-07-12 12:34:02 -04:00
|
|
|
when /^=/
|
2003-12-23 08:27:42 -05:00
|
|
|
style = notwice(default_style.guess(arg = o), style, 'style')
|
|
|
|
default_pattern, conv = search(:atype, Object) unless default_pattern
|
2002-07-12 12:34:02 -04:00
|
|
|
else
|
2003-12-23 08:27:42 -05:00
|
|
|
desc.push(o)
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-01-30 19:41:40 -05:00
|
|
|
default_pattern, conv = search(:atype, default_style.pattern) unless default_pattern
|
2005-04-12 06:30:45 -04:00
|
|
|
if !(short.empty? and long.empty?)
|
|
|
|
s = (style || default_style).new(pattern || default_pattern,
|
2003-12-23 08:27:42 -05:00
|
|
|
conv, sdesc, ldesc, arg, desc, block)
|
2006-09-26 10:57:48 -04:00
|
|
|
elsif !block
|
2008-10-09 11:21:42 -04:00
|
|
|
if style or pattern
|
|
|
|
raise ArgumentError, "no switch given", ParseError.filter_backtrace(caller)
|
|
|
|
end
|
2005-04-12 06:30:45 -04:00
|
|
|
s = desc
|
|
|
|
else
|
|
|
|
short << pattern
|
|
|
|
s = (style || default_style).new(pattern,
|
2005-04-13 09:12:24 -04:00
|
|
|
conv, nil, nil, arg, desc, block)
|
2005-04-12 06:30:45 -04:00
|
|
|
end
|
2002-07-12 12:34:02 -04:00
|
|
|
return s, short, long,
|
|
|
|
(not_style.new(not_pattern, not_conv, sdesc, ldesc, nil, desc, block) if not_style),
|
|
|
|
nolong
|
|
|
|
end
|
|
|
|
|
2002-12-02 04:40:44 -05:00
|
|
|
def define(*opts, &block)
|
2006-10-01 10:13:16 -04:00
|
|
|
top.append(*(sw = make_switch(opts, block)))
|
2002-12-01 09:10:51 -05:00
|
|
|
sw[0]
|
|
|
|
end
|
2006-06-28 17:56:01 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Add option switch and handler. See #make_switch for an explanation of
|
|
|
|
# parameters.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def on(*opts, &block)
|
2002-12-02 04:40:44 -05:00
|
|
|
define(*opts, &block)
|
2002-07-12 12:34:02 -04:00
|
|
|
self
|
|
|
|
end
|
2002-12-02 04:40:44 -05:00
|
|
|
alias def_option define
|
2002-07-12 12:34:02 -04:00
|
|
|
|
2002-12-02 04:40:44 -05:00
|
|
|
def define_head(*opts, &block)
|
2006-10-01 10:13:16 -04:00
|
|
|
top.prepend(*(sw = make_switch(opts, block)))
|
2002-12-01 09:10:51 -05:00
|
|
|
sw[0]
|
|
|
|
end
|
2006-06-28 17:56:01 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Add option switch like with #on, but at head of summary.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def on_head(*opts, &block)
|
2002-12-02 04:40:44 -05:00
|
|
|
define_head(*opts, &block)
|
2002-07-12 12:34:02 -04:00
|
|
|
self
|
|
|
|
end
|
2002-12-02 04:40:44 -05:00
|
|
|
alias def_head_option define_head
|
2002-07-12 12:34:02 -04:00
|
|
|
|
2002-12-02 04:40:44 -05:00
|
|
|
def define_tail(*opts, &block)
|
2006-10-01 10:13:16 -04:00
|
|
|
base.append(*(sw = make_switch(opts, block)))
|
2002-12-01 09:10:51 -05:00
|
|
|
sw[0]
|
|
|
|
end
|
2006-06-28 17:56:01 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Add option switch like with #on, but at tail of summary.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def on_tail(*opts, &block)
|
2002-12-02 04:40:44 -05:00
|
|
|
define_tail(*opts, &block)
|
2002-07-12 12:34:02 -04:00
|
|
|
self
|
|
|
|
end
|
2002-12-02 04:40:44 -05:00
|
|
|
alias def_tail_option define_tail
|
2002-12-01 09:10:51 -05:00
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Add separator in summary.
|
|
|
|
#
|
2002-12-01 09:10:51 -05:00
|
|
|
def separator(string)
|
|
|
|
top.append(string, nil, nil)
|
|
|
|
end
|
2002-07-12 12:34:02 -04:00
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Parses command line arguments +argv+ in order. When a block is given,
|
|
|
|
# each non-option argument is yielded.
|
|
|
|
#
|
|
|
|
# Returns the rest of +argv+ left unparsed.
|
|
|
|
#
|
2003-09-05 11:15:43 -04:00
|
|
|
def order(*argv, &block)
|
|
|
|
argv = argv[0].dup if argv.size == 1 and Array === argv[0]
|
|
|
|
order!(argv, &block)
|
|
|
|
end
|
2002-07-12 12:34:02 -04:00
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Same as #order, but removes switches destructively.
|
2012-12-22 11:36:30 -05:00
|
|
|
# Non-option arguments remain in +argv+.
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
2005-09-20 18:45:06 -04:00
|
|
|
def order!(argv = default_argv, &nonopt)
|
2006-09-12 12:14:50 -04:00
|
|
|
parse_in_order(argv, &nonopt)
|
|
|
|
end
|
|
|
|
|
2006-12-08 04:13:04 -05:00
|
|
|
def parse_in_order(argv = default_argv, setter = nil, &nonopt) # :nodoc:
|
2006-10-09 10:41:24 -04:00
|
|
|
opt, arg, val, rest = nil
|
|
|
|
nonopt ||= proc {|a| throw :terminate, a}
|
2002-07-12 12:34:02 -04:00
|
|
|
argv.unshift(arg) if arg = catch(:terminate) {
|
|
|
|
while arg = argv.shift
|
2003-12-23 08:27:42 -05:00
|
|
|
case arg
|
|
|
|
# long option
|
2009-01-31 04:03:43 -05:00
|
|
|
when /\A--([^=]*)(?:=(.*))?/m
|
2003-12-23 08:27:42 -05:00
|
|
|
opt, rest = $1, $2
|
|
|
|
begin
|
2004-12-05 05:39:58 -05:00
|
|
|
sw, = complete(:long, opt, true)
|
2003-12-23 08:27:42 -05:00
|
|
|
rescue ParseError
|
|
|
|
raise $!.set_option(arg, true)
|
|
|
|
end
|
|
|
|
begin
|
2006-09-26 10:57:48 -04:00
|
|
|
opt, cb, val = sw.parse(rest, argv) {|*exc| raise(*exc)}
|
2008-12-21 00:55:20 -05:00
|
|
|
val = cb.call(val) if cb
|
2006-09-12 12:14:50 -04:00
|
|
|
setter.call(sw.switch_name, val) if setter
|
2003-12-23 08:27:42 -05:00
|
|
|
rescue ParseError
|
|
|
|
raise $!.set_option(arg, rest)
|
|
|
|
end
|
|
|
|
|
|
|
|
# short option
|
2009-01-31 04:03:43 -05:00
|
|
|
when /\A-(.)((=).*|.+)?/m
|
2003-12-23 08:27:42 -05:00
|
|
|
opt, has_arg, eq, val, rest = $1, $3, $3, $2, $2
|
|
|
|
begin
|
2006-06-09 19:18:04 -04:00
|
|
|
sw, = search(:short, opt)
|
|
|
|
unless sw
|
2003-12-23 08:27:42 -05:00
|
|
|
begin
|
|
|
|
sw, = complete(:short, opt)
|
|
|
|
# short option matched.
|
|
|
|
val = arg.sub(/\A-/, '')
|
|
|
|
has_arg = true
|
|
|
|
rescue InvalidOption
|
|
|
|
# if no short options match, try completion with long
|
|
|
|
# options.
|
|
|
|
sw, = complete(:long, opt)
|
|
|
|
eq ||= !rest
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue ParseError
|
|
|
|
raise $!.set_option(arg, true)
|
|
|
|
end
|
|
|
|
begin
|
2006-09-12 12:14:50 -04:00
|
|
|
opt, cb, val = sw.parse(val, argv) {|*exc| raise(*exc) if eq}
|
2003-12-23 08:27:42 -05:00
|
|
|
raise InvalidOption, arg if has_arg and !eq and arg == "-#{opt}"
|
2009-04-26 02:13:11 -04:00
|
|
|
argv.unshift(opt) if opt and (!rest or (opt = opt.sub(/\A-*/, '-')) != '-')
|
2008-12-21 00:55:20 -05:00
|
|
|
val = cb.call(val) if cb
|
2006-09-12 12:14:50 -04:00
|
|
|
setter.call(sw.switch_name, val) if setter
|
2003-12-23 08:27:42 -05:00
|
|
|
rescue ParseError
|
|
|
|
raise $!.set_option(arg, arg.length > 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
# non-option argument
|
|
|
|
else
|
2005-04-12 06:30:45 -04:00
|
|
|
catch(:prune) do
|
2006-10-09 10:41:24 -04:00
|
|
|
visit(:each_option) do |sw0|
|
|
|
|
sw = sw0
|
2006-09-07 19:25:56 -04:00
|
|
|
sw.block.call(arg) if Switch === sw and sw.match_nonswitch?(arg)
|
2005-04-12 06:30:45 -04:00
|
|
|
end
|
2006-09-07 19:25:56 -04:00
|
|
|
nonopt.call(arg)
|
2005-04-12 06:30:45 -04:00
|
|
|
end
|
2003-12-23 08:27:42 -05:00
|
|
|
end
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
nil
|
|
|
|
}
|
|
|
|
|
2006-09-09 20:20:03 -04:00
|
|
|
visit(:search, :short, nil) {|sw| sw.block.call(*argv) if !sw.pattern}
|
2005-04-12 06:30:45 -04:00
|
|
|
|
2002-07-12 12:34:02 -04:00
|
|
|
argv
|
|
|
|
end
|
2006-09-26 10:57:48 -04:00
|
|
|
private :parse_in_order
|
2002-07-12 12:34:02 -04:00
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Parses command line arguments +argv+ in permutation mode and returns
|
|
|
|
# list of non-option arguments.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def permute(*argv)
|
2003-09-05 11:15:43 -04:00
|
|
|
argv = argv[0].dup if argv.size == 1 and Array === argv[0]
|
2002-07-12 12:34:02 -04:00
|
|
|
permute!(argv)
|
|
|
|
end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Same as #permute, but removes switches destructively.
|
2012-12-22 11:36:30 -05:00
|
|
|
# Non-option arguments remain in +argv+.
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
2005-09-20 18:45:06 -04:00
|
|
|
def permute!(argv = default_argv)
|
2002-07-12 12:34:02 -04:00
|
|
|
nonopts = []
|
2006-10-09 10:41:24 -04:00
|
|
|
order!(argv, &nonopts.method(:<<))
|
2002-07-12 12:34:02 -04:00
|
|
|
argv[0, 0] = nonopts
|
|
|
|
argv
|
|
|
|
end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Parses command line arguments +argv+ in order when environment variable
|
|
|
|
# POSIXLY_CORRECT is set, and in permutation mode otherwise.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def parse(*argv)
|
2003-09-05 11:15:43 -04:00
|
|
|
argv = argv[0].dup if argv.size == 1 and Array === argv[0]
|
2002-07-12 12:34:02 -04:00
|
|
|
parse!(argv)
|
|
|
|
end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Same as #parse, but removes switches destructively.
|
2012-12-22 11:36:30 -05:00
|
|
|
# Non-option arguments remain in +argv+.
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
2005-09-20 18:45:06 -04:00
|
|
|
def parse!(argv = default_argv)
|
2002-07-12 12:34:02 -04:00
|
|
|
if ENV.include?('POSIXLY_CORRECT')
|
|
|
|
order!(argv)
|
|
|
|
else
|
|
|
|
permute!(argv)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Wrapper method for getopts.rb.
|
|
|
|
#
|
2006-09-26 10:57:48 -04:00
|
|
|
# params = ARGV.getopts("ab:", "foo", "bar:")
|
|
|
|
# # params[:a] = true # -a
|
|
|
|
# # params[:b] = "1" # -b1
|
|
|
|
# # params[:foo] = "1" # --foo
|
|
|
|
# # params[:bar] = "x" # --bar x
|
|
|
|
#
|
|
|
|
def getopts(*args)
|
|
|
|
argv = Array === args.first ? args.shift : default_argv
|
|
|
|
single_options, *long_options = *args
|
|
|
|
|
2006-06-10 19:31:32 -04:00
|
|
|
result = {}
|
|
|
|
|
|
|
|
single_options.scan(/(.)(:)?/) do |opt, val|
|
|
|
|
if val
|
|
|
|
result[opt] = nil
|
2006-09-12 12:14:50 -04:00
|
|
|
define("-#{opt} VAL")
|
2006-06-10 19:31:32 -04:00
|
|
|
else
|
|
|
|
result[opt] = false
|
2006-09-12 12:14:50 -04:00
|
|
|
define("-#{opt}")
|
2006-06-10 19:31:32 -04:00
|
|
|
end
|
|
|
|
end if single_options
|
|
|
|
|
|
|
|
long_options.each do |arg|
|
|
|
|
opt, val = arg.split(':', 2)
|
|
|
|
if val
|
|
|
|
result[opt] = val.empty? ? nil : val
|
2006-09-12 12:14:50 -04:00
|
|
|
define("--#{opt} VAL")
|
2006-06-10 19:31:32 -04:00
|
|
|
else
|
|
|
|
result[opt] = false
|
2006-09-12 12:14:50 -04:00
|
|
|
define("--#{opt}")
|
2006-06-10 19:31:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-09-12 12:14:50 -04:00
|
|
|
parse_in_order(argv, result.method(:[]=))
|
2006-06-10 19:31:32 -04:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# See #getopts.
|
|
|
|
#
|
2006-06-10 19:31:32 -04:00
|
|
|
def self.getopts(*args)
|
|
|
|
new.getopts(*args)
|
|
|
|
end
|
2006-06-28 17:56:01 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Traverses @stack, sending each element method +id+ with +args+ and
|
|
|
|
# +block+.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def visit(id, *args, &block)
|
|
|
|
@stack.reverse_each do |el|
|
|
|
|
el.send(id, *args, &block)
|
|
|
|
end
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
private :visit
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Searches +key+ in @stack for +id+ hash and returns or yields the result.
|
|
|
|
#
|
2006-02-03 04:15:42 -05:00
|
|
|
def search(id, key)
|
2006-09-26 10:57:48 -04:00
|
|
|
block_given = block_given?
|
2006-06-21 04:08:36 -04:00
|
|
|
visit(:search, id, key) do |k|
|
2006-09-26 10:57:48 -04:00
|
|
|
return block_given ? yield(k) : k
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
private :search
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Completes shortened long style option switch and returns pair of
|
|
|
|
# canonical switch and switch descriptor OptionParser::Switch.
|
|
|
|
#
|
|
|
|
# +id+:: Searching table.
|
|
|
|
# +opt+:: Searching key.
|
|
|
|
# +icase+:: Search case insensitive if true.
|
|
|
|
# +pat+:: Optional pattern for completion.
|
|
|
|
#
|
2004-12-05 05:39:58 -05:00
|
|
|
def complete(typ, opt, icase = false, *pat)
|
2002-07-12 12:34:02 -04:00
|
|
|
if pat.empty?
|
|
|
|
search(typ, opt) {|sw| return [sw, opt]} # exact match or...
|
|
|
|
end
|
|
|
|
raise AmbiguousOption, catch(:ambiguous) {
|
2006-10-09 10:41:24 -04:00
|
|
|
visit(:complete, typ, opt, icase, *pat) {|o, *sw| return sw}
|
2002-07-12 12:34:02 -04:00
|
|
|
raise InvalidOption, opt
|
|
|
|
}
|
|
|
|
end
|
|
|
|
private :complete
|
|
|
|
|
2010-11-19 06:26:54 -05:00
|
|
|
def candidate(word)
|
|
|
|
list = []
|
|
|
|
case word
|
|
|
|
when /\A--/
|
|
|
|
word, arg = word.split(/=/, 2)
|
|
|
|
argpat = Completion.regexp(arg, false) if arg and !arg.empty?
|
|
|
|
long = true
|
|
|
|
when /\A-(!-)/
|
|
|
|
short = true
|
|
|
|
when /\A-/
|
|
|
|
long = short = true
|
|
|
|
end
|
|
|
|
pat = Completion.regexp(word, true)
|
|
|
|
visit(:each_option) do |opt|
|
2010-12-06 08:47:19 -05:00
|
|
|
next unless Switch === opt
|
2010-11-27 00:01:13 -05:00
|
|
|
opts = (long ? opt.long : []) + (short ? opt.short : [])
|
2010-11-19 06:26:54 -05:00
|
|
|
opts = Completion.candidate(word, true, pat, &opts.method(:each)).map(&:first) if pat
|
|
|
|
if /\A=/ =~ opt.arg
|
2010-11-26 21:29:26 -05:00
|
|
|
opts.map! {|sw| sw + "="}
|
2010-11-19 06:26:54 -05:00
|
|
|
if arg and CompletingHash === opt.pattern
|
|
|
|
if opts = opt.pattern.candidate(arg, false, argpat)
|
|
|
|
opts.map!(&:last)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
list.concat(opts)
|
|
|
|
end
|
|
|
|
list
|
|
|
|
end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Loads options from file names as +filename+. Does nothing when the file
|
|
|
|
# is not present. Returns whether successfully loaded.
|
|
|
|
#
|
|
|
|
# +filename+ defaults to basename of the program without suffix in a
|
|
|
|
# directory ~/.options.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def load(filename = nil)
|
|
|
|
begin
|
|
|
|
filename ||= File.expand_path(File.basename($0, '.*'), '~/.options')
|
|
|
|
rescue
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
parse(*IO.readlines(filename).each {|s| s.chomp!})
|
|
|
|
true
|
|
|
|
rescue Errno::ENOENT, Errno::ENOTDIR
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Parses environment variable +env+ or its uppercase with splitting like a
|
|
|
|
# shell.
|
|
|
|
#
|
|
|
|
# +env+ defaults to the basename of the program.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def environment(env = File.basename($0, '.*'))
|
|
|
|
env = ENV[env] || ENV[env.upcase] or return
|
2008-07-13 17:02:28 -04:00
|
|
|
require 'shellwords'
|
2002-07-12 12:34:02 -04:00
|
|
|
parse(*Shellwords.shellwords(env))
|
|
|
|
end
|
2006-06-28 17:56:01 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Acceptable argument classes
|
|
|
|
#
|
|
|
|
|
|
|
|
#
|
|
|
|
# Any string and no conversion. This is fall-back.
|
|
|
|
#
|
2003-05-21 00:27:06 -04:00
|
|
|
accept(Object) {|s,|s or s.nil?}
|
2002-07-12 12:34:02 -04:00
|
|
|
|
2003-05-21 00:27:06 -04:00
|
|
|
accept(NilClass) {|s,|s}
|
2002-07-12 12:34:02 -04:00
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Any non-empty string, and no conversion.
|
|
|
|
#
|
2009-01-31 04:03:43 -05:00
|
|
|
accept(String, /.+/m) {|s,*|s}
|
2002-07-12 12:34:02 -04:00
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Ruby/C-like integer, octal for 0-7 sequence, binary for 0b, hexadecimal
|
|
|
|
# for 0x, and decimal for others; with optional sign prefix. Converts to
|
|
|
|
# Integer.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
decimal = '\d+(?:_\d+)*'
|
|
|
|
binary = 'b[01]+(?:_[01]+)*'
|
|
|
|
hex = 'x[\da-f]+(?:_[\da-f]+)*'
|
2013-09-05 01:02:17 -04:00
|
|
|
octal = "0(?:[0-7]+(?:_[0-7]+)*|#{binary}|#{hex})?"
|
2002-07-12 12:34:02 -04:00
|
|
|
integer = "#{octal}|#{decimal}"
|
2013-09-05 01:02:17 -04:00
|
|
|
|
|
|
|
accept(Integer, %r"\A[-+]?(?:#{integer})\z"io) {|s,|
|
|
|
|
begin
|
|
|
|
Integer(s)
|
|
|
|
rescue ArgumentError
|
|
|
|
raise OptionParser::InvalidArgument, s
|
|
|
|
end if s
|
|
|
|
}
|
2002-07-12 12:34:02 -04:00
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Float number format, and converts to Float.
|
|
|
|
#
|
2003-03-29 01:39:50 -05:00
|
|
|
float = "(?:#{decimal}(?:\\.(?:#{decimal})?)?|\\.#{decimal})(?:E[-+]?#{decimal})?"
|
2013-09-05 01:02:17 -04:00
|
|
|
floatpat = %r"\A[-+]?#{float}\z"io
|
2003-05-21 00:27:06 -04:00
|
|
|
accept(Float, floatpat) {|s,| s.to_f if s}
|
2002-07-12 12:34:02 -04:00
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Generic numeric format, converts to Integer for integer format, Float
|
2010-03-29 05:10:12 -04:00
|
|
|
# for float format, and Rational for rational format.
|
|
|
|
#
|
|
|
|
real = "[-+]?(?:#{octal}|#{float})"
|
2013-09-05 01:02:17 -04:00
|
|
|
accept(Numeric, /\A(#{real})(?:\/(#{real}))?\z/io) {|s, d, n|
|
2010-03-29 05:10:12 -04:00
|
|
|
if n
|
|
|
|
Rational(d, n)
|
|
|
|
elsif s
|
|
|
|
eval(s)
|
|
|
|
end
|
|
|
|
}
|
2002-07-12 12:34:02 -04:00
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Decimal integer format, to be converted to Integer.
|
|
|
|
#
|
2013-09-05 01:02:17 -04:00
|
|
|
DecimalInteger = /\A[-+]?#{decimal}\z/io
|
|
|
|
accept(DecimalInteger, DecimalInteger) {|s,|
|
|
|
|
begin
|
|
|
|
Integer(s)
|
|
|
|
rescue ArgumentError
|
|
|
|
raise OptionParser::InvalidArgument, s
|
|
|
|
end if s
|
|
|
|
}
|
2002-07-12 12:34:02 -04:00
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Ruby/C like octal/hexadecimal/binary integer format, to be converted to
|
|
|
|
# Integer.
|
|
|
|
#
|
2013-09-05 01:02:17 -04:00
|
|
|
OctalInteger = /\A[-+]?(?:[0-7]+(?:_[0-7]+)*|0(?:#{binary}|#{hex}))\z/io
|
|
|
|
accept(OctalInteger, OctalInteger) {|s,|
|
|
|
|
begin
|
|
|
|
Integer(s, 8)
|
|
|
|
rescue ArgumentError
|
|
|
|
raise OptionParser::InvalidArgument, s
|
|
|
|
end if s
|
|
|
|
}
|
2002-07-12 12:34:02 -04:00
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Decimal integer/float number format, to be converted to Integer for
|
|
|
|
# integer format, Float for float format.
|
|
|
|
#
|
2003-12-23 08:27:42 -05:00
|
|
|
DecimalNumeric = floatpat # decimal integer is allowed as float also.
|
2013-09-05 01:02:17 -04:00
|
|
|
accept(DecimalNumeric, floatpat) {|s,|
|
|
|
|
begin
|
|
|
|
eval(s)
|
|
|
|
rescue SyntaxError
|
|
|
|
raise OptionParser::InvalidArgument, s
|
|
|
|
end if s
|
|
|
|
}
|
2002-07-12 12:34:02 -04:00
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Boolean switch, which means whether it is present or not, whether it is
|
|
|
|
# absent or not with prefix no-, or it takes an argument
|
|
|
|
# yes/no/true/false/+/-.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
yesno = CompletingHash.new
|
|
|
|
%w[- no false].each {|el| yesno[el] = false}
|
|
|
|
%w[+ yes true].each {|el| yesno[el] = true}
|
2010-03-19 23:30:59 -04:00
|
|
|
yesno['nil'] = false # should be nil?
|
2002-07-12 12:34:02 -04:00
|
|
|
accept(TrueClass, yesno) {|arg, val| val == nil or val}
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Similar to TrueClass, but defaults to false.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
accept(FalseClass, yesno) {|arg, val| val != nil and val}
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# List of strings separated by ",".
|
|
|
|
#
|
2003-05-21 00:27:06 -04:00
|
|
|
accept(Array) do |s,|
|
2002-07-12 12:34:02 -04:00
|
|
|
if s
|
2006-10-09 10:41:24 -04:00
|
|
|
s = s.split(',').collect {|ss| ss unless ss.empty?}
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
s
|
|
|
|
end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Regular expression with options.
|
|
|
|
#
|
2004-11-09 00:27:58 -05:00
|
|
|
accept(Regexp, %r"\A/((?:\\.|[^\\])*)/([[:alpha:]]+)?\z|.*") do |all, s, o|
|
|
|
|
f = 0
|
|
|
|
if o
|
|
|
|
f |= Regexp::IGNORECASE if /i/ =~ o
|
|
|
|
f |= Regexp::MULTILINE if /m/ =~ o
|
|
|
|
f |= Regexp::EXTENDED if /x/ =~ o
|
2012-01-16 02:42:01 -05:00
|
|
|
k = o.delete("imx")
|
|
|
|
k = nil if k.empty?
|
2004-11-09 00:27:58 -05:00
|
|
|
end
|
|
|
|
Regexp.new(s || all, f, k)
|
|
|
|
end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Exceptions
|
|
|
|
#
|
|
|
|
|
|
|
|
#
|
|
|
|
# Base class of exceptions from OptionParser.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
class ParseError < RuntimeError
|
2006-06-28 17:56:01 -04:00
|
|
|
# Reason which caused the error.
|
2002-07-12 12:34:02 -04:00
|
|
|
Reason = 'parse error'.freeze
|
|
|
|
|
|
|
|
def initialize(*args)
|
|
|
|
@args = args
|
|
|
|
@reason = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :args
|
|
|
|
attr_writer :reason
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Pushes back erred argument(s) to +argv+.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def recover(argv)
|
|
|
|
argv[0, 0] = @args
|
|
|
|
argv
|
|
|
|
end
|
|
|
|
|
2008-10-09 11:21:42 -04:00
|
|
|
def self.filter_backtrace(array)
|
2008-09-18 07:38:24 -04:00
|
|
|
unless $DEBUG
|
|
|
|
array.delete_if(&%r"\A#{Regexp.quote(__FILE__)}:"o.method(:=~))
|
|
|
|
end
|
2008-10-09 11:21:42 -04:00
|
|
|
array
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_backtrace(array)
|
|
|
|
super(self.class.filter_backtrace(array))
|
2008-09-18 07:38:24 -04:00
|
|
|
end
|
|
|
|
|
2002-07-12 12:34:02 -04:00
|
|
|
def set_option(opt, eq)
|
|
|
|
if eq
|
2003-12-23 08:27:42 -05:00
|
|
|
@args[0] = opt
|
2002-07-12 12:34:02 -04:00
|
|
|
else
|
2003-12-23 08:27:42 -05:00
|
|
|
@args.unshift(opt)
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Returns error reason. Override this for I18N.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def reason
|
2002-10-02 12:45:35 -04:00
|
|
|
@reason || self.class::Reason
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def inspect
|
2002-10-02 12:45:35 -04:00
|
|
|
"#<#{self.class.to_s}: #{args.join(' ')}>"
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Default stringizing method to emit standard error message.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def message
|
|
|
|
reason + ': ' + args.join(' ')
|
|
|
|
end
|
|
|
|
|
|
|
|
alias to_s message
|
|
|
|
end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Raises when ambiguously completable string is encountered.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
class AmbiguousOption < ParseError
|
|
|
|
const_set(:Reason, 'ambiguous option'.freeze)
|
|
|
|
end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Raises when there is an argument for a switch which takes no argument.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
class NeedlessArgument < ParseError
|
2004-11-09 00:27:58 -05:00
|
|
|
const_set(:Reason, 'needless argument'.freeze)
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Raises when a switch with mandatory argument has no argument.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
class MissingArgument < ParseError
|
|
|
|
const_set(:Reason, 'missing argument'.freeze)
|
|
|
|
end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Raises when switch is undefined.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
class InvalidOption < ParseError
|
|
|
|
const_set(:Reason, 'invalid option'.freeze)
|
|
|
|
end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Raises when the given argument does not match required format.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
class InvalidArgument < ParseError
|
|
|
|
const_set(:Reason, 'invalid argument'.freeze)
|
|
|
|
end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Raises when the given argument word can't be completed uniquely.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
class AmbiguousArgument < InvalidArgument
|
|
|
|
const_set(:Reason, 'ambiguous argument'.freeze)
|
|
|
|
end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Miscellaneous
|
|
|
|
#
|
|
|
|
|
|
|
|
#
|
|
|
|
# Extends command line arguments array (ARGV) to parse itself.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
module Arguable
|
2006-06-28 17:56:01 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Sets OptionParser object, when +opt+ is +false+ or +nil+, methods
|
|
|
|
# OptionParser::Arguable#options and OptionParser::Arguable#options= are
|
|
|
|
# undefined. Thus, there is no ways to access the OptionParser object
|
|
|
|
# via the receiver object.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def options=(opt)
|
|
|
|
unless @optparse = opt
|
2003-12-23 08:27:42 -05:00
|
|
|
class << self
|
|
|
|
undef_method(:options)
|
|
|
|
undef_method(:options=)
|
|
|
|
end
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Actual OptionParser object, automatically created if nonexistent.
|
|
|
|
#
|
|
|
|
# If called with a block, yields the OptionParser object and returns the
|
|
|
|
# result of the block. If an OptionParser::ParseError exception occurs
|
|
|
|
# in the block, it is rescued, a error message printed to STDERR and
|
|
|
|
# +nil+ returned.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def options
|
|
|
|
@optparse ||= OptionParser.new
|
2005-09-20 18:45:06 -04:00
|
|
|
@optparse.default_argv = self
|
2002-07-12 12:34:02 -04:00
|
|
|
block_given? or return @optparse
|
|
|
|
begin
|
2003-12-23 08:27:42 -05:00
|
|
|
yield @optparse
|
2002-07-12 12:34:02 -04:00
|
|
|
rescue ParseError
|
2003-12-23 08:27:42 -05:00
|
|
|
@optparse.warn $!
|
|
|
|
nil
|
2002-07-12 12:34:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Parses +self+ destructively in order and returns +self+ containing the
|
|
|
|
# rest arguments left unparsed.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def order!(&blk) options.order!(self, &blk) end
|
2006-06-28 17:56:01 -04:00
|
|
|
|
|
|
|
#
|
|
|
|
# Parses +self+ destructively in permutation mode and returns +self+
|
|
|
|
# containing the rest arguments left unparsed.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def permute!() options.permute!(self) end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Parses +self+ destructively and returns +self+ containing the
|
|
|
|
# rest arguments left unparsed.
|
|
|
|
#
|
|
|
|
def parse!() options.parse!(self) end
|
2006-06-10 19:31:32 -04:00
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Substitution of getopts is possible as follows. Also see
|
|
|
|
# OptionParser#getopts.
|
|
|
|
#
|
|
|
|
# def getopts(*args)
|
|
|
|
# ($OPT = ARGV.getopts(*args)).each do |opt, val|
|
|
|
|
# eval "$OPT_#{opt.gsub(/[^A-Za-z0-9_]/, '_')} = val"
|
|
|
|
# end
|
|
|
|
# rescue OptionParser::ParseError
|
|
|
|
# end
|
|
|
|
#
|
2006-06-10 19:31:32 -04:00
|
|
|
def getopts(*args)
|
2006-06-11 10:21:08 -04:00
|
|
|
options.getopts(self, *args)
|
2006-06-10 19:31:32 -04:00
|
|
|
end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Initializes instance variable.
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
def self.extend_object(obj)
|
|
|
|
super
|
|
|
|
obj.instance_eval {@optparse = nil}
|
|
|
|
end
|
|
|
|
def initialize(*args)
|
|
|
|
super
|
|
|
|
@optparse = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-06-28 17:56:01 -04:00
|
|
|
#
|
|
|
|
# Acceptable argument classes. Now contains DecimalInteger, OctalInteger
|
|
|
|
# and DecimalNumeric. See Acceptable argument classes (in source code).
|
|
|
|
#
|
2002-07-12 12:34:02 -04:00
|
|
|
module Acceptables
|
|
|
|
const_set(:DecimalInteger, OptionParser::DecimalInteger)
|
|
|
|
const_set(:OctalInteger, OptionParser::OctalInteger)
|
|
|
|
const_set(:DecimalNumeric, OptionParser::DecimalNumeric)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# ARGV is arguable by OptionParser
|
|
|
|
ARGV.extend(OptionParser::Arguable)
|