命令行参数读取和解析

 1 from argparse import ArgumentParser
 2 
 3 p = ArgumentParser(description='eg:  $python mm_crawler -n 8 -o "D:/mm_pics" -l 500 \
 4                                 this command means that your app will create 8 coroutines to download 500 pictures, and save them to D:/mm_pics')
 5 p.add_argument("-n", type = int, help="Set number of coroutines (default:10)", default=10)
 6 p.add_argument("-o", type = str, help='Set pictures save path (default:./pics/)', default="./pics/")
 7 p.add_argument("-l", type = int, help="number of limits of picture to crawl (default:0, no limit)", default=0)
 8 
 9 # p.print_help()
10 args = p.parse_args()
11 args = vars(args)
12 
13 #把这些值写到conf.py中
14 with open("conf.py",'wb') as conf_file:
15     conf_file.write("LIMIT=%s\n" % str(args['l']))
16     conf_file.write("P_THREAD_NUM=%s\n" % str(args['n']))
17     conf_file.write("SAVE_DIR='%s'\n" % args['o'])
18 conf_file.close()
19 
20 # print(args)

 

posted @ 2014-09-04 17:34  阿驹  阅读(289)  评论(0编辑  收藏  举报