一.正常運行:
咱們隨便寫個文件:
# 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'}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。