python argparser的具体使用

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

python argparser的具体使用

  2021-04-02 我要评论

一.正常运行:

咱们随便写个文件:

# test.py
import argparse

ap = argparse.ArgumentParser()
ap.add_argument('-i', '--image', help='传入图片文件')
args = vars(ap.parse_args())
print(args)

咱们运行一下:

python test.py --image './test.png'
python test.py -i './test.png'

没问题吧:

{'image':'./test.png'}

{'i':'./test.png'}

二.咱们改一下程序:

ap.add_argument('--image', help='传入图片文件')

第一个'-i'参数去掉, 一望而知,只能:

python test.py --image './test.png'

输出:

{'image':'./test.png'}

三.咱们再改一下程序:

ap.add_argument('-i', help='传入图片文件')

第一个'--image'参数去掉, 一望而知,只能:

python test.py -i './test.png'

输出:

{'i':'./test.png'}

也就是说,两个参数任选其一

四.传参数时改一下参数

在只传入一个'--image'的情况下:

ap.add_argument('--image', help='传入图片文件')

我们可以用'--image'、'--imag'、'--ima'、'--im'和'--i'

python test.py --image './test.png'
python test.py --imag './test.png'
python test.py --ima './test.png'
python test.py --im './test.png'
python test.py --i './test.png'

输出都是:

{'image':'./test.png'}

您可能感兴趣的文章:

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们