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高并发异步服务器核心库forkcore使用方法
Nov 26 Python
Python入门篇之函数
Oct 20 Python
Python编程中运用闭包时所需要注意的一些地方
May 02 Python
Python3通过Luhn算法快速验证信用卡卡号的方法
May 14 Python
python 执行文件时额外参数获取的实例
Dec 18 Python
python整小时 整天时间戳获取算法示例
Feb 20 Python
谈一谈基于python的面向对象编程基础
May 21 Python
网易有道2017内推编程题 洗牌(python)
Jun 19 Python
Python selenium抓取虎牙短视频代码实例
Mar 02 Python
Django ORM filter() 的运用详解
May 14 Python
python中对二维列表中一维列表的调用方法
Jun 07 Python
通过Python把学姐照片做成拼图游戏
Feb 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设计模式 Mediator (中介者模式)
2011/06/26 PHP
PHP概率计算函数汇总
2015/09/13 PHP
php把时间戳转换成多少时间之前函数的实例
2016/11/16 PHP
Yii Framework框架使用PHPExcel组件的方法示例
2019/07/24 PHP
用js遍历 table的脚本
2008/07/23 Javascript
JavaScript获取多个数组的交集简单实例
2013/11/11 Javascript
IE中图片的onload事件无效问题和解决方法
2014/06/06 Javascript
jquery操作checkbox示例分享
2014/07/21 Javascript
超链接的禁用属性Disabled使用示例
2014/07/31 Javascript
深入理解JavaScript系列(26):设计模式之构造函数模式详解
2015/03/03 Javascript
JQuery中上下文选择器实现方法
2015/05/18 Javascript
基于jQuery倾斜打开侧边栏菜单特效代码
2015/09/15 Javascript
javascript设置文本框光标的方法实例小结
2016/11/04 Javascript
微信小程序  checkbox组件详解及简单实例
2017/01/10 Javascript
jQuery加载及解析XML文件的方法实例分析
2017/01/22 Javascript
socket.io学习教程之基本应用(二)
2017/04/29 Javascript
angularjs 缓存的使用详解
2018/03/19 Javascript
用原生 JS 实现 innerHTML 功能实例详解
2019/04/03 Javascript
Vue实现表格批量审核功能实例代码
2019/05/28 Javascript
vue-cli3+typescript新建一个项目的思路分析
2019/08/06 Javascript
JavaScript onclick事件使用方法详解
2020/05/15 Javascript
基于vue+echarts数据可视化大屏展示的实现
2020/12/25 Vue.js
python实现微信接口(itchat)详细介绍
2017/10/23 Python
python画出三角形外接圆和内切圆的方法
2018/01/25 Python
使用python实现ftp的文件读写方法
2019/07/02 Python
Django 过滤器汇总及自定义过滤器使用详解
2019/07/19 Python
python属于解释型语言么
2020/06/15 Python
Python 中的函数装饰器和闭包详解
2021/02/06 Python
HTML5中的websocket实现直播功能
2018/05/21 HTML / CSS
Html5定位终极解决方案
2020/02/05 HTML / CSS
美国体育用品商店:Rally House(NCAA、NFL、MLB、NBA、NHL和MLS)
2018/01/03 全球购物
初中生自我鉴定
2014/02/04 职场文书
社区活动策划方案
2014/08/21 职场文书
给朋友的道歉短信
2015/05/12 职场文书
python 用递归实现通用爬虫解析器
2021/04/16 Python
如何利用Matlab制作一款真正的拼图小游戏
2021/05/11 Python