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实现问号表达式(?)的方法
Nov 27 Python
在Python中使用PIL模块对图片进行高斯模糊处理的教程
May 05 Python
Python中使用不同编码读写txt文件详解
May 28 Python
pandas带有重复索引操作方法
Jun 08 Python
Python中分支语句与循环语句实例详解
Sep 13 Python
Python访问MongoDB,并且转换成Dataframe的方法
Oct 15 Python
python使用mitmproxy抓取浏览器请求的方法
Jul 02 Python
Python操作SQLite数据库过程解析
Sep 02 Python
python将print输出的信息保留到日志文件中
Sep 27 Python
python 项目目录结构设置
Feb 14 Python
使用python批量修改XML文件中图像的depth值
Jul 22 Python
详解Python+OpenCV绘制灰度直方图
Mar 22 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/02/14 PHP
php知道与问问的采集插件代码
2010/10/12 PHP
PHP strip_tags()去除HTML、XML以及PHP的标签介绍
2014/02/18 PHP
php生成图片验证码-附五种验证码
2015/08/19 PHP
PHP将字符串首字母大小写转换的实例
2017/01/21 PHP
关于 Laravel Redis 多个进程同时取队列问题详解
2017/12/25 PHP
php给数组赋值的实例方法
2019/09/26 PHP
SUN的《AJAX与J2EE》全文译了
2007/02/23 Javascript
数组Array进行原型prototype扩展后带来的for in遍历问题
2010/02/07 Javascript
jquery 查找select ,并触发事件的实现代码
2011/03/30 Javascript
使用JavaScript判断图片是否加载完成的三种实现方式
2014/05/04 Javascript
将HTML的左右尖括号等转义成实体形式的两种实现方式
2014/05/04 Javascript
javascript中键盘事件用法实例分析
2015/01/30 Javascript
jquery.qtip提示信息插件用法简单实例
2016/06/17 Javascript
jQuery图片渐变特效的简单实现
2016/06/25 Javascript
easyUI combobox实现联动效果
2017/01/17 Javascript
jQuery插件jqGrid动态获取列和列字段的方法
2017/03/03 Javascript
详解nodejs express下使用redis管理session
2017/04/24 NodeJs
详解vue-router和vue-cli以及组件之间的传值
2017/07/04 Javascript
JavaScript实现简单生成随机颜色的方法
2017/09/21 Javascript
详解mpvue小程序中怎么引入iconfont字体图标
2018/10/01 Javascript
JS将时间秒转换成天小时分钟秒的字符串
2019/07/10 Javascript
使用JS location实现搜索框历史记录功能
2019/12/23 Javascript
微信小程序实现同时上传多张图片
2020/02/03 Javascript
浅谈Vue3 Composition API如何替换Vue Mixins
2020/04/29 Javascript
python实现的生成随机迷宫算法核心代码分享(含游戏完整代码)
2014/07/11 Python
python实现获取序列中最小的几个元素
2014/09/25 Python
在Python中操作字典之fromkeys()方法的使用
2015/05/21 Python
使用Django Form解决表单数据无法动态刷新的两种方法
2017/07/14 Python
解决pycharm 工具栏Tool中找不到Run manager.py Task的问题
2019/07/01 Python
基于css3仿造window7的开始菜单
2010/06/17 HTML / CSS
Kaufmann Mercantile官网:家居装饰、配件、户外及更多
2018/09/28 全球购物
金融专业大学生自我评价
2014/01/09 职场文书
学雷锋标语
2014/06/25 职场文书
导游词之襄阳古城
2019/09/27 职场文书
Vue如何实现组件间通信
2021/05/15 Vue.js