my first class: Args
Peter Kleiweg
in.aqua.scribis at nl.invalid
Sun Aug 29 06:49:27 EDT 2004
Scott David Daniels schreef:
> I missed the sys.argv -- here is a sketch of a slightly better rewrite:
>
> > class Args:
> > """..."""
> > def __init__(self,usage='Usage: %(progname)s [opt...] [file...]'):
> > "init, usage string: embed program name as %(progname)s"
> > self.progname = os.path.basename(sys.argv[0])
> > ...
> > self._argv = sys.argv[1:]
> class Args:
> """..."""
> def __init__(self, progname, args,
> usage='Usage: %(progname)s [opt...] [file...]'):
> "usage string: embed program name as %(progname)s"
> self.progname = progname
> self._argv = args
> ...
>
>
> > if __name__ == '__main__':
> > ...
>
> def demo(progname, args):
> a = Args(progname, args,
> 'Usage: %(progname)s [-a value] [-b value] [-c] word [file...]')
> ...
>
> if __name__ == '__main__':
> demo(sys.argv[0], sys.argv[1:])
This goes against the purpose of the class: to take care of as
much of the overhead of script writing as possible. So I can
simply do this:
import args
a = Args()
for line in a:
do_something_with(line)
Or with some extras:
import args
a = Args()
for line in a:
try:
do_something_with(line)
except SomeError:
a.error("something wrong")
In the last example, I would get filename and line number of the
input were the error occured, in the format understood by the
'make' parser of Emacs.
--
Peter Kleiweg L:NL,af,da,de,en,ia,nds,no,sv,(fr,it) S:NL,de,en,(da,ia)
info: http://www.let.rug.nl/~kleiweg/ls.html
More information about the Python-list
mailing list