Class attributes:
standard_option_list : [Option]
list of standard options that will be accepted by all instances
of this parser class (intended to be overridden by subclasses).
Instance attributes:
usage : string
a usage string for your program. Before it is displayed
to the user, "%prog" will be expanded to the name of
your program (self.prog or os.path.basename(sys.argv[0])).
prog : string
the name of the current program (to override
os.path.basename(sys.argv[0])).
option_groups : [OptionGroup]
list of option groups in this parser (option groups are
irrelevant for parsing the command-line, but very useful
for generating help)
allow_interspersed_args : bool = true
if true, positional arguments may be interspersed with options.
Assuming -a and -b each take a single argument, the command-line
-ablah foo bar -bboo baz
will be interpreted the same as
-ablah -bboo -- foo bar baz
If this flag were false, that command line would be interpreted as
-ablah -- foo bar -bboo baz
-- ie. we stop processing options as soon as we see the first
non-option argument. (This is the tradition followed by
Python's getopt module, Perl's Getopt::Std, and other argument-
parsing libraries, but it is generally annoying to users.)
process_default_values : bool = true
if true, option default values are processed similarly to option
values from the command line: that is, they are passed to the
type-checking function for the option's type (as long as the
default value is a string). (This really only matters if you
have defined custom types; see SF bug #955889.) Set it to false
to restore the behaviour of Optik 1.4.1 and earlier.
rargs : [string]
the argument list currently being parsed. Only set when
parse_args() is active, and continually trimmed down as
we consume arguments. Mainly there for the benefit of
callback options.
largs : [string]
the list of leftover arguments that we have skipped while
parsing options. If allow_interspersed_args is false, this
list is always empty.
values : Values
the set of option values currently being accumulated. Only
set when parse_args() is active. Also mainly for callbacks.
Because of the 'rargs', 'largs', and 'values' attributes,
OptionParser is not thread-safe. If, for some perverse reason, you
need to parse command-line arguments simultaneously in different
threads, use different OptionParser instances.
class attributes and properties:
standard_option_list: []
methods:
def __init__(self, usage=None, option_list=None, option_class=<class py.__.compat.optparse.Option at 0x703fa360>, version=None, conflict_handler='error', description=None, formatter=None, add_help_option=True, prog=None):
*no docstring available*
arguments:
- self: <Instance of Class OptionParser>
- usage: <String>
- option_list: <None>
- option_class: Class Option
- version: <None>
- conflict_handler: <String>
- description: <None>
- formatter: <None>
- add_help_option: <Boolean>
- prog: <None>
return value:
<None>
source: compat/optparse.py
1097 |
1098 |
1099 |
1100 |
1101 |
1102 |
1103 |
1104 |
1105 |
1106 |
1107 |
1108 |
1109 |
1110 |
1111 |
1112 |
1113 |
1114 |
1115 |
1116 |
1117 |
1118 |
1119 |
1120 |
1121 |
1122 |
1123 |
1124 |
1125 |
1126 | |
def __init__(self, |
usage=None, |
option_list=None, |
option_class=Option, |
version=None, |
conflict_handler="error", |
description=None, |
formatter=None, |
add_help_option=True, |
prog=None): |
OptionContainer.__init__( |
self, option_class, conflict_handler, description) |
self.set_usage(usage) |
self.prog = prog |
self.version = version |
self.allow_interspersed_args = True |
self.process_default_values = True |
if formatter is None: |
formatter = IndentedHelpFormatter() |
self.formatter = formatter |
self.formatter.set_parser(self) |
|
|
|
|
|
self._populate_option_list(option_list, |
add_help=add_help_option) |
|
self._init_parsing_state() | |
def add_option(self, *args, **kwargs):
add_option(Option)
add_option(opt_str, ..., kwarg=val, ...)
source: compat/optparse.py
926 |
927 |
928 |
929 |
930 |
931 |
932 |
933 |
934 |
935 |
936 |
937 |
938 |
939 |
940 |
941 |
942 |
943 |
944 |
945 |
946 |
947 |
948 |
949 |
950 |
951 |
952 |
953 |
954 | |
def add_option(self, *args, **kwargs): |
"""add_option(Option) |
add_option(opt_str, ..., kwarg=val, ...) |
""" |
if type(args[0]) is types.StringType: |
option = self.option_class(*args, **kwargs) |
elif len(args) == 1 and not kwargs: |
option = args[0] |
if not isinstance(option, Option): |
raise TypeError, "not an Option instance: %r" % option |
else: |
raise TypeError, "invalid arguments" |
|
self._check_conflict(option) |
|
self.option_list.append(option) |
option.container = self |
for opt in option._short_opts: |
self._short_opt[opt] = option |
for opt in option._long_opts: |
self._long_opt[opt] = option |
|
if option.dest is not None: |
if option.default is not NO_DEFAULT: |
self.defaults[option.dest] = option.default |
elif not self.defaults.has_key(option.dest): |
self.defaults[option.dest] = None |
|
return option | |
def add_option_group(self, *args, **kwargs):
*no docstring available*
source: compat/optparse.py
1214 |
1215 |
1216 |
1217 |
1218 |
1219 |
1220 |
1221 |
1222 |
1223 |
1224 |
1225 |
1226 |
1227 |
1228 | |
def add_option_group(self, *args, **kwargs): |
|
if type(args[0]) is types.StringType: |
group = OptionGroup(self, *args, **kwargs) |
elif len(args) == 1 and not kwargs: |
group = args[0] |
if not isinstance(group, OptionGroup): |
raise TypeError, "not an OptionGroup instance: %r" % group |
if group.parser is not self: |
raise ValueError, "invalid OptionGroup (wrong parser)" |
else: |
raise TypeError, "invalid arguments" |
|
self.option_groups.append(group) |
return group | |
def add_options(self, option_list):
*no docstring available*
arguments:
- self: <UNKNOWN>
- option_list: <UNKNOWN>
return value:
<UNKNOWN>
def check_values(self, values, args):
check_values(values : Values, args : [string])
-> (values : Values, args : [string])
Check that the supplied option values and leftover arguments are
valid. Returns the option values and leftover arguments
(possibly adjusted, possibly completely new -- whatever you
like). Default implementation just returns the passed-in
values; subclasses may override as desired.
arguments:
return value:
<Tuple>
source: compat/optparse.py
1285 |
1286 |
1287 |
1288 |
1289 |
1290 |
1291 |
1292 |
1293 |
1294 |
1295 |
1296 | |
def check_values(self, values, args): |
""" |
check_values(values : Values, args : [string]) |
-> (values : Values, args : [string]) |
|
Check that the supplied option values and leftover arguments are |
valid. Returns the option values and leftover arguments |
(possibly adjusted, possibly completely new -- whatever you |
like). Default implementation just returns the passed-in |
values; subclasses may override as desired. |
""" |
return (values, args) | |
def disable_interspersed_args(self):
*no docstring available*
arguments:
return value:
<UNKNOWN>
def enable_interspersed_args(self):
*no docstring available*
arguments:
return value:
<UNKNOWN>
def error(self, msg):
error(msg : string)
Print a usage message incorporating 'msg' to stderr and exit.
If you override this in a subclass, it should not return -- it
should either exit or raise an exception.
arguments:
- self: <UNKNOWN>
- msg: <UNKNOWN>
return value:
<UNKNOWN>
source: compat/optparse.py
1454 |
1455 |
1456 |
1457 |
1458 |
1459 |
1460 |
1461 |
1462 | |
def error(self, msg): |
"""error(msg : string) |
|
Print a usage message incorporating 'msg' to stderr and exit. |
If you override this in a subclass, it should not return -- it |
should either exit or raise an exception. |
""" |
self.print_usage(sys.stderr) |
self.exit(2, "%s: error: %s\n" % (self.get_prog_name(), msg)) | |
def exit(self, status=0, msg=None):
*no docstring available*
arguments:
- self: <UNKNOWN>
- status: <UNKNOWN>
- msg: <UNKNOWN>
return value:
<UNKNOWN>
def expand_prog_name(self, s):
*no docstring available*
arguments:
- self: <UNKNOWN>
- s: <UNKNOWN>
return value:
<UNKNOWN>
def format_description(self, formatter):
def format_help(self, formatter=None):
def format_option_help(self, formatter=None):
def get_default_values(self):
*no docstring available*
source: compat/optparse.py
1197 |
1198 |
1199 |
1200 |
1201 |
1202 |
1203 |
1204 |
1205 |
1206 |
1207 |
1208 |
1209 | |
def get_default_values(self): |
if not self.process_default_values: |
|
return Values(self.defaults) |
|
defaults = self.defaults.copy() |
for option in self._get_all_options(): |
default = defaults.get(option.dest) |
if isinstance(default, basestring): |
opt_str = option.get_opt_string() |
defaults[option.dest] = option.check_value(opt_str, default) |
|
return Values(defaults) | |
def get_description(self):
*no docstring available*
arguments:
return value:
<UNKNOWN>
def get_option(self, opt_str):
*no docstring available*
arguments:
- self: <UNKNOWN>
- opt_str: <UNKNOWN>
return value:
<UNKNOWN>
def get_option_group(self, opt_str):
*no docstring available*
arguments:
- self: <UNKNOWN>
- opt_str: <UNKNOWN>
return value:
<UNKNOWN>
source: compat/optparse.py
1230 |
1231 |
1232 |
1233 |
1234 |
1235 | |
def get_option_group(self, opt_str): |
option = (self._short_opt.get(opt_str) or |
self._long_opt.get(opt_str)) |
if option and option.container is not self: |
return option.container |
return None | |
def get_prog_name(self):
*no docstring available*
arguments:
return value:
<UNKNOWN>
source: compat/optparse.py
|
def get_prog_name(self): |
if self.prog is None: |
return os.path.basename(sys.argv[0]) |
else: |
return self.prog | |
def get_usage(self):
*no docstring available*
arguments:
return value:
<UNKNOWN>
source: compat/optparse.py
1464 |
1465 |
1466 |
1467 |
1468 |
1469 | |
def get_usage(self): |
if self.usage: |
return self.formatter.format_usage( |
self.expand_prog_name(self.usage)) |
else: |
return "" | |
def get_version(self):
*no docstring available*
arguments:
return value:
<UNKNOWN>
def has_option(self, opt_str):
*no docstring available*
arguments:
- self: <UNKNOWN>
- opt_str: <UNKNOWN>
return value:
<UNKNOWN>
source: compat/optparse.py
|
def has_option(self, opt_str): |
return (self._short_opt.has_key(opt_str) or |
self._long_opt.has_key(opt_str)) | |
def parse_args(self, args=None, values=None):
parse_args(args : [string] = sys.argv[1:],
values : Values = None)
-> (values : Values, args : [string])
Parse the command-line options found in 'args' (default:
sys.argv[1:]). Any errors result in a call to 'error()', which
by default prints the usage message to stderr and calls
sys.exit() with an error message. On success returns a pair
(values, args) where 'values' is an Values instance (with all
your option values) and 'args' is the list of arguments left
over after parsing options.
arguments:
return value:
<Tuple>
source: compat/optparse.py
1246 |
1247 |
1248 |
1249 |
1250 |
1251 |
1252 |
1253 |
1254 |
1255 |
1256 |
1257 |
1258 |
1259 |
1260 |
1261 |
1262 |
1263 |
1264 |
1265 |
1266 |
1267 |
1268 |
1269 |
1270 |
1271 |
1272 |
1273 |
1274 |
1275 |
1276 |
1277 |
1278 |
1279 |
1280 |
1281 |
1282 |
1283 | |
def parse_args(self, args=None, values=None): |
""" |
parse_args(args : [string] = sys.argv[1:], |
values : Values = None) |
-> (values : Values, args : [string]) |
|
Parse the command-line options found in 'args' (default: |
sys.argv[1:]). Any errors result in a call to 'error()', which |
by default prints the usage message to stderr and calls |
sys.exit() with an error message. On success returns a pair |
(values, args) where 'values' is an Values instance (with all |
your option values) and 'args' is the list of arguments left |
over after parsing options. |
""" |
rargs = self._get_args(args) |
if values is None: |
values = self.get_default_values() |
|
|
|
|
|
|
|
|
|
|
self.rargs = rargs |
self.largs = largs = [] |
self.values = values |
|
try: |
stop = self._process_args(largs, rargs, values) |
except (BadOptionError, OptionValueError), err: |
self.error(err.msg) |
|
args = largs + rargs |
return self.check_values(values, args) | |
def print_help(self, file=None):
print_help(file : file = stdout)
Print an extended help message, listing all options and any
help text provided with them, to 'file' (default stdout).
arguments:
- self: <UNKNOWN>
- file: <UNKNOWN>
return value:
<UNKNOWN>
source: compat/optparse.py
1528 |
1529 |
1530 |
1531 |
1532 |
1533 |
1534 |
1535 |
1536 | |
def print_help(self, file=None): |
"""print_help(file : file = stdout) |
|
Print an extended help message, listing all options and any |
help text provided with them, to 'file' (default stdout). |
""" |
if file is None: |
file = sys.stdout |
file.write(self.format_help()) | |
def print_usage(self, file=None):
print_usage(file : file = stdout)
Print the usage message for the current program (self.usage) to
'file' (default stdout). Any occurence of the string "%prog" in
self.usage is replaced with the name of the current program
(basename of sys.argv[0]). Does nothing if self.usage is empty
or not defined.
arguments:
- self: <UNKNOWN>
- file: <UNKNOWN>
return value:
<UNKNOWN>
source: compat/optparse.py
1471 |
1472 |
1473 |
1474 |
1475 |
1476 |
1477 |
1478 |
1479 |
1480 |
1481 | |
def print_usage(self, file=None): |
"""print_usage(file : file = stdout) |
|
Print the usage message for the current program (self.usage) to |
'file' (default stdout). Any occurence of the string "%prog" in |
self.usage is replaced with the name of the current program |
(basename of sys.argv[0]). Does nothing if self.usage is empty |
or not defined. |
""" |
if self.usage: |
print >>file, self.get_usage() | |
def print_version(self, file=None):
print_version(file : file = stdout)
Print the version message for this program (self.version) to
'file' (default stdout). As with print_usage(), any occurence
of "%prog" in self.version is replaced by the current program's
name. Does nothing if self.version is empty or undefined.
arguments:
- self: <UNKNOWN>
- file: <UNKNOWN>
return value:
<UNKNOWN>
source: compat/optparse.py
1489 |
1490 |
1491 |
1492 |
1493 |
1494 |
1495 |
1496 |
1497 |
1498 | |
def print_version(self, file=None): |
"""print_version(file : file = stdout) |
|
Print the version message for this program (self.version) to |
'file' (default stdout). As with print_usage(), any occurence |
of "%prog" in self.version is replaced by the current program's |
name. Does nothing if self.version is empty or undefined. |
""" |
if self.version: |
print >>file, self.get_version() | |
def remove_option(self, opt_str):
*no docstring available*
arguments:
- self: <UNKNOWN>
- opt_str: <UNKNOWN>
return value:
<UNKNOWN>
source: compat/optparse.py
970 |
971 |
972 |
973 |
974 |
975 |
976 |
977 |
978 |
979 |
980 |
981 | |
def remove_option(self, opt_str): |
option = self._short_opt.get(opt_str) |
if option is None: |
option = self._long_opt.get(opt_str) |
if option is None: |
raise ValueError("no such option %r" % opt_str) |
|
for opt in option._short_opts: |
del self._short_opt[opt] |
for opt in option._long_opts: |
del self._long_opt[opt] |
option.container.option_list.remove(option) | |
def set_conflict_handler(self, handler):
*no docstring available*
arguments:
return value:
<None>
source: compat/optparse.py
|
def set_conflict_handler(self, handler): |
if handler not in ("error", "resolve"): |
raise ValueError, "invalid conflict_resolution value %r" % handler |
self.conflict_handler = handler | |
def set_default(self, dest, value):
*no docstring available*
arguments:
- self: <UNKNOWN>
- dest: <UNKNOWN>
- value: <UNKNOWN>
return value:
<UNKNOWN>
def set_defaults(self, **kwargs):
*no docstring available*
arguments:
return value:
<UNKNOWN>
def set_description(self, description):
*no docstring available*
arguments:
return value:
<None>
def set_process_default_values(self, process):
*no docstring available*
arguments:
- self: <UNKNOWN>
- process: <UNKNOWN>
return value:
<UNKNOWN>
def set_usage(self, usage):
*no docstring available*
arguments:
return value:
<None>
source: compat/optparse.py
1165 |
1166 |
1167 |
1168 |
1169 |
1170 |
1171 |
1172 |
1173 |
1174 | |
def set_usage(self, usage): |
if usage is None: |
self.usage = _("%prog [options]") |
elif usage is SUPPRESS_USAGE: |
self.usage = None |
|
elif usage.startswith("usage:" + " "): |
self.usage = usage[7:] |
else: |
self.usage = usage | |