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 相关文章推荐
在Windows8上的搭建Python和Django环境
Jul 03 Python
Python对数据库操作
Mar 28 Python
Python环境下搭建属于自己的pip源的教程
May 05 Python
django允许外部访问的实例讲解
May 14 Python
在Python中Dataframe通过print输出多行时显示省略号的实例
Dec 22 Python
python变量的存储原理详解
Jul 10 Python
wxPython实现带颜色的进度条
Nov 19 Python
关于Python3 lambda函数的深入浅出
Nov 27 Python
tensorflow 只恢复部分模型参数的实例
Jan 06 Python
python如何利用Mitmproxy抓包
Oct 10 Python
Python打包为exe详细教程
May 18 Python
Python中的套接字编程是什么?
Jun 21 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
swfupload 多文件上传实现代码
2008/08/27 PHP
php INI配置文件的解析实现分析
2011/01/04 PHP
PHP CURL模拟登录新浪微博抓取页面内容 基于EaglePHP框架开发
2012/01/16 PHP
php对象和数组相互转换的方法
2015/05/12 PHP
CI分页类首页、尾页不显示的解决方法
2016/03/28 PHP
PHP生成随机码的思路与方法实例探索
2019/04/11 PHP
实现复选框全选/全不选切换
2006/12/23 Javascript
JavaScript DOM 学习第七章 表单的扩展
2010/02/19 Javascript
jQuery EasyUI API 中文文档 - Calendar日历使用
2011/10/19 Javascript
JavaScript子窗口ModalDialog中操作父窗口对像
2012/12/11 Javascript
JQuery弹出炫丽对话框的同时让背景变灰色
2014/05/22 Javascript
将数字转换成大写的人民币表达式的js函数
2014/09/21 Javascript
jquery+html5制作超酷的圆盘时钟表
2015/04/14 Javascript
javascript动态添加checkbox复选框的方法
2015/12/23 Javascript
学习javascript文件加载优化
2016/02/19 Javascript
jQuery动态添加
2016/04/07 Javascript
ichart.js绘制虚线、平均分虚线效果的实现代码
2016/05/05 Javascript
JavaScript根据CSS的Media Queries来判断浏览设备的方法
2016/05/10 Javascript
js实现弹窗居中的简单实例
2016/10/09 Javascript
jQuery插件JWPlayer视频播放器用法实例分析
2017/01/11 Javascript
canvas绘制的直线动画
2017/01/23 Javascript
redux中间件之redux-thunk的具体使用
2018/04/17 Javascript
Angular项目如何升级至Angular6步骤全纪录
2018/09/03 Javascript
jQuery实现获取及设置CSS样式操作详解
2018/09/05 jQuery
深入学习JavaScript中的bom
2019/05/27 Javascript
mpvue实现小程序签到金币掉落动画(api实现)
2019/10/17 Javascript
js实现3D旋转相册
2020/08/02 Javascript
python3.6下Numpy库下载与安装图文教程
2019/04/02 Python
详解pandas中iloc, loc和ix的区别和联系
2020/03/09 Python
python爬虫分布式获取数据的实例方法
2020/11/26 Python
Python基于template实现字符串替换
2020/11/27 Python
Farnell德国:电子元器件供应商
2018/07/10 全球购物
Omio葡萄牙:全欧洲低价大巴、火车和航班搜索和比价
2019/02/09 全球购物
考博专家推荐信模板
2013/12/02 职场文书
格列佛游记读书笔记
2015/06/30 职场文书
Lakehouse数据湖并发控制陷阱分析
2022/03/31 Oracle