Newbie Getopts Question

Andrew N. McGuire amcguire at coastalnet.com
Tue Dec 21 09:04:24 EST 1999


Hey all,

    Thank you Justin et al.  It is good to hear from you all, as you all
provided
some good insight, hopefully I will have many good years of hacking
with python in the future.. But I am taking baby steps right now...
Needless to say once more that all the help is appreciated.  Oops
I guess it wasnt needless cause I said it ;^).

Tankuverimutch,

Andrew

Justin Sheehy wrote:

> "Andrew N. McGuire" <amcguire at coastalnet.com> writes:
>
> > this seems like a somewhat ineffecient way of handling options.
>
> There are certainly some things you can do to make your code cleaner
> here.  However, you are not alone in your judgement of the standard
> getopt module.  It works quite well, but is not as feature-rich as
> many people would like.
>
> Those people don't seem to agree on exactly how it should be better,
> though.  Thus, several people have written their own getopt modules.
>
> I've seen one or two private in-house implementations, and a number of
> people have posted their own getopt replacements here.  I have also
> done this, though to suit a specific need: I wrote a getopt-like
> module that was designed to mimic the functionality of the argument
> parsing in the suites of user commands for manipulating the Andrew
> File System.  If anyone wants such a beast, feel free to ask me for it.
>
> With that diversion out of the way, I'll see if I can offer any useful
> suggestions about the code here...
>
> > arglist = sys.argv[1:]
>
> This isn't necessary.  It could even be confusing, since you use that
> name for something else afterward.  You can omit this and just
> replace the following line:
>
> >     optlist, arglist = getopt.getopt(arglist, 'f:n:i:o:c:')
>
> With:
>
>       optlist, arglist = getopt.getopt(sys.argv[1:], 'f:n:i:o:c:')
>
> > list = []
> >
> > for opt in optlist:
> >     list.append(opt[0])
>
> Just because of all of the bad press map and lambda have had lately ;->,
> I'll mention that this whole snippet can be done as follows, among
> other ways:
>
> list = map(lambda x: x[0], optlist)
>
> > for opt in optlist:
>
> I recommend something more like:
>
>   for opt, value in optlist:
>
> Then you can replace all following uses of `opt[0]' and `opt[1]' with
> `opt' and `value', respectively.  Tuple unpacking in this sort of
> context is a wonderful feature that I use frequently.
>
> I'm sure that as you continue to familiarize yourself with Python, you
> will find many more ways to improve your code.
>
> -Justin
>
>




More information about the Python-list mailing list