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多线程编程(二):启动线程的两种方法
Apr 05 Python
Django模板变量如何传递给外部js调用的方法小结
Jul 24 Python
Numpy数组的保存与读取方法
Apr 04 Python
python读取文本中数据并转化为DataFrame的实例
Apr 10 Python
python实现txt文件格式转换为arff格式
May 31 Python
Python调用服务接口的实例
Jan 03 Python
Python matplotlib学习笔记之坐标轴范围
Jun 28 Python
Python内置类型性能分析过程实例
Jan 29 Python
利用pyecharts读取csv并进行数据统计可视化的实现
Apr 17 Python
Iconfont(矢量图标)+iconmoon(图标svg互转)配合javascript实现社交分享系统
Apr 21 Python
python 实现两个npy档案合并
Jul 01 Python
Python实现制作销售数据可视化看板详解
Nov 27 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
PHP分页显示制作详细讲解
2006/10/09 PHP
PHP初学入门
2006/11/19 PHP
注册页面之前先验证用户名是否存在的php代码
2012/07/14 PHP
php封装的page分页类完整实例
2016/10/18 PHP
PHP中单例模式与工厂模式详解
2017/02/17 PHP
web基于浏览器的本地存储方法应用
2012/11/27 Javascript
document.all的一个比较完整的总结及案例
2013/01/31 Javascript
js语法学习之判断一个对象是否为数组
2014/05/13 Javascript
JS来动态的修改url实现对url的增删查改
2014/09/05 Javascript
基于Jquery实现表单验证
2020/07/20 Javascript
js+html5实现复制文字按钮
2017/07/15 Javascript
微信小程序实现签到功能
2018/10/31 Javascript
微信小程序日历组件使用方法详解
2018/12/29 Javascript
jQuery模拟html下拉多选框的原生实现方法示例
2019/05/30 jQuery
vue遍历对象中的数组取值示例
2019/11/07 Javascript
extjs图形绘制之饼图实现方法分析
2020/03/06 Javascript
详解为什么Vue中不要用index作为key(diff算法)
2020/04/04 Javascript
javascript如何使用函数random来实现课堂随机点名方法详解
2020/07/28 Javascript
python正则分组的应用
2013/11/10 Python
Python Web服务器Tornado使用小结
2014/05/06 Python
对dataframe进行列相加,行相加的实例
2018/06/08 Python
pycharm 设置项目的根目录教程
2020/02/12 Python
太阳镜仓库,售价20美元或更少:Sunglass Warehouse
2016/09/28 全球购物
美国领先的水果篮送货公司和新鲜水果供应商:The Fruit Company
2018/02/13 全球购物
保加利亚手表、香水、化妆品和珠宝购物网站:Brasty.bg
2020/04/22 全球购物
幼儿园教师奖惩制度
2014/02/01 职场文书
贷款委托书
2014/08/01 职场文书
2014教师党员自我评议(5篇)
2014/09/20 职场文书
群众路线教育实践活动对照检查材料
2014/09/22 职场文书
MYSQL数据库使用UTF-8中文编码乱码的解决办法
2021/05/26 MySQL
Java面试题冲刺第十七天--基础篇3
2021/08/07 面试题
用python基于appium模块开发一个自动收取能量的小助手
2021/09/25 Python
漫画「你在春天醒来」第10卷封面公开
2022/03/21 日漫
实现GO语言对数组切片去重
2022/04/20 Golang
鲲鹏 CentOS 7 安装Python3.7
2022/05/11 Servers
Mysql表数据比较大情况下修改添加字段的方法实例
2022/06/28 MySQL