Python命令模块argparse学习笔记(二)
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学习笔记(二)的更多相关文章
- Python命令模块argparse学习笔记(一)
首先是关于-h/--help参数的设置 description:位于help信息前,可用于描述helpprog:描述help信息中程序的名称epilog:位于help信息后usage:描述程序的用途a ...
- Python命令模块argparse学习笔记(四)
默认参数 ArgumentParser.set_defaults(**kwargs) set_defaults()可以设置一些参数的默认值 >>> parser = argparse ...
- Python命令模块argparse学习笔记(三)
参数组 ArgumentParser.add_argument_group(title=None, description=None) 默认情况下,当显示帮助消息时,ArgumentParser将命令 ...
- python 命令行参数学习(二)
照着例子看看打打,码了就会.写了个命令行参数调用进行运算的脚本. 参考文章链接:http://www.jianshu.com/p/a50aead61319 #-*-coding:utf-8-*- __ ...
- 基于python实现自动化办公学习笔记二
word文件(1)读word文件 import win32comimport win32com.client def readWordFile(path): # 调用系统word功能,可以处理doc和 ...
- python3.4学习笔记(二十四) Python pycharm window安装redis MySQL-python相关方法
python3.4学习笔记(二十四) Python pycharm window安装redis MySQL-python相关方法window安装redis,下载Redis的压缩包https://git ...
- python3.4学习笔记(二十三) Python调用淘宝IP库获取IP归属地返回省市运营商实例代码
python3.4学习笔记(二十三) Python调用淘宝IP库获取IP归属地返回省市运营商实例代码 淘宝IP地址库 http://ip.taobao.com/目前提供的服务包括:1. 根据用户提供的 ...
- python3.4学习笔记(二十六) Python 输出json到文件,让json.dumps输出中文 实例代码
python3.4学习笔记(二十六) Python 输出json到文件,让json.dumps输出中文 实例代码 python的json.dumps方法默认会输出成这种格式"\u535a\u ...
- python3.4学习笔记(二十五) Python 调用mysql redis实例代码
python3.4学习笔记(二十五) Python 调用mysql redis实例代码 #coding: utf-8 __author__ = 'zdz8207' #python2.7 import ...
随机推荐
- 集成Spring web.xml配置总结
1.web.xml 的加载顺序是:ServletContext -> context-param -> listener -> filter -> servlet 1.serv ...
- springcloud一些概念知识
1.Eureka 1)Eureka服务治理体系支持跨平台 2)三个核心概念:服务注册中心.服务提供者以及服务消费者 3)服务续约:注册完服务之后,服务提供者会维护一个心跳来不停的告诉Eureka Se ...
- java深入探究15-SpringMVC
测试代码:链接:http://pan.baidu.com/s/1c1QGYIk 密码:q924 回顾spring+struts web.xml配置;struts核心过滤器;spring监听器-> ...
- java基础(6)-集合类2
泛型 泛型:是一种把类型明确的工作推迟到创建对象或者调用方法的时候才去明确的特殊的类型,参数化类型,把类型当做参数一样的传递 好处: 1)把运行时期的问题提前到了编译器期间 2)避免了强制类型转换 3 ...
- 80X86寄存器详解<转载>
引子 打算写几篇稍近底层或者说是基础的博文,浅要介绍或者说是回顾一些基础知识, 自然,还是得从最基础的开始,那就从汇编语言开刀吧, 从汇编语言开刀的话,我们必须还先要了解一些其他东西, 像 CPU ...
- BZOJ4654/UOJ223 [Noi2016]国王饮水记
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- Python3一些包的下载
首先在windows的Python扩展包网址:http://www.lfd.uci.edu/~gohlke/pythonlibs/ 这里举例下载opencv3.2.0的安装包 我的电脑是win10,6 ...
- 【译】:lxml.etree官方文档
本文翻译自:http://lxml.de/tutorial.html, 作者:Stefan Behnel 这是一个关于使用lxml.etree进行XML处理的教程.它简要介绍了ElementTree ...
- 字符集、字符编码、XML中的中文编码
字符集.字符编码.XML中的中文编码 作为程序员的你是不是对于ASCII .UNICODE.GB2321.UTF-7.UTF-8等等不时出现在你面前的这些有着奇怪意义的词感到很讨厌呢,是不是总觉得好象 ...
- Python 3.5 socket OSError: [Errno 101] Network is unreachable
/******************************************************************************** * Python 3.5 socke ...