Python中argparse模块的使用
2015-09-09 09:43
633 查看
argparse是python用于解析命令行参数的标准模块,用法如下:
命令行下进行测试:
#####################################################################
更多参数介绍如下:
class argparse.ArgumentParser([description][, epilog][, prog][, usage][, add_help][, argument_default][, parents][, prefix_chars][, conflict_handler][, formatter_class])
* description - Text to display before the argument help.
* epilog - Text to display after the argument help.
* add_help - Add a -h/–help option to the parser. (default: True)
* argument_default - Set the global default value for arguments. (default: None)
* parents - A list of ArgumentParser objects whose arguments should also be included.
* prefix_chars - The set of characters that prefix optional arguments. (default: ‘-‘)
* fromfile_prefix_chars - The set of characters that prefix files from which additional arguments should be read. (default: None)
* formatter_class - A class for customizing the help output.
* conflict_handler - Usually unnecessary, defines strategy for resolving conflicting optionals.
* prog - The name of the program (default: sys.argv[0])
* usage - The string describing the program usage (default: generated)
ArgumentParser.add_argument(name or flags...[, action][, nargs][, const][, default][, type][, choices][, required][, help][, metavar][, dest])
* name or flags - Either a name or a list of option strings, e.g. foo or -f, --foo.
* action - The basic type of action to be taken when this argument is encountered at the command line.
* nargs - The number of command-line arguments that should be consumed.
* const - A constant value required by some action and nargs selections.
* default - The value produced if the argument is absent from the command line.
* type - The type to which the command-line argument should be converted.
* choices - A container of the allowable values for the argument.
* required - Whether or not the command-line option may be omitted (optionals only).
* help - A brief description of what the argument does.
* metavar - A name for the argument in usage messages.
* dest - The name of the attribute to be added to the object returned by parse_args().
import argparse def test(a, b): print int(a)+int(b) parser=argparse.ArgumentParser() # 创建解析器 parser.add_argument("-x","--x",dest="a") # 增加命令行参数 parser.add_argument("-y","--y",dest="b") args=parser.parse_args() test(a=args.a, b=args.b)
命令行下进行测试:
#####################################################################
更多参数介绍如下:
class argparse.ArgumentParser([description][, epilog][, prog][, usage][, add_help][, argument_default][, parents][, prefix_chars][, conflict_handler][, formatter_class])
* description - Text to display before the argument help.
* epilog - Text to display after the argument help.
* add_help - Add a -h/–help option to the parser. (default: True)
* argument_default - Set the global default value for arguments. (default: None)
* parents - A list of ArgumentParser objects whose arguments should also be included.
* prefix_chars - The set of characters that prefix optional arguments. (default: ‘-‘)
* fromfile_prefix_chars - The set of characters that prefix files from which additional arguments should be read. (default: None)
* formatter_class - A class for customizing the help output.
* conflict_handler - Usually unnecessary, defines strategy for resolving conflicting optionals.
* prog - The name of the program (default: sys.argv[0])
* usage - The string describing the program usage (default: generated)
ArgumentParser.add_argument(name or flags...[, action][, nargs][, const][, default][, type][, choices][, required][, help][, metavar][, dest])
* name or flags - Either a name or a list of option strings, e.g. foo or -f, --foo.
* action - The basic type of action to be taken when this argument is encountered at the command line.
* nargs - The number of command-line arguments that should be consumed.
* const - A constant value required by some action and nargs selections.
* default - The value produced if the argument is absent from the command line.
* type - The type to which the command-line argument should be converted.
* choices - A container of the allowable values for the argument.
* required - Whether or not the command-line option may be omitted (optionals only).
* help - A brief description of what the argument does.
* metavar - A name for the argument in usage messages.
* dest - The name of the attribute to be added to the object returned by parse_args().
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法