Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../usr/lib64/python2....
File: getopt.py
"""Parser for command line options.
[0] Fix | Delete
[1] Fix | Delete
This module helps scripts to parse the command line arguments in
[2] Fix | Delete
sys.argv. It supports the same conventions as the Unix getopt()
[3] Fix | Delete
function (including the special meanings of arguments of the form `-'
[4] Fix | Delete
and `--'). Long options similar to those supported by GNU software
[5] Fix | Delete
may be used as well via an optional third argument. This module
[6] Fix | Delete
provides two functions and an exception:
[7] Fix | Delete
[8] Fix | Delete
getopt() -- Parse command line options
[9] Fix | Delete
gnu_getopt() -- Like getopt(), but allow option and non-option arguments
[10] Fix | Delete
to be intermixed.
[11] Fix | Delete
GetoptError -- exception (class) raised with 'opt' attribute, which is the
[12] Fix | Delete
option involved with the exception.
[13] Fix | Delete
"""
[14] Fix | Delete
[15] Fix | Delete
# Long option support added by Lars Wirzenius <liw@iki.fi>.
[16] Fix | Delete
#
[17] Fix | Delete
# Gerrit Holl <gerrit@nl.linux.org> moved the string-based exceptions
[18] Fix | Delete
# to class-based exceptions.
[19] Fix | Delete
#
[20] Fix | Delete
# Peter Astrand <astrand@lysator.liu.se> added gnu_getopt().
[21] Fix | Delete
#
[22] Fix | Delete
# TODO for gnu_getopt():
[23] Fix | Delete
#
[24] Fix | Delete
# - GNU getopt_long_only mechanism
[25] Fix | Delete
# - allow the caller to specify ordering
[26] Fix | Delete
# - RETURN_IN_ORDER option
[27] Fix | Delete
# - GNU extension with '-' as first character of option string
[28] Fix | Delete
# - optional arguments, specified by double colons
[29] Fix | Delete
# - an option string with a W followed by semicolon should
[30] Fix | Delete
# treat "-W foo" as "--foo"
[31] Fix | Delete
[32] Fix | Delete
__all__ = ["GetoptError","error","getopt","gnu_getopt"]
[33] Fix | Delete
[34] Fix | Delete
import os
[35] Fix | Delete
[36] Fix | Delete
class GetoptError(Exception):
[37] Fix | Delete
opt = ''
[38] Fix | Delete
msg = ''
[39] Fix | Delete
def __init__(self, msg, opt=''):
[40] Fix | Delete
self.msg = msg
[41] Fix | Delete
self.opt = opt
[42] Fix | Delete
Exception.__init__(self, msg, opt)
[43] Fix | Delete
[44] Fix | Delete
def __str__(self):
[45] Fix | Delete
return self.msg
[46] Fix | Delete
[47] Fix | Delete
error = GetoptError # backward compatibility
[48] Fix | Delete
[49] Fix | Delete
def getopt(args, shortopts, longopts = []):
[50] Fix | Delete
"""getopt(args, options[, long_options]) -> opts, args
[51] Fix | Delete
[52] Fix | Delete
Parses command line options and parameter list. args is the
[53] Fix | Delete
argument list to be parsed, without the leading reference to the
[54] Fix | Delete
running program. Typically, this means "sys.argv[1:]". shortopts
[55] Fix | Delete
is the string of option letters that the script wants to
[56] Fix | Delete
recognize, with options that require an argument followed by a
[57] Fix | Delete
colon (i.e., the same format that Unix getopt() uses). If
[58] Fix | Delete
specified, longopts is a list of strings with the names of the
[59] Fix | Delete
long options which should be supported. The leading '--'
[60] Fix | Delete
characters should not be included in the option name. Options
[61] Fix | Delete
which require an argument should be followed by an equal sign
[62] Fix | Delete
('=').
[63] Fix | Delete
[64] Fix | Delete
The return value consists of two elements: the first is a list of
[65] Fix | Delete
(option, value) pairs; the second is the list of program arguments
[66] Fix | Delete
left after the option list was stripped (this is a trailing slice
[67] Fix | Delete
of the first argument). Each option-and-value pair returned has
[68] Fix | Delete
the option as its first element, prefixed with a hyphen (e.g.,
[69] Fix | Delete
'-x'), and the option argument as its second element, or an empty
[70] Fix | Delete
string if the option has no argument. The options occur in the
[71] Fix | Delete
list in the same order in which they were found, thus allowing
[72] Fix | Delete
multiple occurrences. Long and short options may be mixed.
[73] Fix | Delete
[74] Fix | Delete
"""
[75] Fix | Delete
[76] Fix | Delete
opts = []
[77] Fix | Delete
if type(longopts) == type(""):
[78] Fix | Delete
longopts = [longopts]
[79] Fix | Delete
else:
[80] Fix | Delete
longopts = list(longopts)
[81] Fix | Delete
while args and args[0].startswith('-') and args[0] != '-':
[82] Fix | Delete
if args[0] == '--':
[83] Fix | Delete
args = args[1:]
[84] Fix | Delete
break
[85] Fix | Delete
if args[0].startswith('--'):
[86] Fix | Delete
opts, args = do_longs(opts, args[0][2:], longopts, args[1:])
[87] Fix | Delete
else:
[88] Fix | Delete
opts, args = do_shorts(opts, args[0][1:], shortopts, args[1:])
[89] Fix | Delete
[90] Fix | Delete
return opts, args
[91] Fix | Delete
[92] Fix | Delete
def gnu_getopt(args, shortopts, longopts = []):
[93] Fix | Delete
"""getopt(args, options[, long_options]) -> opts, args
[94] Fix | Delete
[95] Fix | Delete
This function works like getopt(), except that GNU style scanning
[96] Fix | Delete
mode is used by default. This means that option and non-option
[97] Fix | Delete
arguments may be intermixed. The getopt() function stops
[98] Fix | Delete
processing options as soon as a non-option argument is
[99] Fix | Delete
encountered.
[100] Fix | Delete
[101] Fix | Delete
If the first character of the option string is `+', or if the
[102] Fix | Delete
environment variable POSIXLY_CORRECT is set, then option
[103] Fix | Delete
processing stops as soon as a non-option argument is encountered.
[104] Fix | Delete
[105] Fix | Delete
"""
[106] Fix | Delete
[107] Fix | Delete
opts = []
[108] Fix | Delete
prog_args = []
[109] Fix | Delete
if isinstance(longopts, str):
[110] Fix | Delete
longopts = [longopts]
[111] Fix | Delete
else:
[112] Fix | Delete
longopts = list(longopts)
[113] Fix | Delete
[114] Fix | Delete
# Allow options after non-option arguments?
[115] Fix | Delete
if shortopts.startswith('+'):
[116] Fix | Delete
shortopts = shortopts[1:]
[117] Fix | Delete
all_options_first = True
[118] Fix | Delete
elif os.environ.get("POSIXLY_CORRECT"):
[119] Fix | Delete
all_options_first = True
[120] Fix | Delete
else:
[121] Fix | Delete
all_options_first = False
[122] Fix | Delete
[123] Fix | Delete
while args:
[124] Fix | Delete
if args[0] == '--':
[125] Fix | Delete
prog_args += args[1:]
[126] Fix | Delete
break
[127] Fix | Delete
[128] Fix | Delete
if args[0][:2] == '--':
[129] Fix | Delete
opts, args = do_longs(opts, args[0][2:], longopts, args[1:])
[130] Fix | Delete
elif args[0][:1] == '-' and args[0] != '-':
[131] Fix | Delete
opts, args = do_shorts(opts, args[0][1:], shortopts, args[1:])
[132] Fix | Delete
else:
[133] Fix | Delete
if all_options_first:
[134] Fix | Delete
prog_args += args
[135] Fix | Delete
break
[136] Fix | Delete
else:
[137] Fix | Delete
prog_args.append(args[0])
[138] Fix | Delete
args = args[1:]
[139] Fix | Delete
[140] Fix | Delete
return opts, prog_args
[141] Fix | Delete
[142] Fix | Delete
def do_longs(opts, opt, longopts, args):
[143] Fix | Delete
try:
[144] Fix | Delete
i = opt.index('=')
[145] Fix | Delete
except ValueError:
[146] Fix | Delete
optarg = None
[147] Fix | Delete
else:
[148] Fix | Delete
opt, optarg = opt[:i], opt[i+1:]
[149] Fix | Delete
[150] Fix | Delete
has_arg, opt = long_has_args(opt, longopts)
[151] Fix | Delete
if has_arg:
[152] Fix | Delete
if optarg is None:
[153] Fix | Delete
if not args:
[154] Fix | Delete
raise GetoptError('option --%s requires argument' % opt, opt)
[155] Fix | Delete
optarg, args = args[0], args[1:]
[156] Fix | Delete
elif optarg is not None:
[157] Fix | Delete
raise GetoptError('option --%s must not have an argument' % opt, opt)
[158] Fix | Delete
opts.append(('--' + opt, optarg or ''))
[159] Fix | Delete
return opts, args
[160] Fix | Delete
[161] Fix | Delete
# Return:
[162] Fix | Delete
# has_arg?
[163] Fix | Delete
# full option name
[164] Fix | Delete
def long_has_args(opt, longopts):
[165] Fix | Delete
possibilities = [o for o in longopts if o.startswith(opt)]
[166] Fix | Delete
if not possibilities:
[167] Fix | Delete
raise GetoptError('option --%s not recognized' % opt, opt)
[168] Fix | Delete
# Is there an exact match?
[169] Fix | Delete
if opt in possibilities:
[170] Fix | Delete
return False, opt
[171] Fix | Delete
elif opt + '=' in possibilities:
[172] Fix | Delete
return True, opt
[173] Fix | Delete
# No exact match, so better be unique.
[174] Fix | Delete
if len(possibilities) > 1:
[175] Fix | Delete
# XXX since possibilities contains all valid continuations, might be
[176] Fix | Delete
# nice to work them into the error msg
[177] Fix | Delete
raise GetoptError('option --%s not a unique prefix' % opt, opt)
[178] Fix | Delete
assert len(possibilities) == 1
[179] Fix | Delete
unique_match = possibilities[0]
[180] Fix | Delete
has_arg = unique_match.endswith('=')
[181] Fix | Delete
if has_arg:
[182] Fix | Delete
unique_match = unique_match[:-1]
[183] Fix | Delete
return has_arg, unique_match
[184] Fix | Delete
[185] Fix | Delete
def do_shorts(opts, optstring, shortopts, args):
[186] Fix | Delete
while optstring != '':
[187] Fix | Delete
opt, optstring = optstring[0], optstring[1:]
[188] Fix | Delete
if short_has_arg(opt, shortopts):
[189] Fix | Delete
if optstring == '':
[190] Fix | Delete
if not args:
[191] Fix | Delete
raise GetoptError('option -%s requires argument' % opt,
[192] Fix | Delete
opt)
[193] Fix | Delete
optstring, args = args[0], args[1:]
[194] Fix | Delete
optarg, optstring = optstring, ''
[195] Fix | Delete
else:
[196] Fix | Delete
optarg = ''
[197] Fix | Delete
opts.append(('-' + opt, optarg))
[198] Fix | Delete
return opts, args
[199] Fix | Delete
[200] Fix | Delete
def short_has_arg(opt, shortopts):
[201] Fix | Delete
for i in range(len(shortopts)):
[202] Fix | Delete
if opt == shortopts[i] != ':':
[203] Fix | Delete
return shortopts.startswith(':', i+1)
[204] Fix | Delete
raise GetoptError('option -%s not recognized' % opt, opt)
[205] Fix | Delete
[206] Fix | Delete
if __name__ == '__main__':
[207] Fix | Delete
import sys
[208] Fix | Delete
print getopt(sys.argv[1:], "a:b", ["alpha=", "beta"])
[209] Fix | Delete
[210] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function