Python编程argparse入门浅析


Posted in Python onFebruary 07, 2018

本文研究的主要是Python编程argparse的相关内容,具体介绍如下。

#aaa.py
#version 3.5
import os    #这句是没用了,不知道为什么markdown在编辑代码时,不加这一句,就不能显示代码高亮[汗]
import argparse


parser = argparse.ArgumentParser(description='Process some integers...')  #初始化一个分析器
#parser.add_argument(中的参数)
#__init__(self, option_strings, dest, nargs=None, const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None)
parser.add_argument('integers',metavar='N',type=int,nargs='+',
          help='an integer for the accumulator')    
          #这是一个添加【位置参数】
          #第一个参数是自定义的参数名,在代码中用来计算的(parser.parse_args().integers*2)


parser.add_argument('--sum',dest='accumulate',action='store_const',
          const=sum,default=max,
          help='sum the integers(default:find the max)')
          #这是一个添加【可选参数】
          #第一个参数是自定义的参数【在代码中的使用parser.parse_args().sum】【在系统命令行中的使用:>python aaa.py --sum



args = parser.parse_args()
print(args)       #Namespace(accumulate=<built-in function sum>, integers2=[1, 2, 3, 4])
print(args.integers)  #integers要与上面的对应
print(args.accumulate(args.integers))  #accumulate要与上面的对应

在系统命令行中进行参数调用结果如下:

D:\Program Files (x86)\Python35>python aaa.py -h
usage: aaa.py [-h] [--sum] N [N ...]

Process some integers...

positional arguments:
N an integer for the accumulator

optional arguments:
-h, --help show this help message and exit
--sum sum the integers(default:find the max)

D:\Program Files (x86)\Python35>python aaa.py 1 2 3 4 --sum
Namespace(accumulate=<built-in function sum>, integers2=[1, 2, 3, 4])
[1, 2, 3, 4]
10

D:\Program Files (x86)\Python35>python aaa.py 1 2 3 4
Namespace(accumulate=<built-in function max>, integers2=[1,2,3,4])
[1, 2, 3, 4]
4

在python交互模式下运行结果如下:

Python编程argparse入门浅析

附件

Keyword Arguments:
|
| - option_strings -- A list of command-line option strings which
| should be associated with this action.
|
| - dest -- The name of the attribute to hold the created object(s)
|
| - nargs -- The number of command-line arguments that should be
| consumed. By default, one argument will be consumed and a single
| value will be produced. Other values include:
| - N (an integer) consumes N arguments (and produces a list)
| - '?' consumes zero or one arguments
| - '*' consumes zero or more arguments (and produces a list)
| - '+' consumes one or more arguments (and produces a list)
| Note that the difference between the default and nargs=1 is that
| with the default, a single value will be produced, while with
| nargs=1, a list containing a single value will be produced.
|
| - const -- The value to be produced if the option is specified and the
| option uses an action that takes no values.
|
| - default -- The value to be produced if the option is not specified.
|
| - type -- A callable that accepts a single string argument, and
| returns the converted value. The standard Python types str, int,
| float, and complex are useful examples of such callables. If None,
| str is used.
|
| - choices -- A container of values that should be allowed. If not None,
| after a command-line argument has been converted to the appropriate
| type, an exception will be raised if it is not a member of this
| collection.
|
| - required -- True if the action must always be specified at the
| command line. This is only meaningful for optional command-line
| arguments.
|
| - help -- The help string describing the argument.
|
| - metavar -- The name to be used for the option's argument with the
| help string. If None, the 'dest' value will be used as the name.

总结

以上就是本文关于Python编程argparse入门浅析的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

Python 相关文章推荐
Python中的map()函数和reduce()函数的用法
Apr 27 Python
python使用pil生成图片验证码的方法
May 08 Python
python实现内存监控系统
Mar 07 Python
Python OpenCV处理图像之图像直方图和反向投影
Jul 10 Python
flask入门之文件上传与邮件发送示例
Jul 18 Python
Python目录和文件处理总结详解
Sep 02 Python
Python列表元素常见操作简单示例
Oct 25 Python
django 框架实现的用户注册、登录、退出功能示例
Nov 28 Python
Python+Redis实现布隆过滤器
Dec 08 Python
Python pip配置国内源的方法
Feb 14 Python
使用Tensorboard工具查看Loss损失率
Feb 15 Python
Python操作Excel的学习笔记
Feb 18 Python
PyQt5主窗口动态加载Widget实例代码
Feb 07 #Python
学习python中matplotlib绘图设置坐标轴刻度、文本
Feb 07 #Python
PyQt5打开文件对话框QFileDialog实例代码
Feb 07 #Python
python OpenCV学习笔记直方图反向投影的实现
Feb 07 #Python
Python实现上下班抢个顺风单脚本
Feb 07 #Python
Python SqlAlchemy动态添加数据表字段实例解析
Feb 07 #Python
Python实现抢购IPhone手机
Feb 07 #Python
You might like
PhpMyAdmin出现export.php Missing parameter: what /export_type错误解决方法
2012/08/09 PHP
php检测用户是否用手机(Mobile)访问网站的类
2014/01/09 PHP
ThinkPHP写数组插入与获取最新插入数据ID实例
2014/11/03 PHP
PHP中empty,isset,is_null用法和区别
2017/02/19 PHP
基于PHP常用文件函数和目录函数整理
2017/08/17 PHP
php使用pthreads v3多线程实现抓取新浪新闻信息操作示例
2020/02/21 PHP
javascript 数组学习资料收集
2010/04/11 Javascript
鼠标事件延时切换插件
2011/03/12 Javascript
JavaScript 基础篇(一)
2012/03/30 Javascript
利用jquery动画特效和css打造的侧边弹出垂直导航
2014/04/04 Javascript
跟我学Nodejs(三)--- Node.js模块
2014/05/25 NodeJs
JavaScript字符串常用类使用方法汇总
2015/04/14 Javascript
jquery+CSS3实现淘宝移动网页菜单效果
2015/08/31 Javascript
Vue.js学习之过滤器详解
2017/01/22 Javascript
认识less和webstrom的less配置方法
2017/08/02 Javascript
微信小程序实现列表下拉刷新上拉加载
2020/07/29 Javascript
js中call()和apply()改变指针问题的讲解
2019/01/17 Javascript
Vue组件实现触底判断
2019/06/26 Javascript
Element InputNumber计数器的使用方法
2020/07/27 Javascript
[05:36]DOTA2 2015国际邀请赛中国区预选赛第四日TOP10
2015/05/29 DOTA
Python自动重试HTTP连接装饰器
2015/04/28 Python
浅析Python中将单词首字母大写的capitalize()方法
2015/05/18 Python
总结python实现父类调用两种方法的不同
2017/01/15 Python
Python2和Python3中print的用法示例总结
2017/10/25 Python
python3大文件解压和基本操作
2017/12/15 Python
Django基础三之视图函数的使用方法
2019/07/18 Python
Django之提交表单与前后端交互的方法
2019/07/19 Python
python装饰器练习题及答案
2019/11/01 Python
jupyter修改文件名方式(TensorFlow)
2020/04/21 Python
使用keras实现非线性回归(两种加激活函数的方式)
2020/07/05 Python
opencv 图像轮廓的实现示例
2020/07/08 Python
四川internet信息高速公路(C#)笔试题
2012/02/29 面试题
教育学专业毕业生的自我鉴定
2013/11/26 职场文书
初中教师业务学习材料
2014/05/12 职场文书
小学中等生评语
2014/12/29 职场文书
Python超简单容易上手的画图工具库推荐
2021/05/10 Python