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 ...
随机推荐
- 【转】nodejs mysql 链接数据库集群
1.建立数据库连接:createConnection(Object)方法 该方法接受一个对象作为参数,该对象有四个常用的属性host,user,password,database.与php中 ...
- Spring初学之FactoryBean配置Bean
实体bean: Car.java: package spring.beans.factorybean; public class Car { private String name; private ...
- 使用Jenkins进行持续集成
首先,我们从Jenkins官方网站https://jenkins.io/下载最新的war包.虽然Jenkins提供了Windows.Linux.OS X等各种安装程序,但是,这些安装程序都没有war包 ...
- codeforces 777C.Alyona and Spreadsheet 解题报告
题目链接:http://codeforces.com/problemset/problem/777/C 题目意思:给出一个 n * m 的矩阵,然后问 [l, r] 行之间是否存在至少一列是非递减序列 ...
- js:for循环ul/li,获取当前被点击元素的id,以及给其他li设置属性
js:for循环ul/li,获取当前被点击元素的id,以及给其他li设置属性 <!doctype html> <html> <head> <meta char ...
- java:jsp: 一个简单的自定义标签 tld
java:jsp: 一个简单的自定义标签 tld 请注意,uri都是:http://www.tag.com/mytag,保持统一,要不然报错,不能访问 tld文件 <?xml version=& ...
- unity监测按下键的键值并输出+unity键值
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using U ...
- Codeforces 610D Vika and Segments 线段树+离散化+扫描线
可以转变成上一题(hdu1542)的形式,把每条线段变成宽为1的矩形,求矩形面积并 要注意的就是转化为右下角的点需要x+1,y-1,画一条线就能看出来了 #include<bits/stdc++ ...
- CMMI 3级精简并行过程综述
“精简并行过程”(Simplified Parallel Process,SPP)是基于CMMI以及软件工程和项目管理知识而创作的一种“软件过程改进方法和规范”,它由众多的过程规范和文档模板组成.SP ...
- kylin_异常_01_java.io.FileNotFoundException: /developer/apache-kylin-2.3.0-bin/tomcat/conf/.keystore
一.异常现象 kylin安装完,启动后,控制正常,kylin后台也能正常访问.但是去看kylin的日志,却发现报错了: SEVERE: Failed to load keystore type JKS ...