a simple helper class for parsing command line options

Carel Fellinger cfelling at iae.nl
Mon Nov 8 17:06:12 EST 1999


In article <s1sj8n.11uoa9u at dev.huh.harvard.edu> you wrote:
> I threw this together to make parsing command line options and arguments
> a little easier than just using getopt directly.  Feedback welcome.

Sorry for reacting so late, but it took some time to cough up a responce.
(or actually to build my own option state machine)
Besides being publically accused of Perlish behaviour
made me hesitate to react:), but now that no one else has...

A bunch of nice ideas: inspired me to adjust it, rewrite it and
eventually write my own:), But what would you expect with something
so close to the look of ones program and moreover from a Dutch man.

> class OptionFactory:

this triggered me to see option parsing as a state machine

>         self.__options = [('help','h','help','','print this help message')]

I personally have some problems reading all those quotes, so I would
prefer something more friendly like:

    self.__options = [('-h, --help : print this help message')]

>         self.__dict = {}

a seperate dict for the state, I keep that:)
(though I would call it __state, less likely to be confused with __dict__)

>     def __getitem__(self, item):
>         return self.__dict[item]

and these, ofcourse

>     def add_options(self, options):
>         """
>         label = identifier for the option, used to retrieve option value

my problem here is, why should I specify an extra identifier, I could as
well use the short and or long flags themselves and enhance __getitem__
to deal with that.

>         value = descriptive string of the value the option requires

it took a while before I stopt forgetting that you refer here to the
optional argument to the option flags and not to the actual value
specified on the command line. Well that just shows how silly I am.

>     def add_argdesc(self, argdesc):
>         """ add a description of the expected arguments """

Great. I would add a similar one to allow for the replacement of the
default [OPTIONS...] string as well as it is hard to construct a
meaningfull option list automagically.

>     def usage(self):

a lot of code, necessary to join the flags part of the description.
In my previously proposed variant, this flags part could be used almost
without any massaging. And what about multiline descriptions?

>     def parse(self, argv):
> 	""" pass in sys.argv, not sys.arv[1:] """

or define a default value like:

    def parse(self, argv=argv[1:]):

>         self.name = argv[0]

definitatively

>         try:
>             optlist, args = getopt(argv[1:], self.__shortstring, self.__longlist)
>             for opt in optlist:
>                 self.__process_opt(opt)
>             self.args = args
>         except error, x:
>             msg = 'Error parsing options:  %s' % x
>             sys.stderr.write('%s\n' % msg)
>             sys.stderr.write('\n%s\n' % self.usage())
>             sys.exit(1)

nice to catch getopt.error here, though I myself would prefer a short
responce to stderr refering to the use of the --help option. Just hate
to have to scroll back just to see what I was doing:)


For those interested: I redid the above, treating option parsing more
like transitions in a state machine. Using that module one can say
things like:

    options = OptionState(test=0, verbosity=0)
    add = options.add_option
    add('-t, --test : just testing', test=1)
    add('--version : show the version and exit', options._version)
    add('--help : show this help message', options._help)
    add('-v : increment the verbosity', options._inc, 'verbosity')
    add('-V : increase verbosity with 5', options._inc, 'verbosity', 5)
    add('-D : set verbosity to 11', options._int, 'verbosity', 11)
    add('-x : ', x=1) # secret option, not displayed in help message!
    ...
    if options.verbosity > 8: pass
    if options['test']: pass
    if options.x or options.has_option('x'): pass
    
One can also keep a list of values of repeated option flags, or easily
enhance this state machine with your own special purpose transitions.

-- 
groetjes, carel
-- 
groetjes, carel




More information about the Python-list mailing list