Python编程argparse入门浅析
作者:foryouslgme 发布时间:2023-11-05 09:53:01
本文研究的主要是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 accumulatoroptional 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]
10D:\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交互模式下运行结果如下:
附件
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.
来源:http://blog.csdn.net/foryouslgme/article/details/52815371
![](https://www.aspxhome.com/images/zang.png)
![](https://www.aspxhome.com/images/jiucuo.png)
猜你喜欢
- 最近在网上经常看到朋友们聊到UEO,我就想哈UEO是啥东西啊,我去找啦些资料看,他们都说将来UEO发展一定会比较好,我也说这是肯定的.我为什
- 本文的重点就是如何创建一个PHP类来实现两个数间的加减乘除,这种类似于极简单的计算器功能。实现起来是非常简单的,继续往下看:这里简单给大家介
- Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列。1)
- 前言本来准备讲解nginx和apache的日志的,但是个人不太推荐apache(纯属个人爱好),这里就不介绍apache的日志了。作为一名程
- 背景在写代码过程中,如果有频繁重复性的编码操作,或者可以Reuse的各类代码,可以通过Python写一个脚本,自动生成这类代码,就不用每次手
- Python一切皆对象(object),每个对象都可能有多个属性(attribute)。Python的属性有一套统一的管理方案。属性的__d
- 本文介绍了笔者通过python程序实现某OA系统自动考勤打卡功能及相关逻辑原理的解析。Github: https://github.com/
- 单例模式是一个经典设计模式,简要的说,一个类的单例模式就是它只能被实例化一次,实例变量在第一次实例化时就已经固定。 在Python
- 细线边框是网页中定位区分内容常用的一种方法,配合特定图片的使用,往往能够达到不错的效果,那么如何制作细线边框呢?asp之家注:现在要实现这个
- 使用pandas导入csv文件内容1. 默认导入在Python中导入.csv文件用的方法是read_csv()。使用read_csv()进行
- 本文实例为大家分享了Python Opencv基于透视变换的图像矫正,供大家参考,具体内容如下一、自动获取图像顶点变换(获取图像轮廓顶点矫正
- 前言语音合成技术能将用户输入的文字,转换成流畅自然的语音输出,并且可以支持语速、音调、音量设置,打破传统文字式人机交互的方式,让人机沟通更自
- 在编程时你一定碰到过时间触发的事件,在VB中有timer控件,而ASP中没有,假如你要不停地查询数据库来等待一个返回结果的话,我想你一定知道
- 修改list中所有元素类型:方法一:new = list()a = ['1', '2', '3
- MySQL是中小型网站普遍使用的数据库之一,然而,很多人并不清楚MySQL到底能支持多大的数据量,再加上某些国内CMS厂商把数据承载量的责任
- 在使用DB2以来,碰到了几次出现提示SQL1032N错误,每次出错时出错信息大概如下:11/21/2004 22:15:33 0 0 SQL
- ASP开发中有用的函数(function)集合,挺有用的,请大家保留!'******************************
- 一般事件事件浏览器支持描述onClickIE3|N2|O3鼠标点击事件,多用在某个对象控制的范围内的鼠标点击onDblClickIE4|N4
- 本文实例为大家分享了js选项卡切换效果的具体实现代码,供大家参考,具体内容如下<!DOCTYPE html><html&g
- 完全备份的SH文件:exp_comp.shrq=` date +"%m%d" `su - oracle -c "