Python-argparse模块

argueparase模块

 一个可执行文件或脚本都可以接收参数。

$ ls  -la /etc/
/etc/ 是位置参数
-l 是短选项参数

如果把参数传递给程序?

从Python3.2开始提供了功能强大的参数分析库,argparse。

参数分类

参数分为:

  位置参数,参数放在那里就需要一个对应的位置参数,例如/etc/就是一个参数位置。

  选项参数,必须通过前面是-的短选项或者--的长选项,然后后面的才算该选项的参数,当然选项后面也可以没有参数。

上例中,/etc/对应的位置参数,-l是选项参数。

ls -lah src

基本解析

import argparse

parser = argparse.ArgumentParser() #获得一个参数解析器

args = parser.parse_args() #分析参数

parser.print_help() #打印帮助

#输出
usage: argparse-参数解析.py [-h]

optional arguments:
  -h, --help  show this help message and exit

Process finished with exit code 0

argparse不仅仅是做了参数的定义和解析,还自动帮忙生成了帮助信息,尤其是usage,可以看到现在定义的参数是否是自己想要的。

解析器的参数

参数名称    说明

prog      程序的名字,缺省用sys.argv[0]的basename

add_help      自动为解析器增加-h和--help选项,默认为True

description   为程序添加描述信息

import argparse

parser = argparse.ArgumentParser(
    prog = 'ls',
    description='List information about the FILEs (the current directory by default)',
    add_help = True
)
parser.add_argument('path',nargs='?',default='.',help='path help')
parser.add_argument('-l',action='store_true',help='long list format')
parser.print_help()

args = parser.parse_args()
print(args)
print(args.path)




#输出
$ python argparse-参数解析.py --help 
usage: ls [-h] [-l] [path]

List information about the FILEs (the current directory by default)

positional arguments:
  path        path help

optional arguments:
  -h, --help  show this help message and exit
  -l          long list format
usage: ls [-h] [-l] [path]

List information about the FILEs (the current directory by default)

positional arguments:
  path        path help

optional arguments:
  -h, --help  show this help message and exit
  -l          long list format

位置参数解析

ls基本功能应该解决目录内容的打印。

打印的时候应该指定目录路径,需要位置参数。

import argparse

parser = argparse.ArgumentParser(prog='ls',add_help='True',description='list directory contents') #生成解析器
parser.add_argument('path') #添加参数

args = parser.parse_args() #分析解析器
parser.print_help() #打印帮助信息


#输出
usage: ls [-h] path
ls: error: the following arguments are required: path
程序定义为:
ls [-h] path
-h 为帮助选线,可有可无
path 为位置参数,必须提供

传参

parser.parse_args(args=None,namespace=None)

args参数列表,一个可迭代对象。内部会把可迭代对象转换成列表,如果None则使用命令行传入参数,非None则使用args参数的可迭代对象。

 

 非必须位置参数

上面的代码必须输入位置参数,否则会报错。

但有时候,使用ls命令不需要输入任何路径的话就表示当前目录下的文件列表,可使用默认参数。

可以看出path位置参数变成了可选的位置参数,没有提供就使用默认值.号表示当前路径。

help     表示帮助文档中这个参数的描述

nargs  表示这个参数的接收结果

  ?表示可有可无

  * 表示人一个

  + 至少一个

  数据表示指定数目个

default表示如果不提供该参数,就使用这个值,一般和?+ *号配合使用,因为它们都可以不提供位置参数,不提供就使用缺省值。

import argparse

parser = argparse.ArgumentParser(prog='ls',add_help='True',description='list dierctory contents') #获得一个参数解析器
parser.add_argument('path',nargs='?',default='.',help='directory') #添加参数,位置参数,可有可无,缺省,帮助
parser.add_argument('-l',action='store_true',dest='longformat',help='use a long listing format')
parser.add_argument('-a','--all',action='store_true',help='show all files,do not ignore enteries starting with.')

# args = parser.parse_args()
parser.print_help()

args = parser.parse_args('-l -a /tmp'.split())
print(args)

#输出

usage: ls [-h] [-l] [-a] [path]

list dierctory contents

positional arguments:
  path        directory

optional arguments:
  -h, --help  show this help message and exit
  -l          use a long listing format
  -a, --all   show all files,do not ignore enteries starting with.
Namespace(all=True, longformat=True, path='/tmp')

参数都是namespace对象的属性,如果想指定这些属性名称,可以使用dest = 'longformat'

猜你喜欢

转载自www.cnblogs.com/alrenn/p/12968661.html
今日推荐