22
3344
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
1325Redistribution and use in source and binary forms, with or without
1426modification, are permitted provided that the following conditions are
4254import types
4355import textwrap
4456
45- __version__ = "1.4+"
46-
4757class OptParseError (Exception ):
4858 def __init__ (self , msg ):
4959 self .msg = msg
5060
5161 def __str__ (self ):
5262 return self .msg
5363
64+
5465class 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+
8598class 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
234226class 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+
635628def 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.
13841382make_option = Option
1383+
0 commit comments