10.3 命令行参数 Command Line Arguments

Common utility scripts often invoke processing command line arguments. These arguments are stored in the sys module's argv attribute as a list. For instance the following output results from running "python demo.py one two three" at the command line:

通用工具脚本经常调用命令行参数。这些命令行参数以链表形式存储于 sys 模块的 argv 变量。例如在命令行中执行 "python demo.py one two three"后可以得到以下输出结果:

>>> import sys
>>> print sys.argv
['demo.py', 'one', 'two', 'three']

The getopt module processes sys.argv using the conventions of the Unix getopt() function. More powerful and flexible command line processing is provided by the optparse module.

getopt 模块使用 Unix getopt() 函处理 sys.argv。更多的复杂命令行处理由 optparse 模块提供。