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实现监控linux性能及进程消耗性能的方法
Jul 25 Python
Python字典操作简明总结
Apr 13 Python
Django2.1.3 中间件使用详解
Nov 26 Python
使用Python横向合并excel文件的实例
Dec 11 Python
Python流行ORM框架sqlalchemy安装与使用教程
Jun 04 Python
自定义django admin model表单提交的例子
Aug 23 Python
python 实现turtle画图并导出图片格式的文件
Dec 07 Python
解决Django提交表单报错:CSRF token missing or incorrect的问题
Mar 13 Python
使用python实现下载我们想听的歌曲,速度超快
Jul 09 Python
python Xpath语法的使用
Nov 26 Python
浅谈哪个Python库才最适合做数据可视化
Jun 28 Python
Python实现排序方法常见的四种
Jul 15 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/08/19 PHP
PHP安全配置详细说明
2011/09/26 PHP
php获取服务器端mac和客户端mac的地址支持WIN/LINUX
2014/05/15 PHP
Discuz论坛密码与密保加密规则
2016/12/19 PHP
PHP实现的一致性Hash算法详解【分布式算法】
2018/03/31 PHP
Laravel Validator 实现两个或多个字段联合索引唯一
2019/05/08 PHP
PHP的JSON封装、转变及输出操作示例
2019/09/27 PHP
PHP架构及原理知识点详解
2019/12/22 PHP
js关于命名空间的函数实例
2015/02/05 Javascript
JavaScript编写连连看小游戏
2015/07/07 Javascript
JS组件系列之使用HTML标签的data属性初始化JS组件
2016/09/14 Javascript
使用BootStrap和Metroui设计的metro风格微网站或手机app界面
2016/10/21 Javascript
AngularJS通过ng-route实现基本的路由功能实例详解
2016/12/13 Javascript
Vue Ajax跨域请求实例详解
2017/06/20 Javascript
Vue2.0实现组件数据的双向绑定问题
2018/03/06 Javascript
vue awesome swiper异步加载数据出现的bug问题
2018/07/03 Javascript
Vue之mixin全局的用法详解
2018/08/22 Javascript
vue实现一个6个输入框的验证码输入组件功能的实例代码
2020/06/29 Javascript
[51:17]Mineski vs Secret 2019国际邀请赛淘汰赛 败者组 BO3 第一场 8.22
2019/09/05 DOTA
在Python中使用base64模块处理字符编码的教程
2015/04/28 Python
Python温度转换实例分析
2018/01/17 Python
Windows下python3.6.4安装教程
2018/07/31 Python
基于Python实现迪杰斯特拉和弗洛伊德算法
2020/05/27 Python
对Python3使运行暂停的方法详解
2019/02/18 Python
Windows 安装 Anaconda3+PyCharm的方法步骤
2019/06/13 Python
基于python的socket实现单机五子棋到双人对战
2020/03/24 Python
详解CSS3 filter:drop-shadow滤镜与box-shadow区别与应用
2020/08/24 HTML / CSS
HTML5页面直接调用百度地图API获取当前位置直接导航目的地的实现代码
2018/03/02 HTML / CSS
英国最大的汽车配件在线商店:Euro Car Parts
2019/09/30 全球购物
四好少年事迹材料
2014/01/12 职场文书
团购业务员岗位职责
2014/03/15 职场文书
机关节能减排实施方案
2014/03/17 职场文书
支行行长竞聘演讲稿
2014/05/15 职场文书
2014国庆节标语口号
2014/09/19 职场文书
云台山导游词
2015/02/03 职场文书
《吸血鬼:避世 血猎》官宣4.27发售 系列首款大逃杀
2022/04/03 其他游戏