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 26 Python
Python实现将xml导入至excel
Nov 20 Python
Python使用文件锁实现进程间同步功能【基于fcntl模块】
Oct 16 Python
Python数据结构之哈夫曼树定义与使用方法示例
Apr 22 Python
Python基本数据结构与用法详解【列表、元组、集合、字典】
Mar 23 Python
在Pycharm中调试Django项目程序的操作方法
Jul 17 Python
使用coverage统计python web项目代码覆盖率的方法详解
Aug 05 Python
python实现多线程端口扫描
Aug 31 Python
在Django中自定义filter并在template中的使用详解
May 19 Python
python中pandas库中DataFrame对行和列的操作使用方法示例
Jun 14 Python
音频处理 windows10下python三方库librosa安装教程
Jun 20 Python
Python内置数据类型中的集合详解
Mar 18 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的Yii框架入门使用教程
2016/02/15 PHP
PHP切割汉字的常用方法实例总结
2019/04/27 PHP
jquery一句话全选/取消全选
2011/03/01 Javascript
fancybox modal的完美解决(右上的X)
2012/10/30 Javascript
javascript游戏开发之《三国志曹操传》零部件开发(三)情景对话中仿打字机输出文字
2013/01/23 Javascript
jquery 实现二级/三级/多级联动菜单的思路及代码
2013/04/08 Javascript
JavaScript中json使用自己总结
2013/08/13 Javascript
jquery复选框全选/取消示例
2013/12/30 Javascript
javascript实现简易计算器
2017/02/01 Javascript
js中变量的连续赋值(实例讲解)
2017/07/08 Javascript
ES7中利用Await减少回调嵌套的方法详解
2017/11/01 Javascript
Nodejs连接mysql并实现增、删、改、查操作的方法详解
2018/01/04 NodeJs
vue初始化动画加载的实例
2018/09/01 Javascript
vue实现微信分享功能
2018/11/28 Javascript
JavaScript字符串转数字的简单实现方法
2020/11/27 Javascript
python 动态加载的实现方法
2017/12/22 Python
利用Python+Java调用Shell脚本时的死锁陷阱详解
2018/01/24 Python
Python学习笔记之open()函数打开文件路径报错问题
2018/04/28 Python
Tensorflow实现酸奶销量预测分析
2019/07/19 Python
详解Python IO编程
2020/07/24 Python
HTML5如何为形状图上颜色怎么绘制具有颜色和透明度的矩形
2014/06/23 HTML / CSS
CSS3 画基本图形,圆形、椭圆形、三角形等
2016/09/20 HTML / CSS
用canvas显示验证码的实现
2020/04/10 HTML / CSS
类成员函数的重载、覆盖和隐藏区别
2016/01/27 面试题
《乌塔》教学反思
2014/02/17 职场文书
主管竞聘书范文
2014/03/31 职场文书
荷叶母亲教学反思
2014/04/30 职场文书
服务承诺口号
2014/05/22 职场文书
保证金退回承诺函格式
2015/01/21 职场文书
2015年法务工作总结范文
2015/05/23 职场文书
2015年个人实习工作总结
2015/05/28 职场文书
工资证明范本
2015/06/12 职场文书
政审证明材料
2015/06/19 职场文书
小学学习委员竞选稿
2015/11/20 职场文书
民警忠诚教育心得体会
2016/01/23 职场文书
2016年六一儿童节开幕词
2016/03/04 职场文书