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实现删除文件但保留指定文件
Jun 21 Python
PyQt5每天必学之滑块控件QSlider
Apr 20 Python
Python3实现爬虫爬取赶集网列表功能【基于request和BeautifulSoup模块】
Dec 05 Python
python处理multipart/form-data的请求方法
Dec 26 Python
图文详解python安装Scrapy框架步骤
May 20 Python
Python实现一个数组除以一个数的例子
Jul 20 Python
Python Django Vue 项目创建过程详解
Jul 29 Python
python按行读取文件并找出其中指定字符串
Aug 08 Python
Python3 Tkinkter + SQLite实现登录和注册界面
Nov 19 Python
win10下opencv-python特定版本手动安装与pip自动安装教程
Mar 05 Python
python基于selenium爬取斗鱼弹幕
Feb 20 Python
实例详解Python的进程,线程和协程
Mar 13 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读取RSS feed的代码
2008/08/01 PHP
利用PHP实现智能文件类型检测的实现代码
2011/08/02 PHP
PHP Warning: PHP Startup: Unable to load dynamic library \ D:/php5/ext/php_mysqli.dll\
2012/06/17 PHP
利用“多说”制作留言板、评论系统
2015/07/14 PHP
非常有用的9个PHP代码片段
2016/04/06 PHP
PHP封装的完整分页类示例
2018/08/21 PHP
laravel框架实现敏感词汇过滤功能示例
2020/02/15 PHP
javascript下判断一个元素是否存在的代码
2010/03/05 Javascript
JS判断元素为数字的奇异写法分享
2012/08/01 Javascript
js实现网站首页图片滚动显示
2013/02/04 Javascript
浅析js中的浮点型运算问题
2014/01/06 Javascript
js模拟C#中List的简单实例
2014/03/06 Javascript
JavaScript判断是否为数组的3种方法及效率比较
2015/04/01 Javascript
微信小程序 icon组件详细及实例代码
2016/10/25 Javascript
浅谈MVC+EF easyui dataGrid 动态加载分页表格
2016/11/10 Javascript
JavaScript基于自定义函数判断变量类型的实现方法
2016/11/23 Javascript
原生JS实现$.param() 函数的方法
2018/08/10 Javascript
jquery+ajax实现上传图片并显示上传进度功能【附php后台接收】
2019/06/06 jQuery
vue+mock.js实现前后端分离
2019/07/24 Javascript
微信小程序 自定义弹窗实现过程(附代码)
2019/12/05 Javascript
python以环状形式组合排列图片并输出的方法
2015/03/17 Python
Django接受前端数据的几种方法总结
2016/11/04 Python
python实现求解列表中元素的排列和组合问题
2018/03/15 Python
python贪吃蛇游戏代码
2020/04/18 Python
基于Django静态资源部署404的解决方法
2019/07/28 Python
Python读写操作csv和excle文件代码实例
2020/03/16 Python
python实现在线翻译
2020/06/18 Python
Python 发送邮件方法总结
2020/08/10 Python
Windows环境下Python3.6.8 importError: DLLload failed:找不到指定的模块
2020/11/01 Python
好药师网上药店:安全合法的网上药品零售药房
2017/02/15 全球购物
初中生学习生活的自我评价
2013/11/20 职场文书
教师批评与自我批评范文
2014/10/15 职场文书
2014年安全管理工作总结
2014/12/01 职场文书
2015年司法所工作总结
2015/04/27 职场文书
MySQL 8.0 Online DDL快速加列的相关总结
2021/06/02 MySQL
微信告警的zabbix监控系统 监控整个NGINX集群
2022/04/18 Servers