python argparser的具體使用
一.正常運行:
咱們隨便寫個文件:
# 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'參數(shù)去掉, 一望而知,只能:
python test.py --image './test.png'
輸出:
{'image':'./test.png'}
三.咱們再改一下程序:
ap.add_argument('-i', help='傳入圖片文件')
第一個'--image'參數(shù)去掉, 一望而知,只能:
python test.py -i './test.png'
輸出:
{'i':'./test.png'}
也就是說,兩個參數(shù)任選其一
四.傳參數(shù)時改一下參數(shù)
在只傳入一個'--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'}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python 實現(xiàn)數(shù)據(jù)庫更新腳本的生成方法
下面小編就為大家?guī)硪黄狿ython 實現(xiàn)數(shù)據(jù)庫更新腳本的生成方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07python爬蟲之urllib庫常用方法用法總結(jié)大全
urllib是python自帶的請求庫,各種功能相比較之下也是比較完備的,下面這篇文章主要給大家介紹了關(guān)于python爬蟲之urllib庫常用方法用法的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2018-11-11python3用PIL把圖片轉(zhuǎn)換為RGB圖片的實例
今天小編就為大家分享一篇python3用PIL把圖片轉(zhuǎn)換為RGB圖片的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07