#/usr/bin/env python
#coding:utf-8
import sys
# print(sys.argv[1])
def readFile(filename):
"""定义readFile函数,从文件中读出文件内容"""
with open(filename) as f:
while True:
line = f.readline()
if len(line) == 0:
break
print(line)
print(sys.argv)
if len(sys.argv) < 2:
print('No action specified')
sys.exit()
if sys.argv[1].startswith('--'):
option = sys.argv[1][2:]
# fetch sys.argv[1] but without the first two characters,索引为2开始,往后取所有
if option == 'version':#当命令行参数为-- version,显示版本号
print('version 1.2')
elif option == 'help': #当命令行参数为--help时,显示相关帮助内容
print("""
This program prints files to the standard output.
Any number of files can be specified.
Options include:
--version : Prints the version number
--help : Display this help'
""")
else:
print('Unknow option')
sys.exit()
else:
for filename in sys.argv[1:]:#当参数为文件名时,传入readfile,读出其内容
readFile(filename)