python基础之 sys.argv[]用法

sys.argv[]是用来获取命令行参数的,sys.argv[0]表示代码本身文件路径,所以参数从1开始.

 

arg[1]表示第一个命令行参数

arg[1][2:] 表示取第一个命令行参数,但是去掉前两个字节

比如命令行参数为   “--help” ,就去掉“--”,执行"help"参数。

以下代码来自简明Python教程。

这里如果有sys.arg[0]则表示cat.py
[python] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #!/usr/bin/python  
  2. # Filename: cat.py  
  3.   
  4. import sys  
  5.   
  6. def readfile(filename):  
  7.     '''''Print a file to the standard output.'''  
  8.     f = file(filename)  
  9.     while True:  
  10.         line = f.readline()  
  11.         if len(line) == 0:  
  12.             break  
  13.         print line, # notice comma  
  14.     f.close()  
  15.   
  16. # Script starts from here  
  17. if len(sys.argv) < 2:  
  18.     print 'No action specified.'  
  19.     sys.exit()  
  20.   
  21. if sys.argv[1].startswith('--'):  
  22.     option = sys.argv[1][2:]  
  23.     # 取 sys.argv[1] but without the first two characters,这里去掉“--”  
  24.     if option == 'version':  
  25.         print 'Version 1.2'  
  26.     elif option == 'help':  
  27.         print '''''\ 
  28. This program prints files to the standard output. 
  29. Any number of files can be specified. 
  30. Options include: 
  31.   --version : Prints the version number 
  32.   --help    : Display this help'''  
  33.     else:  
  34.         print 'Unknown option.'  
  35.     sys.exit()  
  36. else:  
  37.     for filename in sys.argv[1:]:  
  38.         readfile(filename)  

    在运行程序时,可能需要根据不同的条件,输入不同的命令行选项来实现不同的功能。目前有短选项长选项两种格式。短选项格式为"-"加上单个字母选项;长选项为"--"加上一个单词。长格式是在Linux下引入的。许多Linux程序都支持这两种格式。在Python中提供了getopt模块很好的实现了对这两种用法的支持,而且使用简单。


取得命令行参数
  在使用之前,首先要取得命令行参数。使用sys模块可以得到命令行参数。
import sys
print sys.argv

  然后在命令行下敲入任意的参数,如:
python get.py -o t --help cmd file1 file2

  结果为:
['get.py', '-o', 't', '--help', 'cmd', 'file1', 'file2']

  可见,所有命令行参数以空格为分隔符,都保存在了sys.argv列表中。其中第1个为脚本的文件名。

选项的写法要求
  对于短格式,"-"号后面要紧跟一个选项字母。如果还有此选项的附加参数,可以用空格分开,也可以不分开。长度任意,可以用引号。如以下是正确的:
-o
-oa
-obbbb
-o bbbb
-o "a b"
  对于长格式,"--"号后面要跟一个单词。如果还有些选项的附加参数,后面要紧跟"=",再加上参数。"="号前后不能有空格。如以下是正确的:

--help=file1

  而这些是不正确的:
-- help=file1
--help =file1
--help = file1
--help= file1

如何用getopt进行分析
  使用getopt模块分析命令行参数大体上分为三个步骤:

1.导入getopt, sys模块
2.分析命令行参数
3.处理结果

  第一步很简单,只需要:
import getopt, sys

  第二步处理方法如下(以Python手册上的例子为例):
try:
    opts, args = getopt.getopt(sys.argv[1:], "ho:", ["help", "output="])
except getopt.GetoptError:
    # print help information and exit:

1.
 处理所使用的函数叫getopt(),因为是直接使用import导入的getopt模块,所以要加上限定getopt才可以。
2. 使用sys.argv[1:]过滤掉第一个参数(它是执行脚本的名字,不应算作参数的一部分)。
3. 使用短格式分析串"ho:"。当一个选项只是表示开关状态时,即后面不带附加参数时,在分析串中写入选项字符。当选项后面是带一个附加参数时,在分析串中写入选项字符同时后面加一个":"号。所以"ho:"就表示"h"是一个开关选项;"o:"则表示后面应该带一个参数。
4. 使用长格式分析串列表:["help", "output="]。长格式串也可以有开关状态,即后面不跟"="号。如果跟一个等号则表示后面还应有一个参数。这个长格式表示"help"是一个开关选项;"output="则表示后面应该带一个参数。
5. 调用getopt函数。函数返回两个列表:opts和args。opts为分析出的格式信息。args为不属于格式信息的剩余的命令行参数。opts是一个两元组的列表。每个元素为:(选项串,附加参数)。如果没有附加参数则为空串''。
6. 整个过程使用异常来包含,这样当分析出错时,就可以打印出使用信息来通知用户如何使用这个程序。

  如上面解释的一个命令行例子为:
'-h -o file --help --output=out file1 file2'

  在分析完成后,opts应该是:
[('-h', ''), ('-o', 'file'), ('--help', ''), ('--output', 'out')]

  而args则为:
['file1', 'file2']

  第三步主要是对分析出的参数进行判断是否存在,然后再进一步处理。主要的处理模式为:
for o, a in opts:
    if o in ("-h", "--help"):
        usage()
        sys.exit()
    if o in ("-o", "--output"):
        output = a

  使用一个循环,每次从opts中取出一个两元组,赋给两个变量。o保存选项参数,a为附加参数。接着对取出的选项参数进行处理。(例子也采用手册的例子)

posted @ 2016-07-20 12:22  JokerJason  阅读(18971)  评论(0编辑  收藏  举报

It's not who you are underneath, it's what you do that defines you

Brick walls are there for a reason :they let us prove how badly we want things