command line arguments martin.witte at
Wed Aug 31 20:13:24 CEST 2005

You also could opt for the OptionParser in optparse, it is quiet
powerful, and you can keep your code clean. Your requirements would
translate to something like:
py>#!/usr/bin/env python
py>"""show OptionParser
py>from optparse import OptionParser
py>def main():
py>    parser = OptionParser(usage = __doc__, version = '3.1415926')
py>    parser.add_option("-n", "--name", dest="name", action="store",
py>   			help="enter a name")
py>    parser.add_option("-u", "--url", action="store", dest="url",
help = "enter an url")
py>    (options, args) = parser.parse_args()
py>    if not and not options.url:
py>	    parser.error('specify both name and url')
py>    print
py>    print options.url
py>if __name__ == "__main__":
py>    main()

when called it will print things like:
martin at ubuntu:~ $ ./ -u -n python
{'url': '', 'name': 'python'} []
martin at ubuntu:~ $ ./ -u -n python
martin at ubuntu:~ $ ./ -h
usage: show OptionParser

  --version             show program's version number and exit
  -h, --help            show this help message and exit
  -n NAME, --name=NAME  enter a name
  -u URL, --url=URL     enter an url
martin at ubuntu:~ $ ./ --version
martin at ubuntu:~ $ ./
usage: show OptionParser error: specify both name and url
martin at ubuntu:~ $

More information about the Python-list mailing list