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知识

原创粉丝点击