Python标准库--argparse模块

argparse--命令行参数解析

简单示例

import argparse

parser = argparse.ArgumentParser(description="sample", add_help=True)

parser.add_argument('-a', action="store_true", default=False)
parser.add_argument('-b', action="store", dest='b')
parser.add_argument('-c', action="store", dest='c', type=int)

print(parser.parse_args(['-a', '-bval', '-c', '3']))

从文件读取参数

parser = argparse.ArgumentParser(description="sample",
                                 add_help=True,
                                 fromfile_prefix_chars='@')

parser.add_argument('-a', action="store_true", default=False)
parser.add_argument('-b', action="store", dest='b')
parser.add_argument('-c', action="store", dest='c', type=int)

print(parser.parse_args(['@argparse.txt']))

# argparse.txt
-a
-b2
-c2

posted @ 2017-06-18 14:36  兔头咖啡  阅读(396)  评论(0编辑  收藏  举报


作者:兔头咖啡
出处:http://www.cnblogs.com/wj5633/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。