Python 读取命令行参数示例

import sys
import time

x=time.clock()

#打印可接受的参数
def prhelp():
    print ”’Usage: argv.py [options]

Options:
-h, –help            show this help message and exit
-v, –version         print the version number and exit
-s, –Second snapshot run the 2.bat

Example: argv.py -s
    ”’

#对各种参数进行处理
if (len(sys.argv) > 2):
    print ‘Option error!’
    prhelp()
    exit()
elif ‘-h’ in sys.argv:
    prhelp()
    exit()
elif ‘-v’ in sys.argv:
    print ‘argv 1.01’
    exit()
elif ‘-s’ in sys.argv:
    print ‘run 2.bat’
    exit()
else:
    prhelp()
    print ‘Option error! No such option: ‘ + sys.argv[1]
    exit()

发表评论

电子邮件地址不会被公开。 必填项已用*标注

机器人检查 *