Command line options processing in Python

Contents

  1. Module

1. Module

At this point in time, Python has three standard library modules for parsing options:

getopt is the python port of the unix getopt(3) libc utility function. It is pretty basic in function. IMHO, its biggest drawback is that you still have to end up writing a helper function which will describe all the usage documentation in case the user makes and error or asks for help.

optparse has been around for a while and gives a more comprehensive support for option parsing.

argparse is the new kid on the block, which gives even more flexibility than optparse and aims to address some gaps in functionality.

argparse is only available from python 2.7 onwards. If you have an older version of python, you can either download the source or use the easy-install package or the pre-compiled distro packages in Linux. e.g. Ubuntu 10.04 "Lucid Lynx" has python-argparse.

This documentation will only focus on argparse as it is probably going to be the de-facto option processing library in Python going forward.

Python/OptionParsing (last edited 2010-09-08 05:57:19 by SandipBhattacharya)