argparse模块可以设置两种命令参数,一个是位置参数,一个是命令参数

  • 位置参数

import argparse
parser = argparse.ArgumentParser(description="The Help of Python")
parser.add_argument("name")
args = parser.parse_args()
if args.name:
print(args.name)

直接不带参数运行

报错,需要传个位置参数

打印了传的参数

  • 可选参数

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR" import argparse
parser = argparse.ArgumentParser(description="The Help of Python")
parser.add_argument("-t","--thread")
args = parser.parse_args()
if args.thread:
print(args.thread)

运行结果

没传参数也没有报错,所以参数是可选的,而且参数的顺序也不是按顺序的

-t和--thread都能进行传参数

后面加等号和直接传参数是一样的

可选参数用规定的符号来识别,如上例的-和--,其他的为位置参数,如果有位置参数,不传的话就会报错

  • 设置命令参数的参数

help:参数描述信息

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR" import argparse
parser = argparse.ArgumentParser(description="The Help of Python")
parser.add_argument("-t","--thread",help="The Thread To Run")
args = parser.parse_args()
if args.thread:
print(args.thread)

运行

type:参数类型,如果类型不对就会报错

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR" import argparse
parser = argparse.ArgumentParser(description="The Help of Python")
parser.add_argument("-t","--thread",type=int,help="The Thread To Run")
args = parser.parse_args()
if args.thread:
print(args.thread)

参数类型设置为int类型

传入一个字符串

传入一个整型

成功执行

default:默认值

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR" import argparse
parser = argparse.ArgumentParser(description="The Help of Python")
parser.add_argument("-t","--thread",default=23,help="The Thread To Run")
args = parser.parse_args()
if args.thread:
print(args.thread)

不传入任何参数,直接运行

dest:可作为参数名

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR" import argparse
parser = argparse.ArgumentParser(description="The Help of Python")
parser.add_argument("-t","--thread",dest="thread_num",help="The Thread To Run")
args = parser.parse_args()
if args.thread_num: #需要改为dest的值
print(args.thread_num)

运行

choices:可选择的值

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR" import argparse
parser = argparse.ArgumentParser(description="The Help of Python")
parser.add_argument("-t","--thread",choices=["one","two"],help="The Thread To Run")
args = parser.parse_args()
if args.thread:
print(args.thread)

参数-t/--thread可选的值为one和two

运行

required:是否为必须参数,值为True或False

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR" import argparse
parser = argparse.ArgumentParser(description="The Help of Python")
parser.add_argument("-t","--thread",required=true,help="The Thread To Run")
args = parser.parse_args()
if args.thread:
print(args.thread)

不传参数,运行

报错

const:保存一个常量,不能单独使用,可以和nargs和action中的部分参数一起使用

nargs:参数的数量,值为整数,*为任意个,+为一个或多个,当值为?时,首先从命令行获取参数,然后是从const获取参数,最后从default获得参数

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR" import argparse
parser = argparse.ArgumentParser(description="The Help of Python")
parser.add_argument("-t","--thread",nargs="*",help="The Thread To Run")
args = parser.parse_args()
if args.thread:
print(args.thread)

运行结果

把传入的参数以列表输出

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR" import argparse
parser = argparse.ArgumentParser(description="The Help of Python")
parser.add_argument("-t","--thread",nargs="?",const=1,default=2,help="The Thread To Run")
args = parser.parse_args()
if args.thread:
print(args.thread)

从const获取了值

action:默认值为store,store_true和store_false把值设置为True或False,store_const把值存入const中,count统计参数出现的次数,append把传入参数存为列表,append_const根据const存为列表

store:

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR" import argparse
parser = argparse.ArgumentParser(description="The Help of Python")
parser.add_argument("-t","--thread",action="store",help="The Thread To Run")
args = parser.parse_args()
if args.thread:
print(args.thread)

运行结果

store_true和store_false:

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR" import argparse
parser = argparse.ArgumentParser(description="The Help of Python")
parser.add_argument("-t","--thread",action="store_true")
parser.add_argument("-u","--url",action="store_false")
args = parser.parse_args()
if args:
print(args)

运行

store_const:

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR" import argparse
parser = argparse.ArgumentParser(description="The Help of Python")
parser.add_argument("-t","--thread",action="store_const",const=12)
args = parser.parse_args()
if args:
print(args)

运行结果

count:

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR" import argparse
parser = argparse.ArgumentParser(description="The Help of Python")
parser.add_argument("-t","--thread",action="count")
args = parser.parse_args()
if args:
print(args)

运行结果

append:

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR" import argparse
parser = argparse.ArgumentParser(description="The Help of Python")
parser.add_argument("-t","--thread",action="append")
args = parser.parse_args()
if args:
print(args)

运行结果

append_const:

