Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/smanonr..../opt/alt/ruby31/share/ruby
File: getoptlong.rb
# frozen_string_literal: true
[0] Fix | Delete
#
[1] Fix | Delete
# GetoptLong for Ruby
[2] Fix | Delete
#
[3] Fix | Delete
# Copyright (C) 1998, 1999, 2000 Motoyuki Kasahara.
[4] Fix | Delete
#
[5] Fix | Delete
# You may redistribute and/or modify this library under the same license
[6] Fix | Delete
# terms as Ruby.
[7] Fix | Delete
#
[8] Fix | Delete
# See GetoptLong for documentation.
[9] Fix | Delete
#
[10] Fix | Delete
# Additional documents and the latest version of `getoptlong.rb' can be
[11] Fix | Delete
# found at http://www.sra.co.jp/people/m-kasahr/ruby/getoptlong/
[12] Fix | Delete
[13] Fix | Delete
# The GetoptLong class allows you to parse command line options similarly to
[14] Fix | Delete
# the GNU getopt_long() C library call. Note, however, that GetoptLong is a
[15] Fix | Delete
# pure Ruby implementation.
[16] Fix | Delete
#
[17] Fix | Delete
# GetoptLong allows for POSIX-style options like <tt>--file</tt> as well
[18] Fix | Delete
# as single letter options like <tt>-f</tt>
[19] Fix | Delete
#
[20] Fix | Delete
# The empty option <tt>--</tt> (two minus symbols) is used to end option
[21] Fix | Delete
# processing. This can be particularly important if options have optional
[22] Fix | Delete
# arguments.
[23] Fix | Delete
#
[24] Fix | Delete
# Here is a simple example of usage:
[25] Fix | Delete
#
[26] Fix | Delete
# require 'getoptlong'
[27] Fix | Delete
#
[28] Fix | Delete
# opts = GetoptLong.new(
[29] Fix | Delete
# [ '--help', '-h', GetoptLong::NO_ARGUMENT ],
[30] Fix | Delete
# [ '--repeat', '-n', GetoptLong::REQUIRED_ARGUMENT ],
[31] Fix | Delete
# [ '--name', GetoptLong::OPTIONAL_ARGUMENT ]
[32] Fix | Delete
# )
[33] Fix | Delete
#
[34] Fix | Delete
# dir = nil
[35] Fix | Delete
# name = nil
[36] Fix | Delete
# repetitions = 1
[37] Fix | Delete
# opts.each do |opt, arg|
[38] Fix | Delete
# case opt
[39] Fix | Delete
# when '--help'
[40] Fix | Delete
# puts <<-EOF
[41] Fix | Delete
# hello [OPTION] ... DIR
[42] Fix | Delete
#
[43] Fix | Delete
# -h, --help:
[44] Fix | Delete
# show help
[45] Fix | Delete
#
[46] Fix | Delete
# --repeat x, -n x:
[47] Fix | Delete
# repeat x times
[48] Fix | Delete
#
[49] Fix | Delete
# --name [name]:
[50] Fix | Delete
# greet user by name, if name not supplied default is John
[51] Fix | Delete
#
[52] Fix | Delete
# DIR: The directory in which to issue the greeting.
[53] Fix | Delete
# EOF
[54] Fix | Delete
# when '--repeat'
[55] Fix | Delete
# repetitions = arg.to_i
[56] Fix | Delete
# when '--name'
[57] Fix | Delete
# if arg == ''
[58] Fix | Delete
# name = 'John'
[59] Fix | Delete
# else
[60] Fix | Delete
# name = arg
[61] Fix | Delete
# end
[62] Fix | Delete
# end
[63] Fix | Delete
# end
[64] Fix | Delete
#
[65] Fix | Delete
# if ARGV.length != 1
[66] Fix | Delete
# puts "Missing dir argument (try --help)"
[67] Fix | Delete
# exit 0
[68] Fix | Delete
# end
[69] Fix | Delete
#
[70] Fix | Delete
# dir = ARGV.shift
[71] Fix | Delete
#
[72] Fix | Delete
# Dir.chdir(dir)
[73] Fix | Delete
# for i in (1..repetitions)
[74] Fix | Delete
# print "Hello"
[75] Fix | Delete
# if name
[76] Fix | Delete
# print ", #{name}"
[77] Fix | Delete
# end
[78] Fix | Delete
# puts
[79] Fix | Delete
# end
[80] Fix | Delete
#
[81] Fix | Delete
# Example command line:
[82] Fix | Delete
#
[83] Fix | Delete
# hello -n 6 --name -- /tmp
[84] Fix | Delete
#
[85] Fix | Delete
class GetoptLong
[86] Fix | Delete
# Version.
[87] Fix | Delete
VERSION = "0.1.1"
[88] Fix | Delete
[89] Fix | Delete
#
[90] Fix | Delete
# Orderings.
[91] Fix | Delete
#
[92] Fix | Delete
ORDERINGS = [REQUIRE_ORDER = 0, PERMUTE = 1, RETURN_IN_ORDER = 2]
[93] Fix | Delete
[94] Fix | Delete
#
[95] Fix | Delete
# Argument flags.
[96] Fix | Delete
#
[97] Fix | Delete
ARGUMENT_FLAGS = [NO_ARGUMENT = 0, REQUIRED_ARGUMENT = 1,
[98] Fix | Delete
OPTIONAL_ARGUMENT = 2]
[99] Fix | Delete
[100] Fix | Delete
#
[101] Fix | Delete
# Status codes.
[102] Fix | Delete
#
[103] Fix | Delete
STATUS_YET, STATUS_STARTED, STATUS_TERMINATED = 0, 1, 2
[104] Fix | Delete
[105] Fix | Delete
#
[106] Fix | Delete
# Error types.
[107] Fix | Delete
#
[108] Fix | Delete
class Error < StandardError; end
[109] Fix | Delete
class AmbiguousOption < Error; end
[110] Fix | Delete
class NeedlessArgument < Error; end
[111] Fix | Delete
class MissingArgument < Error; end
[112] Fix | Delete
class InvalidOption < Error; end
[113] Fix | Delete
[114] Fix | Delete
#
[115] Fix | Delete
# \Set up option processing.
[116] Fix | Delete
#
[117] Fix | Delete
# The options to support are passed to new() as an array of arrays.
[118] Fix | Delete
# Each sub-array contains any number of String option names which carry
[119] Fix | Delete
# the same meaning, and one of the following flags:
[120] Fix | Delete
#
[121] Fix | Delete
# GetoptLong::NO_ARGUMENT :: Option does not take an argument.
[122] Fix | Delete
#
[123] Fix | Delete
# GetoptLong::REQUIRED_ARGUMENT :: Option always takes an argument.
[124] Fix | Delete
#
[125] Fix | Delete
# GetoptLong::OPTIONAL_ARGUMENT :: Option may or may not take an argument.
[126] Fix | Delete
#
[127] Fix | Delete
# The first option name is considered to be the preferred (canonical) name.
[128] Fix | Delete
# Other than that, the elements of each sub-array can be in any order.
[129] Fix | Delete
#
[130] Fix | Delete
def initialize(*arguments)
[131] Fix | Delete
#
[132] Fix | Delete
# Current ordering.
[133] Fix | Delete
#
[134] Fix | Delete
if ENV.include?('POSIXLY_CORRECT')
[135] Fix | Delete
@ordering = REQUIRE_ORDER
[136] Fix | Delete
else
[137] Fix | Delete
@ordering = PERMUTE
[138] Fix | Delete
end
[139] Fix | Delete
[140] Fix | Delete
#
[141] Fix | Delete
# Hash table of option names.
[142] Fix | Delete
# Keys of the table are option names, and their values are canonical
[143] Fix | Delete
# names of the options.
[144] Fix | Delete
#
[145] Fix | Delete
@canonical_names = Hash.new
[146] Fix | Delete
[147] Fix | Delete
#
[148] Fix | Delete
# Hash table of argument flags.
[149] Fix | Delete
# Keys of the table are option names, and their values are argument
[150] Fix | Delete
# flags of the options.
[151] Fix | Delete
#
[152] Fix | Delete
@argument_flags = Hash.new
[153] Fix | Delete
[154] Fix | Delete
#
[155] Fix | Delete
# Whether error messages are output to $stderr.
[156] Fix | Delete
#
[157] Fix | Delete
@quiet = false
[158] Fix | Delete
[159] Fix | Delete
#
[160] Fix | Delete
# Status code.
[161] Fix | Delete
#
[162] Fix | Delete
@status = STATUS_YET
[163] Fix | Delete
[164] Fix | Delete
#
[165] Fix | Delete
# Error code.
[166] Fix | Delete
#
[167] Fix | Delete
@error = nil
[168] Fix | Delete
[169] Fix | Delete
#
[170] Fix | Delete
# Error message.
[171] Fix | Delete
#
[172] Fix | Delete
@error_message = nil
[173] Fix | Delete
[174] Fix | Delete
#
[175] Fix | Delete
# Rest of catenated short options.
[176] Fix | Delete
#
[177] Fix | Delete
@rest_singles = ''
[178] Fix | Delete
[179] Fix | Delete
#
[180] Fix | Delete
# List of non-option-arguments.
[181] Fix | Delete
# Append them to ARGV when option processing is terminated.
[182] Fix | Delete
#
[183] Fix | Delete
@non_option_arguments = Array.new
[184] Fix | Delete
[185] Fix | Delete
if 0 < arguments.length
[186] Fix | Delete
set_options(*arguments)
[187] Fix | Delete
end
[188] Fix | Delete
end
[189] Fix | Delete
[190] Fix | Delete
#
[191] Fix | Delete
# \Set the handling of the ordering of options and arguments.
[192] Fix | Delete
# A RuntimeError is raised if option processing has already started.
[193] Fix | Delete
#
[194] Fix | Delete
# The supplied value must be a member of GetoptLong::ORDERINGS. It alters
[195] Fix | Delete
# the processing of options as follows:
[196] Fix | Delete
#
[197] Fix | Delete
# <b>REQUIRE_ORDER</b> :
[198] Fix | Delete
#
[199] Fix | Delete
# Options are required to occur before non-options.
[200] Fix | Delete
#
[201] Fix | Delete
# Processing of options ends as soon as a word is encountered that has not
[202] Fix | Delete
# been preceded by an appropriate option flag.
[203] Fix | Delete
#
[204] Fix | Delete
# For example, if -a and -b are options which do not take arguments,
[205] Fix | Delete
# parsing command line arguments of '-a one -b two' would result in
[206] Fix | Delete
# 'one', '-b', 'two' being left in ARGV, and only ('-a', '') being
[207] Fix | Delete
# processed as an option/arg pair.
[208] Fix | Delete
#
[209] Fix | Delete
# This is the default ordering, if the environment variable
[210] Fix | Delete
# POSIXLY_CORRECT is set. (This is for compatibility with GNU getopt_long.)
[211] Fix | Delete
#
[212] Fix | Delete
# <b>PERMUTE</b> :
[213] Fix | Delete
#
[214] Fix | Delete
# Options can occur anywhere in the command line parsed. This is the
[215] Fix | Delete
# default behavior.
[216] Fix | Delete
#
[217] Fix | Delete
# Every sequence of words which can be interpreted as an option (with or
[218] Fix | Delete
# without argument) is treated as an option; non-option words are skipped.
[219] Fix | Delete
#
[220] Fix | Delete
# For example, if -a does not require an argument and -b optionally takes
[221] Fix | Delete
# an argument, parsing '-a one -b two three' would result in ('-a','') and
[222] Fix | Delete
# ('-b', 'two') being processed as option/arg pairs, and 'one','three'
[223] Fix | Delete
# being left in ARGV.
[224] Fix | Delete
#
[225] Fix | Delete
# If the ordering is set to PERMUTE but the environment variable
[226] Fix | Delete
# POSIXLY_CORRECT is set, REQUIRE_ORDER is used instead. This is for
[227] Fix | Delete
# compatibility with GNU getopt_long.
[228] Fix | Delete
#
[229] Fix | Delete
# <b>RETURN_IN_ORDER</b> :
[230] Fix | Delete
#
[231] Fix | Delete
# All words on the command line are processed as options. Words not
[232] Fix | Delete
# preceded by a short or long option flag are passed as arguments
[233] Fix | Delete
# with an option of '' (empty string).
[234] Fix | Delete
#
[235] Fix | Delete
# For example, if -a requires an argument but -b does not, a command line
[236] Fix | Delete
# of '-a one -b two three' would result in option/arg pairs of ('-a', 'one')
[237] Fix | Delete
# ('-b', ''), ('', 'two'), ('', 'three') being processed.
[238] Fix | Delete
#
[239] Fix | Delete
def ordering=(ordering)
[240] Fix | Delete
#
[241] Fix | Delete
# The method is failed if option processing has already started.
[242] Fix | Delete
#
[243] Fix | Delete
if @status != STATUS_YET
[244] Fix | Delete
set_error(ArgumentError, "argument error")
[245] Fix | Delete
raise RuntimeError,
[246] Fix | Delete
"invoke ordering=, but option processing has already started"
[247] Fix | Delete
end
[248] Fix | Delete
[249] Fix | Delete
#
[250] Fix | Delete
# Check ordering.
[251] Fix | Delete
#
[252] Fix | Delete
if !ORDERINGS.include?(ordering)
[253] Fix | Delete
raise ArgumentError, "invalid ordering `#{ordering}'"
[254] Fix | Delete
end
[255] Fix | Delete
if ordering == PERMUTE && ENV.include?('POSIXLY_CORRECT')
[256] Fix | Delete
@ordering = REQUIRE_ORDER
[257] Fix | Delete
else
[258] Fix | Delete
@ordering = ordering
[259] Fix | Delete
end
[260] Fix | Delete
end
[261] Fix | Delete
[262] Fix | Delete
#
[263] Fix | Delete
# Return ordering.
[264] Fix | Delete
#
[265] Fix | Delete
attr_reader :ordering
[266] Fix | Delete
[267] Fix | Delete
#
[268] Fix | Delete
# \Set options. Takes the same argument as GetoptLong.new.
[269] Fix | Delete
#
[270] Fix | Delete
# Raises a RuntimeError if option processing has already started.
[271] Fix | Delete
#
[272] Fix | Delete
def set_options(*arguments)
[273] Fix | Delete
#
[274] Fix | Delete
# The method is failed if option processing has already started.
[275] Fix | Delete
#
[276] Fix | Delete
if @status != STATUS_YET
[277] Fix | Delete
raise RuntimeError,
[278] Fix | Delete
"invoke set_options, but option processing has already started"
[279] Fix | Delete
end
[280] Fix | Delete
[281] Fix | Delete
#
[282] Fix | Delete
# Clear tables of option names and argument flags.
[283] Fix | Delete
#
[284] Fix | Delete
@canonical_names.clear
[285] Fix | Delete
@argument_flags.clear
[286] Fix | Delete
[287] Fix | Delete
arguments.each do |arg|
[288] Fix | Delete
if !arg.is_a?(Array)
[289] Fix | Delete
raise ArgumentError, "the option list contains non-Array argument"
[290] Fix | Delete
end
[291] Fix | Delete
[292] Fix | Delete
#
[293] Fix | Delete
# Find an argument flag and it set to `argument_flag'.
[294] Fix | Delete
#
[295] Fix | Delete
argument_flag = nil
[296] Fix | Delete
arg.each do |i|
[297] Fix | Delete
if ARGUMENT_FLAGS.include?(i)
[298] Fix | Delete
if argument_flag != nil
[299] Fix | Delete
raise ArgumentError, "too many argument-flags"
[300] Fix | Delete
end
[301] Fix | Delete
argument_flag = i
[302] Fix | Delete
end
[303] Fix | Delete
end
[304] Fix | Delete
[305] Fix | Delete
raise ArgumentError, "no argument-flag" if argument_flag == nil
[306] Fix | Delete
[307] Fix | Delete
canonical_name = nil
[308] Fix | Delete
arg.each do |i|
[309] Fix | Delete
#
[310] Fix | Delete
# Check an option name.
[311] Fix | Delete
#
[312] Fix | Delete
next if i == argument_flag
[313] Fix | Delete
begin
[314] Fix | Delete
if !i.is_a?(String) || i !~ /\A-([^-]|-.+)\z/
[315] Fix | Delete
raise ArgumentError, "an invalid option `#{i}'"
[316] Fix | Delete
end
[317] Fix | Delete
if (@canonical_names.include?(i))
[318] Fix | Delete
raise ArgumentError, "option redefined `#{i}'"
[319] Fix | Delete
end
[320] Fix | Delete
rescue
[321] Fix | Delete
@canonical_names.clear
[322] Fix | Delete
@argument_flags.clear
[323] Fix | Delete
raise
[324] Fix | Delete
end
[325] Fix | Delete
[326] Fix | Delete
#
[327] Fix | Delete
# Register the option (`i') to the `@canonical_names' and
[328] Fix | Delete
# `@canonical_names' Hashes.
[329] Fix | Delete
#
[330] Fix | Delete
if canonical_name == nil
[331] Fix | Delete
canonical_name = i
[332] Fix | Delete
end
[333] Fix | Delete
@canonical_names[i] = canonical_name
[334] Fix | Delete
@argument_flags[i] = argument_flag
[335] Fix | Delete
end
[336] Fix | Delete
raise ArgumentError, "no option name" if canonical_name == nil
[337] Fix | Delete
end
[338] Fix | Delete
return self
[339] Fix | Delete
end
[340] Fix | Delete
[341] Fix | Delete
#
[342] Fix | Delete
# \Set/Unset `quiet' mode.
[343] Fix | Delete
#
[344] Fix | Delete
attr_writer :quiet
[345] Fix | Delete
[346] Fix | Delete
#
[347] Fix | Delete
# Return the flag of `quiet' mode.
[348] Fix | Delete
#
[349] Fix | Delete
attr_reader :quiet
[350] Fix | Delete
[351] Fix | Delete
#
[352] Fix | Delete
# `quiet?' is an alias of `quiet'.
[353] Fix | Delete
#
[354] Fix | Delete
alias quiet? quiet
[355] Fix | Delete
[356] Fix | Delete
#
[357] Fix | Delete
# Explicitly terminate option processing.
[358] Fix | Delete
#
[359] Fix | Delete
def terminate
[360] Fix | Delete
return nil if @status == STATUS_TERMINATED
[361] Fix | Delete
raise RuntimeError, "an error has occurred" if @error != nil
[362] Fix | Delete
[363] Fix | Delete
@status = STATUS_TERMINATED
[364] Fix | Delete
@non_option_arguments.reverse_each do |argument|
[365] Fix | Delete
ARGV.unshift(argument)
[366] Fix | Delete
end
[367] Fix | Delete
[368] Fix | Delete
@canonical_names = nil
[369] Fix | Delete
@argument_flags = nil
[370] Fix | Delete
@rest_singles = nil
[371] Fix | Delete
@non_option_arguments = nil
[372] Fix | Delete
[373] Fix | Delete
return self
[374] Fix | Delete
end
[375] Fix | Delete
[376] Fix | Delete
#
[377] Fix | Delete
# Returns true if option processing has terminated, false otherwise.
[378] Fix | Delete
#
[379] Fix | Delete
def terminated?
[380] Fix | Delete
return @status == STATUS_TERMINATED
[381] Fix | Delete
end
[382] Fix | Delete
[383] Fix | Delete
#
[384] Fix | Delete
# \Set an error (a protected method).
[385] Fix | Delete
#
[386] Fix | Delete
def set_error(type, message)
[387] Fix | Delete
$stderr.print("#{$0}: #{message}\n") if !@quiet
[388] Fix | Delete
[389] Fix | Delete
@error = type
[390] Fix | Delete
@error_message = message
[391] Fix | Delete
@canonical_names = nil
[392] Fix | Delete
@argument_flags = nil
[393] Fix | Delete
@rest_singles = nil
[394] Fix | Delete
@non_option_arguments = nil
[395] Fix | Delete
[396] Fix | Delete
raise type, message
[397] Fix | Delete
end
[398] Fix | Delete
protected :set_error
[399] Fix | Delete
[400] Fix | Delete
#
[401] Fix | Delete
# Examine whether an option processing is failed.
[402] Fix | Delete
#
[403] Fix | Delete
attr_reader :error
[404] Fix | Delete
[405] Fix | Delete
#
[406] Fix | Delete
# `error?' is an alias of `error'.
[407] Fix | Delete
#
[408] Fix | Delete
alias error? error
[409] Fix | Delete
[410] Fix | Delete
# Return the appropriate error message in POSIX-defined format.
[411] Fix | Delete
# If no error has occurred, returns nil.
[412] Fix | Delete
#
[413] Fix | Delete
def error_message
[414] Fix | Delete
return @error_message
[415] Fix | Delete
end
[416] Fix | Delete
[417] Fix | Delete
#
[418] Fix | Delete
# Get next option name and its argument, as an Array of two elements.
[419] Fix | Delete
#
[420] Fix | Delete
# The option name is always converted to the first (preferred)
[421] Fix | Delete
# name given in the original options to GetoptLong.new.
[422] Fix | Delete
#
[423] Fix | Delete
# Example: ['--option', 'value']
[424] Fix | Delete
#
[425] Fix | Delete
# Returns nil if the processing is complete (as determined by
[426] Fix | Delete
# STATUS_TERMINATED).
[427] Fix | Delete
#
[428] Fix | Delete
def get
[429] Fix | Delete
option_name, option_argument = nil, ''
[430] Fix | Delete
[431] Fix | Delete
#
[432] Fix | Delete
# Check status.
[433] Fix | Delete
#
[434] Fix | Delete
return nil if @error != nil
[435] Fix | Delete
case @status
[436] Fix | Delete
when STATUS_YET
[437] Fix | Delete
@status = STATUS_STARTED
[438] Fix | Delete
when STATUS_TERMINATED
[439] Fix | Delete
return nil
[440] Fix | Delete
end
[441] Fix | Delete
[442] Fix | Delete
#
[443] Fix | Delete
# Get next option argument.
[444] Fix | Delete
#
[445] Fix | Delete
if 0 < @rest_singles.length
[446] Fix | Delete
argument = '-' + @rest_singles
[447] Fix | Delete
elsif (ARGV.length == 0)
[448] Fix | Delete
terminate
[449] Fix | Delete
return nil
[450] Fix | Delete
elsif @ordering == PERMUTE
[451] Fix | Delete
while 0 < ARGV.length && ARGV[0] !~ /\A-./
[452] Fix | Delete
@non_option_arguments.push(ARGV.shift)
[453] Fix | Delete
end
[454] Fix | Delete
if ARGV.length == 0
[455] Fix | Delete
terminate
[456] Fix | Delete
return nil
[457] Fix | Delete
end
[458] Fix | Delete
argument = ARGV.shift
[459] Fix | Delete
elsif @ordering == REQUIRE_ORDER
[460] Fix | Delete
if (ARGV[0] !~ /\A-./)
[461] Fix | Delete
terminate
[462] Fix | Delete
return nil
[463] Fix | Delete
end
[464] Fix | Delete
argument = ARGV.shift
[465] Fix | Delete
else
[466] Fix | Delete
argument = ARGV.shift
[467] Fix | Delete
end
[468] Fix | Delete
[469] Fix | Delete
#
[470] Fix | Delete
# Check the special argument `--'.
[471] Fix | Delete
# `--' indicates the end of the option list.
[472] Fix | Delete
#
[473] Fix | Delete
if argument == '--' && @rest_singles.length == 0
[474] Fix | Delete
terminate
[475] Fix | Delete
return nil
[476] Fix | Delete
end
[477] Fix | Delete
[478] Fix | Delete
#
[479] Fix | Delete
# Check for long and short options.
[480] Fix | Delete
#
[481] Fix | Delete
if argument =~ /\A(--[^=]+)/ && @rest_singles.length == 0
[482] Fix | Delete
#
[483] Fix | Delete
# This is a long style option, which start with `--'.
[484] Fix | Delete
#
[485] Fix | Delete
pattern = $1
[486] Fix | Delete
if @canonical_names.include?(pattern)
[487] Fix | Delete
option_name = pattern
[488] Fix | Delete
else
[489] Fix | Delete
#
[490] Fix | Delete
# The option `option_name' is not registered in `@canonical_names'.
[491] Fix | Delete
# It may be an abbreviated.
[492] Fix | Delete
#
[493] Fix | Delete
matches = []
[494] Fix | Delete
@canonical_names.each_key do |key|
[495] Fix | Delete
if key.index(pattern) == 0
[496] Fix | Delete
option_name = key
[497] Fix | Delete
matches << key
[498] Fix | Delete
end
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function