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