sys module - argv, but no argc ??
Peter Hansen
peter at engcorp.com
Sun Aug 4 00:37:42 EDT 2002
TuxTrax wrote:
>
> On Sat, 03 Aug 2002 08:57:43 -0400, Peter Hansen Wrote in
> > You're probably right that it's not the most elegant way. The
> > most elegant way might be to use one of the existing modules,
> > perhaps getopt from the standard library, or Optik at
> > http://optik.sourceforge.net, and to avoid reinventing the wheel.
[...]
> It does seem to me that if I am parsing the command line for two
> options only, this snippet of code would be the way to go rather than
> import a library to do it. I am sure that if I was doing any serious
> parsing, I might want to use a library designed to do it. What do you think?
I think this is about as simple, and definitely more maintainable,
especially since if you have two options now, you'll have five later.
Also less to debug:
import getopt
import sys
opts, args = getopt.getopt(sys.argv[1:], 'G:S:')
server = newsgroup = 'not given'
for opt, val in opts:
if opt == '-G':
newsgroup = val
elif opt == '-S':
server = val
print 'Server: %s, newsgroup: %s' % (newsgroup, server)
print 'Other args: %s' % (args,)
What do you think? :-) (I think it's generally worth learning
to use the library modules, at least so you know when it is _really_
easier to do roll your own.)
-Peter
More information about the Python-list
mailing list