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