Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/smanonr..../opt/alt/ruby31/share/ruby
File: optparse.rb
# frozen_string_literal: true
[0] Fix | Delete
#
[1] Fix | Delete
# optparse.rb - command-line option analysis with the OptionParser class.
[2] Fix | Delete
#
[3] Fix | Delete
# Author:: Nobu Nakada
[4] Fix | Delete
# Documentation:: Nobu Nakada and Gavin Sinclair.
[5] Fix | Delete
#
[6] Fix | Delete
# See OptionParser for documentation.
[7] Fix | Delete
#
[8] Fix | Delete
[9] Fix | Delete
[10] Fix | Delete
#--
[11] Fix | Delete
# == Developer Documentation (not for RDoc output)
[12] Fix | Delete
#
[13] Fix | Delete
# === Class tree
[14] Fix | Delete
#
[15] Fix | Delete
# - OptionParser:: front end
[16] Fix | Delete
# - OptionParser::Switch:: each switches
[17] Fix | Delete
# - OptionParser::List:: options list
[18] Fix | Delete
# - OptionParser::ParseError:: errors on parsing
[19] Fix | Delete
# - OptionParser::AmbiguousOption
[20] Fix | Delete
# - OptionParser::NeedlessArgument
[21] Fix | Delete
# - OptionParser::MissingArgument
[22] Fix | Delete
# - OptionParser::InvalidOption
[23] Fix | Delete
# - OptionParser::InvalidArgument
[24] Fix | Delete
# - OptionParser::AmbiguousArgument
[25] Fix | Delete
#
[26] Fix | Delete
# === Object relationship diagram
[27] Fix | Delete
#
[28] Fix | Delete
# +--------------+
[29] Fix | Delete
# | OptionParser |<>-----+
[30] Fix | Delete
# +--------------+ | +--------+
[31] Fix | Delete
# | ,-| Switch |
[32] Fix | Delete
# on_head -------->+---------------+ / +--------+
[33] Fix | Delete
# accept/reject -->| List |<|>-
[34] Fix | Delete
# | |<|>- +----------+
[35] Fix | Delete
# on ------------->+---------------+ `-| argument |
[36] Fix | Delete
# : : | class |
[37] Fix | Delete
# +---------------+ |==========|
[38] Fix | Delete
# on_tail -------->| | |pattern |
[39] Fix | Delete
# +---------------+ |----------|
[40] Fix | Delete
# OptionParser.accept ->| DefaultList | |converter |
[41] Fix | Delete
# reject |(shared between| +----------+
[42] Fix | Delete
# | all instances)|
[43] Fix | Delete
# +---------------+
[44] Fix | Delete
#
[45] Fix | Delete
#++
[46] Fix | Delete
#
[47] Fix | Delete
# == OptionParser
[48] Fix | Delete
#
[49] Fix | Delete
# === New to \OptionParser?
[50] Fix | Delete
#
[51] Fix | Delete
# See the {Tutorial}[optparse/tutorial.rdoc].
[52] Fix | Delete
#
[53] Fix | Delete
# === Introduction
[54] Fix | Delete
#
[55] Fix | Delete
# OptionParser is a class for command-line option analysis. It is much more
[56] Fix | Delete
# advanced, yet also easier to use, than GetoptLong, and is a more Ruby-oriented
[57] Fix | Delete
# solution.
[58] Fix | Delete
#
[59] Fix | Delete
# === Features
[60] Fix | Delete
#
[61] Fix | Delete
# 1. The argument specification and the code to handle it are written in the
[62] Fix | Delete
# same place.
[63] Fix | Delete
# 2. It can output an option summary; you don't need to maintain this string
[64] Fix | Delete
# separately.
[65] Fix | Delete
# 3. Optional and mandatory arguments are specified very gracefully.
[66] Fix | Delete
# 4. Arguments can be automatically converted to a specified class.
[67] Fix | Delete
# 5. Arguments can be restricted to a certain set.
[68] Fix | Delete
#
[69] Fix | Delete
# All of these features are demonstrated in the examples below. See
[70] Fix | Delete
# #make_switch for full documentation.
[71] Fix | Delete
#
[72] Fix | Delete
# === Minimal example
[73] Fix | Delete
#
[74] Fix | Delete
# require 'optparse'
[75] Fix | Delete
#
[76] Fix | Delete
# options = {}
[77] Fix | Delete
# OptionParser.new do |parser|
[78] Fix | Delete
# parser.banner = "Usage: example.rb [options]"
[79] Fix | Delete
#
[80] Fix | Delete
# parser.on("-v", "--[no-]verbose", "Run verbosely") do |v|
[81] Fix | Delete
# options[:verbose] = v
[82] Fix | Delete
# end
[83] Fix | Delete
# end.parse!
[84] Fix | Delete
#
[85] Fix | Delete
# p options
[86] Fix | Delete
# p ARGV
[87] Fix | Delete
#
[88] Fix | Delete
# === Generating Help
[89] Fix | Delete
#
[90] Fix | Delete
# OptionParser can be used to automatically generate help for the commands you
[91] Fix | Delete
# write:
[92] Fix | Delete
#
[93] Fix | Delete
# require 'optparse'
[94] Fix | Delete
#
[95] Fix | Delete
# Options = Struct.new(:name)
[96] Fix | Delete
#
[97] Fix | Delete
# class Parser
[98] Fix | Delete
# def self.parse(options)
[99] Fix | Delete
# args = Options.new("world")
[100] Fix | Delete
#
[101] Fix | Delete
# opt_parser = OptionParser.new do |parser|
[102] Fix | Delete
# parser.banner = "Usage: example.rb [options]"
[103] Fix | Delete
#
[104] Fix | Delete
# parser.on("-nNAME", "--name=NAME", "Name to say hello to") do |n|
[105] Fix | Delete
# args.name = n
[106] Fix | Delete
# end
[107] Fix | Delete
#
[108] Fix | Delete
# parser.on("-h", "--help", "Prints this help") do
[109] Fix | Delete
# puts parser
[110] Fix | Delete
# exit
[111] Fix | Delete
# end
[112] Fix | Delete
# end
[113] Fix | Delete
#
[114] Fix | Delete
# opt_parser.parse!(options)
[115] Fix | Delete
# return args
[116] Fix | Delete
# end
[117] Fix | Delete
# end
[118] Fix | Delete
# options = Parser.parse %w[--help]
[119] Fix | Delete
#
[120] Fix | Delete
# #=>
[121] Fix | Delete
# # Usage: example.rb [options]
[122] Fix | Delete
# # -n, --name=NAME Name to say hello to
[123] Fix | Delete
# # -h, --help Prints this help
[124] Fix | Delete
#
[125] Fix | Delete
# === Required Arguments
[126] Fix | Delete
#
[127] Fix | Delete
# For options that require an argument, option specification strings may include an
[128] Fix | Delete
# option name in all caps. If an option is used without the required argument,
[129] Fix | Delete
# an exception will be raised.
[130] Fix | Delete
#
[131] Fix | Delete
# require 'optparse'
[132] Fix | Delete
#
[133] Fix | Delete
# options = {}
[134] Fix | Delete
# OptionParser.new do |parser|
[135] Fix | Delete
# parser.on("-r", "--require LIBRARY",
[136] Fix | Delete
# "Require the LIBRARY before executing your script") do |lib|
[137] Fix | Delete
# puts "You required #{lib}!"
[138] Fix | Delete
# end
[139] Fix | Delete
# end.parse!
[140] Fix | Delete
#
[141] Fix | Delete
# Used:
[142] Fix | Delete
#
[143] Fix | Delete
# $ ruby optparse-test.rb -r
[144] Fix | Delete
# optparse-test.rb:9:in `<main>': missing argument: -r (OptionParser::MissingArgument)
[145] Fix | Delete
# $ ruby optparse-test.rb -r my-library
[146] Fix | Delete
# You required my-library!
[147] Fix | Delete
#
[148] Fix | Delete
# === Type Coercion
[149] Fix | Delete
#
[150] Fix | Delete
# OptionParser supports the ability to coerce command line arguments
[151] Fix | Delete
# into objects for us.
[152] Fix | Delete
#
[153] Fix | Delete
# OptionParser comes with a few ready-to-use kinds of type
[154] Fix | Delete
# coercion. They are:
[155] Fix | Delete
#
[156] Fix | Delete
# - Date -- Anything accepted by +Date.parse+
[157] Fix | Delete
# - DateTime -- Anything accepted by +DateTime.parse+
[158] Fix | Delete
# - Time -- Anything accepted by +Time.httpdate+ or +Time.parse+
[159] Fix | Delete
# - URI -- Anything accepted by +URI.parse+
[160] Fix | Delete
# - Shellwords -- Anything accepted by +Shellwords.shellwords+
[161] Fix | Delete
# - String -- Any non-empty string
[162] Fix | Delete
# - Integer -- Any integer. Will convert octal. (e.g. 124, -3, 040)
[163] Fix | Delete
# - Float -- Any float. (e.g. 10, 3.14, -100E+13)
[164] Fix | Delete
# - Numeric -- Any integer, float, or rational (1, 3.4, 1/3)
[165] Fix | Delete
# - DecimalInteger -- Like +Integer+, but no octal format.
[166] Fix | Delete
# - OctalInteger -- Like +Integer+, but no decimal format.
[167] Fix | Delete
# - DecimalNumeric -- Decimal integer or float.
[168] Fix | Delete
# - TrueClass -- Accepts '+, yes, true, -, no, false' and
[169] Fix | Delete
# defaults as +true+
[170] Fix | Delete
# - FalseClass -- Same as +TrueClass+, but defaults to +false+
[171] Fix | Delete
# - Array -- Strings separated by ',' (e.g. 1,2,3)
[172] Fix | Delete
# - Regexp -- Regular expressions. Also includes options.
[173] Fix | Delete
#
[174] Fix | Delete
# We can also add our own coercions, which we will cover below.
[175] Fix | Delete
#
[176] Fix | Delete
# ==== Using Built-in Conversions
[177] Fix | Delete
#
[178] Fix | Delete
# As an example, the built-in +Time+ conversion is used. The other built-in
[179] Fix | Delete
# conversions behave in the same way.
[180] Fix | Delete
# OptionParser will attempt to parse the argument
[181] Fix | Delete
# as a +Time+. If it succeeds, that time will be passed to the
[182] Fix | Delete
# handler block. Otherwise, an exception will be raised.
[183] Fix | Delete
#
[184] Fix | Delete
# require 'optparse'
[185] Fix | Delete
# require 'optparse/time'
[186] Fix | Delete
# OptionParser.new do |parser|
[187] Fix | Delete
# parser.on("-t", "--time [TIME]", Time, "Begin execution at given time") do |time|
[188] Fix | Delete
# p time
[189] Fix | Delete
# end
[190] Fix | Delete
# end.parse!
[191] Fix | Delete
#
[192] Fix | Delete
# Used:
[193] Fix | Delete
#
[194] Fix | Delete
# $ ruby optparse-test.rb -t nonsense
[195] Fix | Delete
# ... invalid argument: -t nonsense (OptionParser::InvalidArgument)
[196] Fix | Delete
# $ ruby optparse-test.rb -t 10-11-12
[197] Fix | Delete
# 2010-11-12 00:00:00 -0500
[198] Fix | Delete
# $ ruby optparse-test.rb -t 9:30
[199] Fix | Delete
# 2014-08-13 09:30:00 -0400
[200] Fix | Delete
#
[201] Fix | Delete
# ==== Creating Custom Conversions
[202] Fix | Delete
#
[203] Fix | Delete
# The +accept+ method on OptionParser may be used to create converters.
[204] Fix | Delete
# It specifies which conversion block to call whenever a class is specified.
[205] Fix | Delete
# The example below uses it to fetch a +User+ object before the +on+ handler receives it.
[206] Fix | Delete
#
[207] Fix | Delete
# require 'optparse'
[208] Fix | Delete
#
[209] Fix | Delete
# User = Struct.new(:id, :name)
[210] Fix | Delete
#
[211] Fix | Delete
# def find_user id
[212] Fix | Delete
# not_found = ->{ raise "No User Found for id #{id}" }
[213] Fix | Delete
# [ User.new(1, "Sam"),
[214] Fix | Delete
# User.new(2, "Gandalf") ].find(not_found) do |u|
[215] Fix | Delete
# u.id == id
[216] Fix | Delete
# end
[217] Fix | Delete
# end
[218] Fix | Delete
#
[219] Fix | Delete
# op = OptionParser.new
[220] Fix | Delete
# op.accept(User) do |user_id|
[221] Fix | Delete
# find_user user_id.to_i
[222] Fix | Delete
# end
[223] Fix | Delete
#
[224] Fix | Delete
# op.on("--user ID", User) do |user|
[225] Fix | Delete
# puts user
[226] Fix | Delete
# end
[227] Fix | Delete
#
[228] Fix | Delete
# op.parse!
[229] Fix | Delete
#
[230] Fix | Delete
# Used:
[231] Fix | Delete
#
[232] Fix | Delete
# $ ruby optparse-test.rb --user 1
[233] Fix | Delete
# #<struct User id=1, name="Sam">
[234] Fix | Delete
# $ ruby optparse-test.rb --user 2
[235] Fix | Delete
# #<struct User id=2, name="Gandalf">
[236] Fix | Delete
# $ ruby optparse-test.rb --user 3
[237] Fix | Delete
# optparse-test.rb:15:in `block in find_user': No User Found for id 3 (RuntimeError)
[238] Fix | Delete
#
[239] Fix | Delete
# === Store options to a Hash
[240] Fix | Delete
#
[241] Fix | Delete
# The +into+ option of +order+, +parse+ and so on methods stores command line options into a Hash.
[242] Fix | Delete
#
[243] Fix | Delete
# require 'optparse'
[244] Fix | Delete
#
[245] Fix | Delete
# options = {}
[246] Fix | Delete
# OptionParser.new do |parser|
[247] Fix | Delete
# parser.on('-a')
[248] Fix | Delete
# parser.on('-b NUM', Integer)
[249] Fix | Delete
# parser.on('-v', '--verbose')
[250] Fix | Delete
# end.parse!(into: options)
[251] Fix | Delete
#
[252] Fix | Delete
# p options
[253] Fix | Delete
#
[254] Fix | Delete
# Used:
[255] Fix | Delete
#
[256] Fix | Delete
# $ ruby optparse-test.rb -a
[257] Fix | Delete
# {:a=>true}
[258] Fix | Delete
# $ ruby optparse-test.rb -a -v
[259] Fix | Delete
# {:a=>true, :verbose=>true}
[260] Fix | Delete
# $ ruby optparse-test.rb -a -b 100
[261] Fix | Delete
# {:a=>true, :b=>100}
[262] Fix | Delete
#
[263] Fix | Delete
# === Complete example
[264] Fix | Delete
#
[265] Fix | Delete
# The following example is a complete Ruby program. You can run it and see the
[266] Fix | Delete
# effect of specifying various options. This is probably the best way to learn
[267] Fix | Delete
# the features of +optparse+.
[268] Fix | Delete
#
[269] Fix | Delete
# require 'optparse'
[270] Fix | Delete
# require 'optparse/time'
[271] Fix | Delete
# require 'ostruct'
[272] Fix | Delete
# require 'pp'
[273] Fix | Delete
#
[274] Fix | Delete
# class OptparseExample
[275] Fix | Delete
# Version = '1.0.0'
[276] Fix | Delete
#
[277] Fix | Delete
# CODES = %w[iso-2022-jp shift_jis euc-jp utf8 binary]
[278] Fix | Delete
# CODE_ALIASES = { "jis" => "iso-2022-jp", "sjis" => "shift_jis" }
[279] Fix | Delete
#
[280] Fix | Delete
# class ScriptOptions
[281] Fix | Delete
# attr_accessor :library, :inplace, :encoding, :transfer_type,
[282] Fix | Delete
# :verbose, :extension, :delay, :time, :record_separator,
[283] Fix | Delete
# :list
[284] Fix | Delete
#
[285] Fix | Delete
# def initialize
[286] Fix | Delete
# self.library = []
[287] Fix | Delete
# self.inplace = false
[288] Fix | Delete
# self.encoding = "utf8"
[289] Fix | Delete
# self.transfer_type = :auto
[290] Fix | Delete
# self.verbose = false
[291] Fix | Delete
# end
[292] Fix | Delete
#
[293] Fix | Delete
# def define_options(parser)
[294] Fix | Delete
# parser.banner = "Usage: example.rb [options]"
[295] Fix | Delete
# parser.separator ""
[296] Fix | Delete
# parser.separator "Specific options:"
[297] Fix | Delete
#
[298] Fix | Delete
# # add additional options
[299] Fix | Delete
# perform_inplace_option(parser)
[300] Fix | Delete
# delay_execution_option(parser)
[301] Fix | Delete
# execute_at_time_option(parser)
[302] Fix | Delete
# specify_record_separator_option(parser)
[303] Fix | Delete
# list_example_option(parser)
[304] Fix | Delete
# specify_encoding_option(parser)
[305] Fix | Delete
# optional_option_argument_with_keyword_completion_option(parser)
[306] Fix | Delete
# boolean_verbose_option(parser)
[307] Fix | Delete
#
[308] Fix | Delete
# parser.separator ""
[309] Fix | Delete
# parser.separator "Common options:"
[310] Fix | Delete
# # No argument, shows at tail. This will print an options summary.
[311] Fix | Delete
# # Try it and see!
[312] Fix | Delete
# parser.on_tail("-h", "--help", "Show this message") do
[313] Fix | Delete
# puts parser
[314] Fix | Delete
# exit
[315] Fix | Delete
# end
[316] Fix | Delete
# # Another typical switch to print the version.
[317] Fix | Delete
# parser.on_tail("--version", "Show version") do
[318] Fix | Delete
# puts Version
[319] Fix | Delete
# exit
[320] Fix | Delete
# end
[321] Fix | Delete
# end
[322] Fix | Delete
#
[323] Fix | Delete
# def perform_inplace_option(parser)
[324] Fix | Delete
# # Specifies an optional option argument
[325] Fix | Delete
# parser.on("-i", "--inplace [EXTENSION]",
[326] Fix | Delete
# "Edit ARGV files in place",
[327] Fix | Delete
# "(make backup if EXTENSION supplied)") do |ext|
[328] Fix | Delete
# self.inplace = true
[329] Fix | Delete
# self.extension = ext || ''
[330] Fix | Delete
# self.extension.sub!(/\A\.?(?=.)/, ".") # Ensure extension begins with dot.
[331] Fix | Delete
# end
[332] Fix | Delete
# end
[333] Fix | Delete
#
[334] Fix | Delete
# def delay_execution_option(parser)
[335] Fix | Delete
# # Cast 'delay' argument to a Float.
[336] Fix | Delete
# parser.on("--delay N", Float, "Delay N seconds before executing") do |n|
[337] Fix | Delete
# self.delay = n
[338] Fix | Delete
# end
[339] Fix | Delete
# end
[340] Fix | Delete
#
[341] Fix | Delete
# def execute_at_time_option(parser)
[342] Fix | Delete
# # Cast 'time' argument to a Time object.
[343] Fix | Delete
# parser.on("-t", "--time [TIME]", Time, "Begin execution at given time") do |time|
[344] Fix | Delete
# self.time = time
[345] Fix | Delete
# end
[346] Fix | Delete
# end
[347] Fix | Delete
#
[348] Fix | Delete
# def specify_record_separator_option(parser)
[349] Fix | Delete
# # Cast to octal integer.
[350] Fix | Delete
# parser.on("-F", "--irs [OCTAL]", OptionParser::OctalInteger,
[351] Fix | Delete
# "Specify record separator (default \\0)") do |rs|
[352] Fix | Delete
# self.record_separator = rs
[353] Fix | Delete
# end
[354] Fix | Delete
# end
[355] Fix | Delete
#
[356] Fix | Delete
# def list_example_option(parser)
[357] Fix | Delete
# # List of arguments.
[358] Fix | Delete
# parser.on("--list x,y,z", Array, "Example 'list' of arguments") do |list|
[359] Fix | Delete
# self.list = list
[360] Fix | Delete
# end
[361] Fix | Delete
# end
[362] Fix | Delete
#
[363] Fix | Delete
# def specify_encoding_option(parser)
[364] Fix | Delete
# # Keyword completion. We are specifying a specific set of arguments (CODES
[365] Fix | Delete
# # and CODE_ALIASES - notice the latter is a Hash), and the user may provide
[366] Fix | Delete
# # the shortest unambiguous text.
[367] Fix | Delete
# code_list = (CODE_ALIASES.keys + CODES).join(', ')
[368] Fix | Delete
# parser.on("--code CODE", CODES, CODE_ALIASES, "Select encoding",
[369] Fix | Delete
# "(#{code_list})") do |encoding|
[370] Fix | Delete
# self.encoding = encoding
[371] Fix | Delete
# end
[372] Fix | Delete
# end
[373] Fix | Delete
#
[374] Fix | Delete
# def optional_option_argument_with_keyword_completion_option(parser)
[375] Fix | Delete
# # Optional '--type' option argument with keyword completion.
[376] Fix | Delete
# parser.on("--type [TYPE]", [:text, :binary, :auto],
[377] Fix | Delete
# "Select transfer type (text, binary, auto)") do |t|
[378] Fix | Delete
# self.transfer_type = t
[379] Fix | Delete
# end
[380] Fix | Delete
# end
[381] Fix | Delete
#
[382] Fix | Delete
# def boolean_verbose_option(parser)
[383] Fix | Delete
# # Boolean switch.
[384] Fix | Delete
# parser.on("-v", "--[no-]verbose", "Run verbosely") do |v|
[385] Fix | Delete
# self.verbose = v
[386] Fix | Delete
# end
[387] Fix | Delete
# end
[388] Fix | Delete
# end
[389] Fix | Delete
#
[390] Fix | Delete
# #
[391] Fix | Delete
# # Return a structure describing the options.
[392] Fix | Delete
# #
[393] Fix | Delete
# def parse(args)
[394] Fix | Delete
# # The options specified on the command line will be collected in
[395] Fix | Delete
# # *options*.
[396] Fix | Delete
#
[397] Fix | Delete
# @options = ScriptOptions.new
[398] Fix | Delete
# @args = OptionParser.new do |parser|
[399] Fix | Delete
# @options.define_options(parser)
[400] Fix | Delete
# parser.parse!(args)
[401] Fix | Delete
# end
[402] Fix | Delete
# @options
[403] Fix | Delete
# end
[404] Fix | Delete
#
[405] Fix | Delete
# attr_reader :parser, :options
[406] Fix | Delete
# end # class OptparseExample
[407] Fix | Delete
#
[408] Fix | Delete
# example = OptparseExample.new
[409] Fix | Delete
# options = example.parse(ARGV)
[410] Fix | Delete
# pp options # example.options
[411] Fix | Delete
# pp ARGV
[412] Fix | Delete
#
[413] Fix | Delete
# === Shell Completion
[414] Fix | Delete
#
[415] Fix | Delete
# For modern shells (e.g. bash, zsh, etc.), you can use shell
[416] Fix | Delete
# completion for command line options.
[417] Fix | Delete
#
[418] Fix | Delete
# === Further documentation
[419] Fix | Delete
#
[420] Fix | Delete
# The above examples, along with the accompanying
[421] Fix | Delete
# {Tutorial}[optparse/tutorial.rdoc],
[422] Fix | Delete
# should be enough to learn how to use this class.
[423] Fix | Delete
# If you have any questions, file a ticket at http://bugs.ruby-lang.org.
[424] Fix | Delete
#
[425] Fix | Delete
class OptionParser
[426] Fix | Delete
OptionParser::Version = "0.2.0"
[427] Fix | Delete
[428] Fix | Delete
# :stopdoc:
[429] Fix | Delete
NoArgument = [NO_ARGUMENT = :NONE, nil].freeze
[430] Fix | Delete
RequiredArgument = [REQUIRED_ARGUMENT = :REQUIRED, true].freeze
[431] Fix | Delete
OptionalArgument = [OPTIONAL_ARGUMENT = :OPTIONAL, false].freeze
[432] Fix | Delete
# :startdoc:
[433] Fix | Delete
[434] Fix | Delete
#
[435] Fix | Delete
# Keyword completion module. This allows partial arguments to be specified
[436] Fix | Delete
# and resolved against a list of acceptable values.
[437] Fix | Delete
#
[438] Fix | Delete
module Completion
[439] Fix | Delete
def self.regexp(key, icase)
[440] Fix | Delete
Regexp.new('\A' + Regexp.quote(key).gsub(/\w+\b/, '\&\w*'), icase)
[441] Fix | Delete
end
[442] Fix | Delete
[443] Fix | Delete
def self.candidate(key, icase = false, pat = nil, &block)
[444] Fix | Delete
pat ||= Completion.regexp(key, icase)
[445] Fix | Delete
candidates = []
[446] Fix | Delete
block.call do |k, *v|
[447] Fix | Delete
(if Regexp === k
[448] Fix | Delete
kn = ""
[449] Fix | Delete
k === key
[450] Fix | Delete
else
[451] Fix | Delete
kn = defined?(k.id2name) ? k.id2name : k
[452] Fix | Delete
pat === kn
[453] Fix | Delete
end) or next
[454] Fix | Delete
v << k if v.empty?
[455] Fix | Delete
candidates << [k, v, kn]
[456] Fix | Delete
end
[457] Fix | Delete
candidates
[458] Fix | Delete
end
[459] Fix | Delete
[460] Fix | Delete
def candidate(key, icase = false, pat = nil)
[461] Fix | Delete
Completion.candidate(key, icase, pat, &method(:each))
[462] Fix | Delete
end
[463] Fix | Delete
[464] Fix | Delete
public
[465] Fix | Delete
def complete(key, icase = false, pat = nil)
[466] Fix | Delete
candidates = candidate(key, icase, pat, &method(:each)).sort_by {|k, v, kn| kn.size}
[467] Fix | Delete
if candidates.size == 1
[468] Fix | Delete
canon, sw, * = candidates[0]
[469] Fix | Delete
elsif candidates.size > 1
[470] Fix | Delete
canon, sw, cn = candidates.shift
[471] Fix | Delete
candidates.each do |k, v, kn|
[472] Fix | Delete
next if sw == v
[473] Fix | Delete
if String === cn and String === kn
[474] Fix | Delete
if cn.rindex(kn, 0)
[475] Fix | Delete
canon, sw, cn = k, v, kn
[476] Fix | Delete
next
[477] Fix | Delete
elsif kn.rindex(cn, 0)
[478] Fix | Delete
next
[479] Fix | Delete
end
[480] Fix | Delete
end
[481] Fix | Delete
throw :ambiguous, key
[482] Fix | Delete
end
[483] Fix | Delete
end
[484] Fix | Delete
if canon
[485] Fix | Delete
block_given? or return key, *sw
[486] Fix | Delete
yield(key, *sw)
[487] Fix | Delete
end
[488] Fix | Delete
end
[489] Fix | Delete
[490] Fix | Delete
def convert(opt = nil, val = nil, *)
[491] Fix | Delete
val
[492] Fix | Delete
end
[493] Fix | Delete
end
[494] Fix | Delete
[495] Fix | Delete
[496] Fix | Delete
#
[497] Fix | Delete
# Map from option/keyword string to object with completion.
[498] Fix | Delete
#
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function