command-line parser

Trent Mick trentm at activestate.com
Sun Jun 18 20:11:16 EDT 2000


I didn't really look over all of this (no time!) but some more comments:

help() should probably be __str__. When I wrote something like this before I
made sure that the help() output kicked out a nicely formated usage line such
as:

  -v, --verbose       give verbose output

With code something like this:

	def help():
		tabs = (2, 20)
		sys.stdout.write('%s-%s, --%s%s%s' %\
			(' '*tabs[0], self.short_form, self.long_form, ' '*tabs[1],
			self.description))
	


On Sun, Jun 18, 2000 at 11:15:18PM +0000, Ben Wolfson wrote:
> [comments excised].
> 
> Some advice taken, and it's a lot more readable, or seems so to me.
> An instance of the Option class can take a callback that gets called on its
> value, so if a particular option's value needs to be an int, say, you could
> instantiate it with
> 
> Option(<short>, <long>, 1, val_cb = int)
> 
> If the callback raises an exception, the option's help() function prints
> its description
> 
> # arglist.py
> class Error(Exception):
>     pass
> 
> class Option:
>     def __init__(self, short, long=None, has_value=None, default_value =\
>                  None, val_cb = None, description = None):
>         self.short = short
>         self.long = long
>         self.has_val = has_value
>         self.val_cb = val_cb
>         self.value = default_value or None
>         self.description = description

Don't use 'short' and 'long'. These typically represent types. Confusing for
C programmers, and 'long' is a Python built-in. As well in function
declaration Guido suggests not having a space btwn then name and its default
value. This is all just nitpicking though. :)

>         
>     def get_keys(self):
>         return self.short, self.long
>     
>     def handle(self, val):
>         if self.has_val:
>             if val:
>                 self.value = val
>             elif self.value is None:
>                     raise Error, 'Option %s requires a value' % opt
>         else:
>             self.value = 1
>         if self.val_cb:
>             try:
>                 self.value = self.val_cb(self.value)
>             except:
>                 self.help()
>         del self.short, self.long, self.val_cb, self.has_val

I think it would be better *not* to catch the conversion error. Or if you do
then:

	raise Error, "conversion of option value %s by %s failed" %\
		(`self.value`, `self.val_cb`)

> 
>     def help(self):#rather simpleminded, yes?
>         if self.description:
>             print
>             print self.description
>    

See comment above


>         
> class Arguments:
>     def __init__(self, *opts):
>         self.__dict__['nonopts'] = []
>         self.__dict__['dict'] = {}

Whoa! Do you need to mess with self.__dict__ directly? Is this not the same
as self.nonopts = [], etc.?

>         for opt in opts:
>             short, long = opt.get_keys()
>             if short and long:
>                 self.dict[short] = self.dict[long] = opt
>             elif short:
>                 self.dict[short] = opt
>             else:
>                 self.dict[long] = opt
>

Use something like 'opt_handlers' or whatever rather than 'dict'.

>     def process(self, options):
>         for opt in options:
>             if opt[0] <> '-':
>                 self.nonopts.append(opt)
>                 options.remove(opt)
>         for opt,val in map(splitstrip, options):
>             self.dict[opt].handle(val)
> 
>     def __getattr__(self, key):
>         if not self.dict.has_key(key):
>             raise Error, 'No option %s' % key
>         return self.dict[key].value
> 
>     def get_nonopts(self):
>         return self.nonopts
>             
> 
> def splitstrip(opt):
>     return optsplit(optstrip(opt))
> 
> def optstrip(opt):
>     while opt[0] == '-':
>         opt = opt[1:]
>     return opt
> 
> def optsplit(opt):
>     if ':' in opt:
>         return opt.split(':',2)
>     elif '=' in opt:
>         return opt.split('=',2)
>     return opt, None
> -- 
> http://www.python.org/mailman/listinfo/python-list


Trent

-- 
Trent Mick
trentm at activestate.com




More information about the Python-list mailing list