Kaydet (Commit) 2492fcf3 authored tarafından Greg Ward's avatar Greg Ward

Update to Optik 1.4.1; here are the relevant bits of the change log:

  * Fixed some long-hidden bugs revealed by the new PyUnit-based
    test suite (thanks to Johannes Gijsbers the new test suite,
    improved tests that caught the bugs, and the bug fixes).

  * Make store_true/store_false store True/False rather than 1/0.

Details available in Optik's CVS repository.
üst be733ee7
...@@ -2,13 +2,25 @@ ...@@ -2,13 +2,25 @@
By Greg Ward <gward@python.net> By Greg Ward <gward@python.net>
Originally distributed as Optik. Originally distributed as Optik; see http://optik.sourceforge.net/ .
See http://optik.sourceforge.net/ If you have problems with this module, please do not files bugs,
patches, or feature requests with Python; instead, use Optik's
SourceForge project page:
http://sourceforge.net/projects/optik
For support, use the optik-users@lists.sourceforge.net mailing list
(http://lists.sourceforge.net/lists/listinfo/optik-users).
""" """
# Python developers: please do not make changes to this file, since
# it is automatically generated from the Optik source code.
__version__ = "1.4.1"
__copyright__ = """ __copyright__ = """
Copyright (c) 2001-2002 Gregory P. Ward. All rights reserved. Copyright (c) 2001-2003 Gregory P. Ward. All rights reserved.
Redistribution and use in source and binary forms, with or without Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are modification, are permitted provided that the following conditions are
...@@ -42,8 +54,6 @@ import sys, os ...@@ -42,8 +54,6 @@ import sys, os
import types import types
import textwrap import textwrap
__version__ = "1.4+"
class OptParseError (Exception): class OptParseError (Exception):
def __init__ (self, msg): def __init__ (self, msg):
self.msg = msg self.msg = msg
...@@ -51,6 +61,7 @@ class OptParseError (Exception): ...@@ -51,6 +61,7 @@ class OptParseError (Exception):
def __str__ (self): def __str__ (self):
return self.msg return self.msg
class OptionError (OptParseError): class OptionError (OptParseError):
""" """
Raised if an Option instance is created with invalid or Raised if an Option instance is created with invalid or
...@@ -82,6 +93,8 @@ class BadOptionError (OptParseError): ...@@ -82,6 +93,8 @@ class BadOptionError (OptParseError):
""" """
Raised if an invalid or ambiguous option is seen on the command-line. Raised if an invalid or ambiguous option is seen on the command-line.
""" """
class HelpFormatter: class HelpFormatter:
""" """
...@@ -118,10 +131,7 @@ class HelpFormatter: ...@@ -118,10 +131,7 @@ class HelpFormatter:
self.current_indent = 0 self.current_indent = 0
self.level = 0 self.level = 0
self.help_width = width - max_help_position self.help_width = width - max_help_position
if short_first: self.short_first = short_first
self.format_option_strings = self.format_option_strings_short_first
else:
self.format_option_strings = self.format_option_strings_long_first
def indent (self): def indent (self):
self.current_indent += self.indent_increment self.current_indent += self.indent_increment
...@@ -198,38 +208,20 @@ class HelpFormatter: ...@@ -198,38 +208,20 @@ class HelpFormatter:
def format_option_strings (self, option): def format_option_strings (self, option):
"""Return a comma-separated list of option strings & metavariables.""" """Return a comma-separated list of option strings & metavariables."""
raise NotImplementedError( if option.takes_value():
"abstract method: use format_option_strings_short_first or "
"format_option_strings_long_first instead.")
def format_option_strings_short_first (self, option):
opts = [] # list of "-a" or "--foo=FILE" strings
takes_value = option.takes_value()
if takes_value:
metavar = option.metavar or option.dest.upper() metavar = option.metavar or option.dest.upper()
for sopt in option._short_opts: short_opts = [sopt + metavar for sopt in option._short_opts]
opts.append(sopt + metavar) long_opts = [lopt + "=" + metavar for lopt in option._long_opts]
for lopt in option._long_opts:
opts.append(lopt + "=" + metavar)
else: else:
for opt in option._short_opts + option._long_opts: short_opts = option._short_opts
opts.append(opt) long_opts = option._long_opts
return ", ".join(opts)
def format_option_strings_long_first (self, option): if self.short_first:
opts = [] # list of "-a" or "--foo=FILE" strings opts = short_opts + long_opts
takes_value = option.takes_value()
if takes_value:
metavar = option.metavar or option.dest.upper()
for lopt in option._long_opts:
opts.append(lopt + "=" + metavar)
for sopt in option._short_opts:
opts.append(sopt + metavar)
else: else:
for opt in option._long_opts + option._short_opts: opts = long_opts + short_opts
opts.append(opt)
return ", ".join(opts)
return ", ".join(opts)
class IndentedHelpFormatter (HelpFormatter): class IndentedHelpFormatter (HelpFormatter):
"""Format help with indented section bodies. """Format help with indented section bodies.
...@@ -267,6 +259,8 @@ class TitledHelpFormatter (HelpFormatter): ...@@ -267,6 +259,8 @@ class TitledHelpFormatter (HelpFormatter):
def format_heading (self, heading): def format_heading (self, heading):
return "%s\n%s\n" % (heading, "=-"[self.level] * len(heading)) return "%s\n%s\n" % (heading, "=-"[self.level] * len(heading))
_builtin_cvt = { "int" : (int, "integer"), _builtin_cvt = { "int" : (int, "integer"),
"long" : (long, "long integer"), "long" : (long, "long integer"),
"float" : (float, "floating-point"), "float" : (float, "floating-point"),
...@@ -400,7 +394,10 @@ class Option: ...@@ -400,7 +394,10 @@ class Option:
# -- Constructor/initialization methods ---------------------------- # -- Constructor/initialization methods ----------------------------
def __init__ (self, *opts, **attrs): def __init__ (self, *opts, **attrs):
# Set _short_opts, _long_opts attrs from 'opts' tuple # Set _short_opts, _long_opts attrs from 'opts' tuple.
# Have to be set now, in case no option strings are supplied.
self._short_opts = []
self._long_opts = []
opts = self._check_opt_strings(opts) opts = self._check_opt_strings(opts)
self._set_opt_strings(opts) self._set_opt_strings(opts)
...@@ -421,13 +418,10 @@ class Option: ...@@ -421,13 +418,10 @@ class Option:
# could be None. # could be None.
opts = filter(None, opts) opts = filter(None, opts)
if not opts: if not opts:
raise OptionError("at least one option string must be supplied", raise TypeError("at least one option string must be supplied")
self)
return opts return opts
def _set_opt_strings (self, opts): def _set_opt_strings (self, opts):
self._short_opts = []
self._long_opts = []
for opt in opts: for opt in opts:
if len(opt) < 2: if len(opt) < 2:
raise OptionError( raise OptionError(
...@@ -569,10 +563,7 @@ class Option: ...@@ -569,10 +563,7 @@ class Option:
# -- Miscellaneous methods ----------------------------------------- # -- Miscellaneous methods -----------------------------------------
def __str__ (self): def __str__ (self):
if self._short_opts or self._long_opts: return "/".join(self._short_opts + self._long_opts)
return "/".join(self._short_opts + self._long_opts)
else:
raise RuntimeError, "short_opts and long_opts both empty!"
def takes_value (self): def takes_value (self):
return self.type is not None return self.type is not None
...@@ -609,9 +600,9 @@ class Option: ...@@ -609,9 +600,9 @@ class Option:
elif action == "store_const": elif action == "store_const":
setattr(values, dest, self.const) setattr(values, dest, self.const)
elif action == "store_true": elif action == "store_true":
setattr(values, dest, 1) setattr(values, dest, True)
elif action == "store_false": elif action == "store_false":
setattr(values, dest, 0) setattr(values, dest, False)
elif action == "append": elif action == "append":
values.ensure_value(dest, []).append(value) values.ensure_value(dest, []).append(value)
elif action == "count": elif action == "count":
...@@ -632,6 +623,8 @@ class Option: ...@@ -632,6 +623,8 @@ class Option:
return 1 return 1
# class Option # class Option
def get_prog_name (): def get_prog_name ():
return os.path.basename(sys.argv[0]) return os.path.basename(sys.argv[0])
...@@ -922,7 +915,10 @@ class OptionParser (OptionContainer): ...@@ -922,7 +915,10 @@ class OptionParser (OptionContainer):
usage : string usage : string
a usage string for your program. Before it is displayed a usage string for your program. Before it is displayed
to the user, "%prog" will be expanded to the name of to the user, "%prog" will be expanded to the name of
your program (os.path.basename(sys.argv[0])). 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])).
allow_interspersed_args : boolean = true allow_interspersed_args : boolean = true
if true, positional arguments may be interspersed with options. if true, positional arguments may be interspersed with options.
...@@ -967,10 +963,12 @@ class OptionParser (OptionContainer): ...@@ -967,10 +963,12 @@ class OptionParser (OptionContainer):
conflict_handler="error", conflict_handler="error",
description=None, description=None,
formatter=None, formatter=None,
add_help_option=1): add_help_option=1,
prog=None):
OptionContainer.__init__( OptionContainer.__init__(
self, option_class, conflict_handler, description) self, option_class, conflict_handler, description)
self.set_usage(usage) self.set_usage(usage)
self.prog = prog
self.version = version self.version = version
self.allow_interspersed_args = 1 self.allow_interspersed_args = 1
if formatter is None: if formatter is None:
...@@ -1382,3 +1380,4 @@ def _match_abbrev (s, wordmap): ...@@ -1382,3 +1380,4 @@ def _match_abbrev (s, wordmap):
# which will become a factory function when there are many Option # which will become a factory function when there are many Option
# classes. # classes.
make_option = Option make_option = Option
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment