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数据结构之线性表的顺序存储结构
Sep 28 Python
浅析python中的迭代与迭代对象
Oct 08 Python
python 生成图形验证码的方法示例
Nov 11 Python
python http基本验证方法
Dec 26 Python
pandas dataframe添加表格框线输出的方法
Feb 08 Python
Python多项式回归的实现方法
Mar 11 Python
Python 中Django安装和使用教程详解
Jul 03 Python
利用python计算windows全盘文件md5值的脚本
Jul 27 Python
python 实现屏幕录制示例
Dec 23 Python
python json load json 数据后出现乱序的解决方案
Feb 27 Python
Python爬虫小例子——爬取51job发布的工作职位
Jul 10 Python
BeautifulSoup中find和find_all的使用详解
Dec 07 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 注释规范
2012/03/29 PHP
JSON两种结构之对象和数组的理解
2016/07/19 PHP
PHP编程实现多维数组按照某个键值排序的方法小结【2种方法】
2017/04/27 PHP
PHP生成二维码与识别二维码的方法详解【附源码下载】
2019/03/07 PHP
屏蔽鼠标右键、Ctrl+n、shift+F10、F5刷新、退格键 的javascript代码
2007/04/01 Javascript
Jquery 数据选择插件Pickerbox使用介绍
2012/08/24 Javascript
JS等比例缩小图片尺寸的实例
2013/02/27 Javascript
使用jquery选择器如何获取父级元素、同级元素、子元素
2014/05/14 Javascript
完美兼容各大浏览器的jQuery插件实现图片切换特效
2014/12/12 Javascript
jQuery中closest和parents的区别分析
2015/05/07 Javascript
JavaScript学习笔记之DOM基础 2.4
2015/08/14 Javascript
JavaScript小技巧整理
2015/12/30 Javascript
js简单倒计时实现代码
2016/04/30 Javascript
Struts2+jquery.form.js实现图片与文件上传的方法
2016/05/05 Javascript
微信小程序购物商城系统开发系列-工具篇的介绍
2016/11/21 Javascript
ionic实现底部分享功能
2017/05/11 Javascript
通俗解释JavaScript正则表达式快速记忆
2017/08/23 Javascript
基于JavaScript获取url参数2种方法
2020/04/17 Javascript
JavaScript监听键盘事件代码实现
2020/06/03 Javascript
Python中操作文件之write()方法的使用教程
2015/05/25 Python
Python表示矩阵的方法分析
2017/05/26 Python
Python读取Json字典写入Excel表格的方法
2018/01/03 Python
python使用itchat库实现微信机器人(好友聊天、群聊天)
2018/01/04 Python
详解Django的CSRF认证实现
2018/10/09 Python
python将一组数分成每3个一组的实例
2018/11/14 Python
在Python中获取操作系统的进程信息
2019/08/27 Python
浅析python 动态库m.so.1.0错误问题
2020/05/09 Python
python 解决Fatal error in launcher:错误问题
2020/05/21 Python
keras实现多GPU或指定GPU的使用介绍
2020/06/17 Python
学习委员自我鉴定
2014/01/13 职场文书
揠苗助长教学反思
2014/02/04 职场文书
环保公益广告语
2014/03/13 职场文书
村级换届选举方案
2014/05/10 职场文书
公司车辆维修管理制度
2015/08/05 职场文书
施工安全协议书
2016/03/22 职场文书
python模块与C和C++动态库相互调用实现过程示例
2021/11/02 Python