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. mysql基础(5)-关联(mysql+pandas)

    表关联类型 内连接: 仅显示满足条件的行 From T1,T2 where T1.ID=T2.ID From T1 inner join T2 ON T1.ID=T2.ID 左连接: 显示左表T1中的 ...

  2. MapReduce-二进制输入

    Hadoop的MapReduce不只是可以处理文本信息,它还可以处理二进制格式的数据1. 关于SequenceFileInputFormat类Hadoop的顺序文件格式存储二进制的键/值对的序列.由于 ...

  3. sqoop1.4.6 全量导入与增量导入 与使用技巧

    全量导入: sqoop import --connect jdbc:mysql://192.168.0.144:3306/db_blog --username root --password 1234 ...

  4. linux下 stat statfs 获取 文件 磁盘 信息

    stat函数讲解 表头文件:    #include <sys/stat.h>              #include <unistd.h> 定义函数:    int st ...

  5. Linux命令之awk_1

    简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再 ...

  6. poj 1300 欧拉图

    http://poj.org/problem?id=1300 要不是书上有翻译我估计要卡死,,,首先这是一个连通图,鬼知道是那句话表示出来的,终点必须是0,统计一下每个点的度数,如果是欧拉回路那么起点 ...

  7. cassandra 集群并发测试脚本

    prepare: create keyspace ycsb WITH REPLICATION = { }; USE ycsb; CREATE TABLE users ( firstname text, ...

  8. mac上获取手机的uuid

    把手机连上mac 终端中输入: system_profiler SPUSBDataType | grep "Serial Number:.*" 修改用 | sed s#" ...

  9. JavaWeb学习总结(二) Servlet

    本文目录 一.Servlet概述 二.Servlet接口 三.GenericServlet 四.HttpServlet 五.Servlet细节 六.ServletContext 回到顶部 一.Serv ...

  10. L107

    It is advisable to take an open- minded approach to new idea. 对新思想采取不存先入之见的态度是明智的.That said, the com ...