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字符串和文件操作常用函数分析
Apr 08 Python
浅谈python中np.array的shape( ,)与( ,1)的区别
Jun 04 Python
python读取txt文件并取其某一列数据的示例
Feb 19 Python
谈一谈基于python的面向对象编程基础
May 21 Python
解决Python内层for循环如何break出外层的循环的问题
Jun 24 Python
python装饰器原理与用法深入详解
Dec 19 Python
python 统计文件中的字符串数目示例
Dec 24 Python
tensorflow:指定gpu 限制使用量百分比,设置最小使用量的实现
Feb 06 Python
python opencv进行图像拼接
Mar 27 Python
python 实现控制鼠标键盘
Nov 27 Python
用Python爬虫破解滑动验证码的案例解析
May 06 Python
python自动化测试通过日志3分钟定位bug
Nov 20 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的配置文件php.ini
2006/10/09 PHP
PHP IF ELSE简化/三元一次式的使用
2011/08/22 PHP
PHP通过串口实现发送短信
2015/07/08 PHP
PDO的安全处理与事物处理方法
2016/10/31 PHP
jquery select操作的日期联动实现代码
2009/12/06 Javascript
jQuery判断元素是否是隐藏的代码
2011/04/24 Javascript
js中parseInt函数浅谈
2013/07/31 Javascript
基于jquery实现可定制的web在线富文本编辑器附源码下载
2015/11/17 Javascript
nodejs6下使用koa2框架实例
2017/05/18 NodeJs
Vue的路由动态重定向和导航守卫实例
2018/03/17 Javascript
微信小程序收藏功能的实现代码
2018/06/12 Javascript
小程序文字跑马灯效果
2018/12/28 Javascript
JavaScript简单实现的仿微博留言功能示例
2019/01/17 Javascript
vue计算属性computed、事件、监听器watch的使用讲解
2019/01/21 Javascript
vue element-ui el-date-picker限制选择时间为当天之前的代码
2019/11/07 Javascript
vue 二维码长按保存和复制内容操作
2020/09/22 Javascript
关于better-scroll插件的无法滑动bug(2021通过插件解决)
2021/03/01 Javascript
python+flask实现API的方法
2018/11/21 Python
用python爬取租房网站信息的代码
2018/12/14 Python
Python3.5装饰器原理及应用实例详解
2019/04/30 Python
Python从list类型、range()序列简单认识类(class)【可迭代】
2019/05/31 Python
python3 pillow模块实现简单验证码
2019/10/31 Python
基于python实现图片转字符画代码实例
2020/09/04 Python
python 基于UDP协议套接字通信的实现
2021/01/22 Python
CSS3 :default伪类选择器使用简介
2018/03/15 HTML / CSS
创业计划书如何编写
2014/02/06 职场文书
《纸船和风筝》教学反思
2014/02/15 职场文书
实习护士自荐信
2014/06/21 职场文书
商务邀请函
2015/01/30 职场文书
个人总结与自我评价2015
2015/03/11 职场文书
干部外出学习心得体会
2016/01/18 职场文书
2019银行竞聘书
2019/06/21 职场文书
北京大学中文系教授推荐的10本小说
2019/08/08 职场文书
使用CSS连接数据库的方式
2022/02/28 HTML / CSS
《游戏王:大师决斗》新活动上线 若无符合卡组可免费租用
2022/04/13 其他游戏
WIN10使用IIS部署ftp服务器详细教程
2022/08/05 Servers