Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2492fcf

Browse files
committed
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.
1 parent be733ee commit 2492fcf

1 file changed

Lines changed: 48 additions & 49 deletions

File tree

Lib/optparse.py

Lines changed: 48 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,25 @@
22
33
By Greg Ward <[email protected]>
44
5-
Originally distributed as Optik.
5+
Originally distributed as Optik; see http://optik.sourceforge.net/ .
66
7-
See http://optik.sourceforge.net/
7+
If you have problems with this module, please do not files bugs,
8+
patches, or feature requests with Python; instead, use Optik's
9+
SourceForge project page:
10+
http://sourceforge.net/projects/optik
11+
12+
For support, use the [email protected] mailing list
13+
(http://lists.sourceforge.net/lists/listinfo/optik-users).
814
"""
915

16+
# Python developers: please do not make changes to this file, since
17+
# it is automatically generated from the Optik source code.
18+
19+
20+
__version__ = "1.4.1"
21+
1022
__copyright__ = """
11-
Copyright (c) 2001-2002 Gregory P. Ward. All rights reserved.
23+
Copyright (c) 2001-2003 Gregory P. Ward. All rights reserved.
1224
1325
Redistribution and use in source and binary forms, with or without
1426
modification, are permitted provided that the following conditions are
@@ -42,15 +54,14 @@
4254
import types
4355
import textwrap
4456

45-
__version__ = "1.4+"
46-
4757
class OptParseError (Exception):
4858
def __init__ (self, msg):
4959
self.msg = msg
5060

5161
def __str__ (self):
5262
return self.msg
5363

64+
5465
class OptionError (OptParseError):
5566
"""
5667
Raised if an Option instance is created with invalid or
@@ -82,6 +93,8 @@ class BadOptionError (OptParseError):
8293
"""
8394
Raised if an invalid or ambiguous option is seen on the command-line.
8495
"""
96+
97+
8598
class HelpFormatter:
8699

87100
"""
@@ -118,10 +131,7 @@ def __init__ (self,
118131
self.current_indent = 0
119132
self.level = 0
120133
self.help_width = width - max_help_position
121-
if short_first:
122-
self.format_option_strings = self.format_option_strings_short_first
123-
else:
124-
self.format_option_strings = self.format_option_strings_long_first
134+
self.short_first = short_first
125135

126136
def indent (self):
127137
self.current_indent += self.indent_increment
@@ -198,38 +208,20 @@ def store_option_strings (self, parser):
198208

199209
def format_option_strings (self, option):
200210
"""Return a comma-separated list of option strings & metavariables."""
201-
raise NotImplementedError(
202-
"abstract method: use format_option_strings_short_first or "
203-
"format_option_strings_long_first instead.")
204-
205-
def format_option_strings_short_first (self, option):
206-
opts = [] # list of "-a" or "--foo=FILE" strings
207-
takes_value = option.takes_value()
208-
if takes_value:
211+
if option.takes_value():
209212
metavar = option.metavar or option.dest.upper()
210-
for sopt in option._short_opts:
211-
opts.append(sopt + metavar)
212-
for lopt in option._long_opts:
213-
opts.append(lopt + "=" + metavar)
213+
short_opts = [sopt + metavar for sopt in option._short_opts]
214+
long_opts = [lopt + "=" + metavar for lopt in option._long_opts]
214215
else:
215-
for opt in option._short_opts + option._long_opts:
216-
opts.append(opt)
217-
return ", ".join(opts)
216+
short_opts = option._short_opts
217+
long_opts = option._long_opts
218218

219-
def format_option_strings_long_first (self, option):
220-
opts = [] # list of "-a" or "--foo=FILE" strings
221-
takes_value = option.takes_value()
222-
if takes_value:
223-
metavar = option.metavar or option.dest.upper()
224-
for lopt in option._long_opts:
225-
opts.append(lopt + "=" + metavar)
226-
for sopt in option._short_opts:
227-
opts.append(sopt + metavar)
219+
if self.short_first:
220+
opts = short_opts + long_opts
228221
else:
229-
for opt in option._long_opts + option._short_opts:
230-
opts.append(opt)
231-
return ", ".join(opts)
222+
opts = long_opts + short_opts
232223

224+
return ", ".join(opts)
233225

234226
class IndentedHelpFormatter (HelpFormatter):
235227
"""Format help with indented section bodies.
@@ -267,6 +259,8 @@ def format_usage (self, usage):
267259

268260
def format_heading (self, heading):
269261
return "%s\n%s\n" % (heading, "=-"[self.level] * len(heading))
262+
263+
270264
_builtin_cvt = { "int" : (int, "integer"),
271265
"long" : (long, "long integer"),
272266
"float" : (float, "floating-point"),
@@ -400,7 +394,10 @@ class Option:
400394
# -- Constructor/initialization methods ----------------------------
401395

402396
def __init__ (self, *opts, **attrs):
403-
# Set _short_opts, _long_opts attrs from 'opts' tuple
397+
# Set _short_opts, _long_opts attrs from 'opts' tuple.
398+
# Have to be set now, in case no option strings are supplied.
399+
self._short_opts = []
400+
self._long_opts = []
404401
opts = self._check_opt_strings(opts)
405402
self._set_opt_strings(opts)
406403

@@ -421,13 +418,10 @@ def _check_opt_strings (self, opts):
421418
# could be None.
422419
opts = filter(None, opts)
423420
if not opts:
424-
raise OptionError("at least one option string must be supplied",
425-
self)
421+
raise TypeError("at least one option string must be supplied")
426422
return opts
427423

428424
def _set_opt_strings (self, opts):
429-
self._short_opts = []
430-
self._long_opts = []
431425
for opt in opts:
432426
if len(opt) < 2:
433427
raise OptionError(
@@ -569,10 +563,7 @@ def _check_callback (self):
569563
# -- Miscellaneous methods -----------------------------------------
570564

571565
def __str__ (self):
572-
if self._short_opts or self._long_opts:
573-
return "/".join(self._short_opts + self._long_opts)
574-
else:
575-
raise RuntimeError, "short_opts and long_opts both empty!"
566+
return "/".join(self._short_opts + self._long_opts)
576567

577568
def takes_value (self):
578569
return self.type is not None
@@ -609,9 +600,9 @@ def take_action (self, action, dest, opt, value, values, parser):
609600
elif action == "store_const":
610601
setattr(values, dest, self.const)
611602
elif action == "store_true":
612-
setattr(values, dest, 1)
603+
setattr(values, dest, True)
613604
elif action == "store_false":
614-
setattr(values, dest, 0)
605+
setattr(values, dest, False)
615606
elif action == "append":
616607
values.ensure_value(dest, []).append(value)
617608
elif action == "count":
@@ -632,6 +623,8 @@ def take_action (self, action, dest, opt, value, values, parser):
632623
return 1
633624

634625
# class Option
626+
627+
635628
def get_prog_name ():
636629
return os.path.basename(sys.argv[0])
637630

@@ -922,7 +915,10 @@ class OptionParser (OptionContainer):
922915
usage : string
923916
a usage string for your program. Before it is displayed
924917
to the user, "%prog" will be expanded to the name of
925-
your program (os.path.basename(sys.argv[0])).
918+
your program (self.prog or os.path.basename(sys.argv[0])).
919+
prog : string
920+
the name of the current program (to override
921+
os.path.basename(sys.argv[0])).
926922
927923
allow_interspersed_args : boolean = true
928924
if true, positional arguments may be interspersed with options.
@@ -967,10 +963,12 @@ def __init__ (self,
967963
conflict_handler="error",
968964
description=None,
969965
formatter=None,
970-
add_help_option=1):
966+
add_help_option=1,
967+
prog=None):
971968
OptionContainer.__init__(
972969
self, option_class, conflict_handler, description)
973970
self.set_usage(usage)
971+
self.prog = prog
974972
self.version = version
975973
self.allow_interspersed_args = 1
976974
if formatter is None:
@@ -1382,3 +1380,4 @@ def _match_abbrev (s, wordmap):
13821380
# which will become a factory function when there are many Option
13831381
# classes.
13841382
make_option = Option
1383+

0 commit comments

Comments
 (0)