https://www.cnblogs.com/captain_jack/archive/2011/01/11/1933366.html
zzz.py
import sys
from optparse import OptionParser
filelist = ["/home/an/a","/home/an/b"]
def main(filelist,tmpfile=None):
for filename in filelist:
with open(filename) as file_obj:
for line in file_obj:
print(line)
if __name__ == '__main__':
parse = OptionParser()
parse.add_option("-f","--file",dest='filename',help='Here need one file at least', action='append')
(options, args) = parse.parse_args()
print(options.filename)
if not options.filename:
parse.error("YOU NEED INPUT A FILE")
#或者是
# sys.exit(1)
python zzz.py
# options.filename == None
python zzz.py -f 'aaaa'
# options.filename == ['aaaa']
python zzz.py -f 'aaaa' 'bbb' 'ccc' 'dddd'
# options.filename == ['aaaa']
# args = ['bbb','ccc','dddd']
python zzz.py -f 'aaaa' -f 'bbb' -f 'ccc' -f 'dddd'
# options.filename == [ 'aaaa' ,'bbb','ccc','dddd']
# args =[]