# -*- coding:utf-8 -*-
__author__ = "MuT6 Sch01aR" import argparse
parser = argparse.ArgumentParser(description="The Help of Python")
parser.add_argument("-t","--thread",action="append_const",const=1)
args = parser.parse_args()
if args:
print(args)

运行结果

Python命令模块argparse学习笔记(二)的更多相关文章

  1. Python命令模块argparse学习笔记(一)

    首先是关于-h/--help参数的设置 description:位于help信息前,可用于描述helpprog:描述help信息中程序的名称epilog:位于help信息后usage:描述程序的用途a ...

  2. Python命令模块argparse学习笔记(四)

    默认参数 ArgumentParser.set_defaults(**kwargs) set_defaults()可以设置一些参数的默认值 >>> parser = argparse ...

  3. Python命令模块argparse学习笔记(三)

    参数组 ArgumentParser.add_argument_group(title=None, description=None) 默认情况下,当显示帮助消息时,ArgumentParser将命令 ...

  4. python 命令行参数学习(二)

    照着例子看看打打,码了就会.写了个命令行参数调用进行运算的脚本. 参考文章链接:http://www.jianshu.com/p/a50aead61319 #-*-coding:utf-8-*- __ ...

  5. 基于python实现自动化办公学习笔记二

    word文件(1)读word文件 import win32comimport win32com.client def readWordFile(path): # 调用系统word功能,可以处理doc和 ...

  6. python3.4学习笔记(二十四) Python pycharm window安装redis MySQL-python相关方法

    python3.4学习笔记(二十四) Python pycharm window安装redis MySQL-python相关方法window安装redis,下载Redis的压缩包https://git ...

  7. python3.4学习笔记(二十三) Python调用淘宝IP库获取IP归属地返回省市运营商实例代码

    python3.4学习笔记(二十三) Python调用淘宝IP库获取IP归属地返回省市运营商实例代码 淘宝IP地址库 http://ip.taobao.com/目前提供的服务包括:1. 根据用户提供的 ...

  8. python3.4学习笔记(二十六) Python 输出json到文件,让json.dumps输出中文 实例代码

    python3.4学习笔记(二十六) Python 输出json到文件,让json.dumps输出中文 实例代码 python的json.dumps方法默认会输出成这种格式"\u535a\u ...

  9. python3.4学习笔记(二十五) Python 调用mysql redis实例代码

    python3.4学习笔记(二十五) Python 调用mysql redis实例代码 #coding: utf-8 __author__ = 'zdz8207' #python2.7 import ...

随机推荐

  1. 【bzoj5085】最大(二分+乱搞)

    题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=5085 这道题我们可以先二分答案,然后转化为判定是否有四角权值>=mid的矩形. ...

  2. JVM内存管理中的垃圾回收策略

    JVM垃圾回收策略 1.静态内存分配和回收 编译时已经确定了内存空间大小,程序被加载后则一次性分配好内存空间.程序结束后,则对应栈帧撤销,分配的静态内存空间则被回收. 2.动态内存分配和回收 程序运行 ...

  3. struts2常见配置解决错误There is no mapped for namespace[/] and action name

    我碰到这个错误的原因是我把配置文件名写成了Struts.xml,改成struts.xml就可以了. 在确定struts.xml本身并没有写错的情况下,那么发生错误有可能是路径,配置文件名. 如果实在找 ...

  4. 《Think in Java》(七)复用类

    Java 中复用代码的方式就是复用类,复用类的方式有: 组合 继承 代理(并没有啥高深的含义,只是在使用类A前,新增了类B,让类B的每个方法去调用类A中对应的方法,也就是说类B代理了类A...不过我还 ...

  5. Redis源码研究:哈希表 - 蕫的博客

    [http://dongxicheng.org/nosql/redis-code-hashtable/] 1. Redis中的哈希表 前面提到Redis是个key/value存储系统,学过数据结构的人 ...

  6. 如何直接在网页中显示PDF文件

    通过的浏览器:360.Firefox.IE.Chrome 2. 下面这个完整点: <param name="_Version" value="65539" ...

  7. 51nod 1215 单调栈/迭代

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1215 1215 数组的宽度 题目来源: Javaman 基准时间限制:1 ...

  8. C#中的线程(三)多线程

    C#中的线程(三)多线程   Keywords:C# 线程Source:http://www.albahari.com/threading/Author: Joe AlbahariTranslator ...

  9. hdoj-1046-Gridland(规律题)

    题目链接 #include <algorithm> #include <iostream> using namespace std; int main() { int t; c ...

  10. xdebug的安装测试

    1.下载 php -version PHP 7.2.0 (cli) (built: Dec 7 2017 23:07:46) ( NTS DEBUG ) 如果PHP版本是7.2以上的必须要下载Xdeb ...