博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python的argparse 模块测试
阅读量:4216 次
发布时间:2019-05-26

本文共 1038 字,大约阅读时间需要 3 分钟。

argparse是python 内置的用于命令行选项与参数解析的模块使用一般分为三步,创建ArgumentParser()对象,调用add_argument() 方法添加参数,调用parse_args()解析添加的参数其中参数有分为可以参数和必选参数下面的例子包含了可选参数和必选参数,下面的--sum 属于可选参数import argparseparser=argparse.ArgumentParser()parser.add_argument('integer',type=int,metavar='N',nargs='+',help="display a number")parser.add_argument('--sum',dest='accumulate',action='store_const',const=sum,default=max,help="sum the  number")args=parser.parse_args()print args.accumulate(args.integer)[root@localhost kernel]# python test.pyusage: test.py [-h] integertest.py: error: too few arguments[root@localhost kernel]# python test.py 123123[root@localhost kernel]# python test.py asdfusage: test.py [-h] integertest.py: error: argument integer: invalid int value: 'asdf'[root@localhost kernel]# python test.py -husage: test.py [-h] integerpositional arguments:  integer     display a numberoptional arguments:  -h, --help  show this help message and exit[root@localhost kernel]# python test.py 1 2 --sum3[root@localhost kernel]# python test.py 1 22[root@localhost kernel]#

 

转载地址:http://cvnmi.baihongyu.com/

你可能感兴趣的文章
通过FTP服务的winsockes录制脚本
查看>>
LRwinsocket协议测试AAA服务器
查看>>
Net远程管理实验
查看>>
反病毒专家谈虚拟机技术 面临两大技术难题
查看>>
几种典型的反病毒技术:特征码技术、覆盖法技术等
查看>>
Software Security Testing软件安全测试
查看>>
论文浅尝 | 通过共享表示和结构化预测进行事件和事件时序关系的联合抽取
查看>>
廖雪峰Python教程 学习笔记3 hello.py
查看>>
从内核看epoll的实现(基于5.9.9)
查看>>
python与正则表达式
查看>>
安装.Net Framework 4.7.2时出现“不受信任提供程序信任的根证书中终止”的解决方法
查看>>
input type=“button“与input type=“submit“的区别
查看>>
解决Github代码下载慢问题!
查看>>
LeetCode-栈|双指针-42. 接雨水
查看>>
Linux文件和设备编程
查看>>
文件描述符
查看>>
终端驱动程序:几个简单例子
查看>>
HTML条件注释
查看>>
内核态与用户态
查看>>
使用mingw(fedora)移植virt-viewer
查看>>