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