Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python3....
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 Åstrand <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
try:
[36] Fix | Delete
from gettext import gettext as _
[37] Fix | Delete
except ImportError:
[38] Fix | Delete
# Bootstrapping Python: gettext's dependencies not built yet
[39] Fix | Delete
def _(s): return s
[40] Fix | Delete
[41] Fix | Delete
class GetoptError(Exception):
[42] Fix | Delete
opt = ''
[43] Fix | Delete
msg = ''
[44] Fix | Delete
def __init__(self, msg, opt=''):
[45] Fix | Delete
self.msg = msg
[46] Fix | Delete
self.opt = opt
[47] Fix | Delete
Exception.__init__(self, msg, opt)
[48] Fix | Delete
[49] Fix | Delete
def __str__(self):
[50] Fix | Delete
return self.msg
[51] Fix | Delete
[52] Fix | Delete
error = GetoptError # backward compatibility
[53] Fix | Delete
[54] Fix | Delete
def getopt(args, shortopts, longopts = []):
[55] Fix | Delete
"""getopt(args, options[, long_options]) -> opts, args
[56] Fix | Delete
[57] Fix | Delete
Parses command line options and parameter list. args is the
[58] Fix | Delete
argument list to be parsed, without the leading reference to the
[59] Fix | Delete
running program. Typically, this means "sys.argv[1:]". shortopts
[60] Fix | Delete
is the string of option letters that the script wants to
[61] Fix | Delete
recognize, with options that require an argument followed by a
[62] Fix | Delete
colon (i.e., the same format that Unix getopt() uses). If
[63] Fix | Delete
specified, longopts is a list of strings with the names of the
[64] Fix | Delete
long options which should be supported. The leading '--'
[65] Fix | Delete
characters should not be included in the option name. Options
[66] Fix | Delete
which require an argument should be followed by an equal sign
[67] Fix | Delete
('=').
[68] Fix | Delete
[69] Fix | Delete
The return value consists of two elements: the first is a list of
[70] Fix | Delete
(option, value) pairs; the second is the list of program arguments
[71] Fix | Delete
left after the option list was stripped (this is a trailing slice
[72] Fix | Delete
of the first argument). Each option-and-value pair returned has
[73] Fix | Delete
the option as its first element, prefixed with a hyphen (e.g.,
[74] Fix | Delete
'-x'), and the option argument as its second element, or an empty
[75] Fix | Delete
string if the option has no argument. The options occur in the
[76] Fix | Delete
list in the same order in which they were found, thus allowing
[77] Fix | Delete
multiple occurrences. Long and short options may be mixed.
[78] Fix | Delete
[79] Fix | Delete
"""
[80] Fix | Delete
[81] Fix | Delete
opts = []
[82] Fix | Delete
if type(longopts) == type(""):
[83] Fix | Delete
longopts = [longopts]
[84] Fix | Delete
else:
[85] Fix | Delete
longopts = list(longopts)
[86] Fix | Delete
while args and args[0].startswith('-') and args[0] != '-':
[87] Fix | Delete
if args[0] == '--':
[88] Fix | Delete
args = args[1:]
[89] Fix | Delete
break
[90] Fix | Delete
if args[0].startswith('--'):
[91] Fix | Delete
opts, args = do_longs(opts, args[0][2:], longopts, args[1:])
[92] Fix | Delete
else:
[93] Fix | Delete
opts, args = do_shorts(opts, args[0][1:], shortopts, args[1:])
[94] Fix | Delete
[95] Fix | Delete
return opts, args
[96] Fix | Delete
[97] Fix | Delete
def gnu_getopt(args, shortopts, longopts = []):
[98] Fix | Delete
"""getopt(args, options[, long_options]) -> opts, args
[99] Fix | Delete
[100] Fix | Delete
This function works like getopt(), except that GNU style scanning
[101] Fix | Delete
mode is used by default. This means that option and non-option
[102] Fix | Delete
arguments may be intermixed. The getopt() function stops
[103] Fix | Delete
processing options as soon as a non-option argument is
[104] Fix | Delete
encountered.
[105] Fix | Delete
[106] Fix | Delete
If the first character of the option string is `+', or if the
[107] Fix | Delete
environment variable POSIXLY_CORRECT is set, then option
[108] Fix | Delete
processing stops as soon as a non-option argument is encountered.
[109] Fix | Delete
[110] Fix | Delete
"""
[111] Fix | Delete
[112] Fix | Delete
opts = []
[113] Fix | Delete
prog_args = []
[114] Fix | Delete
if isinstance(longopts, str):
[115] Fix | Delete
longopts = [longopts]
[116] Fix | Delete
else:
[117] Fix | Delete
longopts = list(longopts)
[118] Fix | Delete
[119] Fix | Delete
# Allow options after non-option arguments?
[120] Fix | Delete
if shortopts.startswith('+'):
[121] Fix | Delete
shortopts = shortopts[1:]
[122] Fix | Delete
all_options_first = True
[123] Fix | Delete
elif os.environ.get("POSIXLY_CORRECT"):
[124] Fix | Delete
all_options_first = True
[125] Fix | Delete
else:
[126] Fix | Delete
all_options_first = False
[127] Fix | Delete
[128] Fix | Delete
while args:
[129] Fix | Delete
if args[0] == '--':
[130] Fix | Delete
prog_args += args[1:]
[131] Fix | Delete
break
[132] Fix | Delete
[133] Fix | Delete
if args[0][:2] == '--':
[134] Fix | Delete
opts, args = do_longs(opts, args[0][2:], longopts, args[1:])
[135] Fix | Delete
elif args[0][:1] == '-' and args[0] != '-':
[136] Fix | Delete
opts, args = do_shorts(opts, args[0][1:], shortopts, args[1:])
[137] Fix | Delete
else:
[138] Fix | Delete
if all_options_first:
[139] Fix | Delete
prog_args += args
[140] Fix | Delete
break
[141] Fix | Delete
else:
[142] Fix | Delete
prog_args.append(args[0])
[143] Fix | Delete
args = args[1:]
[144] Fix | Delete
[145] Fix | Delete
return opts, prog_args
[146] Fix | Delete
[147] Fix | Delete
def do_longs(opts, opt, longopts, args):
[148] Fix | Delete
try:
[149] Fix | Delete
i = opt.index('=')
[150] Fix | Delete
except ValueError:
[151] Fix | Delete
optarg = None
[152] Fix | Delete
else:
[153] Fix | Delete
opt, optarg = opt[:i], opt[i+1:]
[154] Fix | Delete
[155] Fix | Delete
has_arg, opt = long_has_args(opt, longopts)
[156] Fix | Delete
if has_arg:
[157] Fix | Delete
if optarg is None:
[158] Fix | Delete
if not args:
[159] Fix | Delete
raise GetoptError(_('option --%s requires argument') % opt, opt)
[160] Fix | Delete
optarg, args = args[0], args[1:]
[161] Fix | Delete
elif optarg is not None:
[162] Fix | Delete
raise GetoptError(_('option --%s must not have an argument') % opt, opt)
[163] Fix | Delete
opts.append(('--' + opt, optarg or ''))
[164] Fix | Delete
return opts, args
[165] Fix | Delete
[166] Fix | Delete
# Return:
[167] Fix | Delete
# has_arg?
[168] Fix | Delete
# full option name
[169] Fix | Delete
def long_has_args(opt, longopts):
[170] Fix | Delete
possibilities = [o for o in longopts if o.startswith(opt)]
[171] Fix | Delete
if not possibilities:
[172] Fix | Delete
raise GetoptError(_('option --%s not recognized') % opt, opt)
[173] Fix | Delete
# Is there an exact match?
[174] Fix | Delete
if opt in possibilities:
[175] Fix | Delete
return False, opt
[176] Fix | Delete
elif opt + '=' in possibilities:
[177] Fix | Delete
return True, opt
[178] Fix | Delete
# No exact match, so better be unique.
[179] Fix | Delete
if len(possibilities) > 1:
[180] Fix | Delete
# XXX since possibilities contains all valid continuations, might be
[181] Fix | Delete
# nice to work them into the error msg
[182] Fix | Delete
raise GetoptError(_('option --%s not a unique prefix') % opt, opt)
[183] Fix | Delete
assert len(possibilities) == 1
[184] Fix | Delete
unique_match = possibilities[0]
[185] Fix | Delete
has_arg = unique_match.endswith('=')
[186] Fix | Delete
if has_arg:
[187] Fix | Delete
unique_match = unique_match[:-1]
[188] Fix | Delete
return has_arg, unique_match
[189] Fix | Delete
[190] Fix | Delete
def do_shorts(opts, optstring, shortopts, args):
[191] Fix | Delete
while optstring != '':
[192] Fix | Delete
opt, optstring = optstring[0], optstring[1:]
[193] Fix | Delete
if short_has_arg(opt, shortopts):
[194] Fix | Delete
if optstring == '':
[195] Fix | Delete
if not args:
[196] Fix | Delete
raise GetoptError(_('option -%s requires argument') % opt,
[197] Fix | Delete
opt)
[198] Fix | Delete
optstring, args = args[0], args[1:]
[199] Fix | Delete
optarg, optstring = optstring, ''
[200] Fix | Delete
else:
[201] Fix | Delete
optarg = ''
[202] Fix | Delete
opts.append(('-' + opt, optarg))
[203] Fix | Delete
return opts, args
[204] Fix | Delete
[205] Fix | Delete
def short_has_arg(opt, shortopts):
[206] Fix | Delete
for i in range(len(shortopts)):
[207] Fix | Delete
if opt == shortopts[i] != ':':
[208] Fix | Delete
return shortopts.startswith(':', i+1)
[209] Fix | Delete
raise GetoptError(_('option -%s not recognized') % opt, opt)
[210] Fix | Delete
[211] Fix | Delete
if __name__ == '__main__':
[212] Fix | Delete
import sys
[213] Fix | Delete
print(getopt(sys.argv[1:], "a:b", ["alpha=", "beta"]))
[214] Fix | Delete
[215] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function