Passing parameters at the command line (New Python User)
Ben Finney
bignose+hates-spam at benfinney.id.au
Mon Sep 24 05:01:53 EDT 2007
cjt22 at bath.ac.uk writes:
> I have never used Python to pass in arguments at the command line so
> any help would be much appreciated.
Your 'main()' approach is good. I'd rather have the function require
an 'argv' parameter, and have the default set only in the 'if __name__
== "__main__":' block, since that fits my ideas better about the
defaults.
def main(argv):
parse_commandline(argv)
do_cool_stuff()
if __name__ == "__main__":
from sys import argv
main(argv)
I also tend to catch SystemExit in the function, so the exit code can
be returned instead of raised; but that's outside the scope of this
thread.
For anything more advanced than unconditionally grabbing arguments in
sequence from the command line, you should investigate the 'optparse'
module <URL:http://docs.python.org/lib/module-optparse> from the
standard library.
--
\ "Holy knit one purl two, Batman!" -- Robin |
`\ |
_o__) |
Ben Finney
More information about the Python-list
mailing list