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 用户登录验证的小例子
Mar 06 Python
Python常用列表数据结构小结
Aug 06 Python
使用基于Python的Tornado框架的HTTP客户端的教程
Apr 24 Python
Python实现包含min函数的栈
Apr 29 Python
python如何统计序列中元素
Jul 31 Python
numpy返回array中元素的index方法
Jun 27 Python
使用Pyinstaller转换.py文件为.exe可执行程序过程详解
Aug 06 Python
python代码实现逻辑回归logistic原理
Aug 07 Python
python 19个值得学习的编程技巧
Aug 15 Python
详解python的xlwings库读写excel操作总结
Feb 26 Python
基于Python实现的购物商城管理系统
Apr 27 Python
python可视化大屏库big_screen示例详解
Nov 23 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编写大型网站问题集
2007/03/06 PHP
jquery中输入验证中一个不错的效果
2010/08/21 Javascript
javaScript arguments 对象使用介绍
2013/10/18 Javascript
jQuery $.extend()用法总结
2014/06/15 Javascript
javascript进行四舍五入方法汇总
2014/12/16 Javascript
node.js解决获取图片真实文件类型的问题
2014/12/20 Javascript
浅析JavaScript中的变量复制、参数传递和作用域链
2016/01/13 Javascript
JS+Canvas绘制时钟效果
2020/08/20 Javascript
AngularJS ng-bind 指令简单实现
2016/07/30 Javascript
vue.js实现仿原生ios时间选择组件实例代码
2016/12/21 Javascript
php简单数据库操作类的封装
2017/06/08 Javascript
JS实现灯泡开关特效
2020/03/30 Javascript
VUE实现密码验证与提示功能
2019/10/18 Javascript
vue中watch和computed为什么能监听到数据的改变以及不同之处
2019/12/27 Javascript
vue项目中在可编辑div光标位置插入内容的实现代码
2020/01/07 Javascript
Python生成随机数的方法
2014/01/14 Python
python遍历类中所有成员的方法
2015/03/18 Python
以windows service方式运行Python程序的方法
2015/06/03 Python
RC4文件加密的python实现方法
2015/06/30 Python
python入门基础之用户输入与模块初认识
2016/11/14 Python
使用Python实现从各个子文件夹中复制指定文件的方法
2018/10/25 Python
Python 获取div标签中的文字实例
2018/12/20 Python
python远程调用rpc模块xmlrpclib的方法
2019/01/11 Python
python 执行终端/控制台命令的例子
2019/07/12 Python
python多线程与多进程及其区别详解
2019/08/08 Python
Python一键安装全部依赖包的方法
2019/08/12 Python
python numpy生成等差数列、等比数列的实例
2020/02/25 Python
英国复古和经典球衣网站:Vintage Football Shirts
2018/10/05 全球购物
学生干部学习的自我评价
2014/02/18 职场文书
出国留学担保书
2014/05/20 职场文书
员工安全生产责任书
2014/07/22 职场文书
员工安全责任书范本
2014/07/24 职场文书
毕业实习指导教师评语
2014/12/31 职场文书
2015年度招聘工作总结
2015/05/28 职场文书
Redis分布式锁的7种实现
2022/04/01 Redis
Java Spring Boot 正确读取配置文件中的属性的值
2022/04/20 Java/Android