Python 中 argparse简单使用
来源:互联网 发布:linux内核usb驱动架构 编辑:程序博客网 时间:2024/06/05 22:40
参考网站
argparse模块主要用来解析参数
一般从这里开始,但是这段代码什么都不做,因为parser.parse_args()返回的是一个命名空间,而现在没有添加参数
import argparseparser = argparse.ArgumentParser()parser.parse_args()
而这段代码添加了一个参数’echo’,所以当在控制台输入参数时就可以用类似Java中 ‘.’的方式访问你的参数了:
import argparseparser = argparse.ArgumentParser()parser.add_argument("echo")args = parser.parse_args()print('print my argument: ',args.echo)
E:\examples of coding\python\shiyanlou>python argparse_test.py my_inputprint my argument: my_input
来一个高级点的。。。:
import argparseparser = argparse.ArgumentParser()parser.add_argument("square", type = int, help = 'display a square of a given number')# 这里type = int表示只可以输入整数parser.add_argument('-v', '--verbose', action = 'store_true', help = 'increase out verbosity')args = parser.parse_args()answer = args.square**2if args.verbose: # 如果输入了verbose参数 print("the square of {} equals {}".format(args.square, answer))else: # 如果没有输入verbose参数 print(answer)
E:\examples of coding\python\shiyanlou>python argparse_test.py 4 -vthe square of 4 equals 16E:\examples of coding\python\shiyanlou>python argparse_test.py -v 4the square of 4 equals 16E:\examples of coding\python\shiyanlou>python argparse_test.py 416E:\examples of coding\python\shiyanlou>python argparse_test.py 4 --verbosethe square of 4 equals 16
还可以限制取值:
import argparseparser = argparse.ArgumentParser()parser.add_argument("square", type = int, help = 'display a square of a given number')# 这里type = int表示只可以输入整数parser.add_argument("-v", "--verbosity", type=int, choices=[0, 1, 2], help = 'increase out verbosity')# 其中verbosity 只可以取值为0,1,2args = parser.parse_args()answer = args.square**2if args.verbosity == 2: # 如果输入的verbosity参数为2 print("the square of {} equals {}".format(args.square, answer))elif args.verbosity == 1: # 如果输入的verbosity参数为1 print("{}^2 == {}".format(args.square, answer))else: # 如果没有输入verbosity参数 print(answer)
E:\examples of coding\python\shiyanlou>python argparse_test.py 4 -v 2the square of 4 equals 16E:\examples of coding\python\shiyanlou>python argparse_test.py 4 -v 14^2 == 16E:\examples of coding\python\shiyanlou>python argparse_test.py 4 -v 016E:\examples of coding\python\shiyanlou>python argparse_test.py 4 -v 3usage: argparse_test.py [-h] [-v {0,1,2}] squareargparse_test.py: error: argument -v/--verbosity: invalid choice: 3 (choose from 0, 1, 2)
还可以定义action=’count’, 统计对应关键字出现的次数:
import argparseparser = argparse.ArgumentParser()parser.add_argument("square", type = int, help = 'display a square of a given number')# 这里type = int表示只可以输入整数parser.add_argument("-v", "--verbosity", action = 'count', default=0, help = 'increase out verbosity')# 其中verbosity 出现的次数默认为0,可以计数args = parser.parse_args()answer = args.square**2if args.verbosity >= 2: # 如果输入的verbosity参数为2 print("the square of {} equals {}".format(args.square, answer))elif args.verbosity == 1: # 如果输入的verbosity参数为1 print("{}^2 == {}".format(args.square, answer))else: # 如果没有输入verbosity参数 print(answer)
E:\examples of coding\python\shiyanlou>python argparse_test.py 4 -vvthe square of 4 equals 16E:\examples of coding\python\shiyanlou>python argparse_test.py 4 -vvvthe square of 4 equals 16E:\examples of coding\python\shiyanlou>python argparse_test.py 4 --verbosity --verbositythe square of 4 equals 16E:\examples of coding\python\shiyanlou>python argparse_test.py 4 -v4^2 == 16E:\examples of coding\python\shiyanlou>python argparse_test.py 416
更多高级argparse知识
阅读全文
0 0
- Python 中 argparse简单使用
- Python中argparse模块简单使用
- python argparse模块的简单使用
- Python中argparse模块的使用
- python中argparse库的使用
- Python-OpenCV中argparse的使用
- python中argparse
- python argparse使用示例
- Python argparse使用
- Python使用argparse命令行选项计算文件中单词个数
- Python 中Argparse 语句介绍
- Python中argparse模块学习
- python Argparse模块的使用
- python Argparse模块的使用
- python Argparse模块的使用
- python Argparse模块的使用
- python 命令行参数解析 argparse简单分析
- python 命令行参数解析 argparse简单分析
- Group Anagrams
- [AHK]简版堆栈实现
- JavaScript中promise的基本用法
- 深入理解线程安全和可重入函数
- 基于jfreechart的图表生成方法
- Python 中 argparse简单使用
- Python中的join()函数的用法
- 深入理解HBase的系统架构
- java中Class对象详解
- jQuery选择器大全
- maven update后jdk变成1.5问题
- hibernate sql查询转化为实体类
- stm32_GPIO模拟I2c读写EEPROM
- hdu 2822 Dogs