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