詳解Python命令行解析工具Argparse
最近在研究pathon的命令行解析工具,argparse,它是Python標(biāo)準(zhǔn)庫(kù)中推薦使用的編寫命令行程序的工具。
以前老是做UI程序,今天試了下命令行程序,感覺相當(dāng)好,不用再花大把時(shí)間去研究界面問題,尤其是vc++中尤其繁瑣。
現(xiàn)在用python來實(shí)現(xiàn)命令行,核心計(jì)算模塊可以用c自己寫擴(kuò)展庫(kù),效果挺好。
學(xué)習(xí)了argparse,在官方文檔中找到一篇toturial,簡(jiǎn)單翻譯了下。
http://docs.python.org/2/howto/argparse.html#id1
Argparse Tutorial
這篇教程簡(jiǎn)明地介紹了Python標(biāo)準(zhǔn)庫(kù)推薦使用的命令行參數(shù)解析模塊——Argparse的使用。
1、基本概念
在這篇教程中我們通過一個(gè)常見的ls命令來展示argparse的功能。
$ ls cpython devguide prog.py pypy rm-unused-function.patch $ ls pypy ctypes_configure demo dotviewer include lib_pypy lib-python ... $ ls -l total 20 drwxr-xr-x 19 wena wena 4096 Feb 18 18:51 cpython drwxr-xr-x 4 wena wena 4096 Feb 8 12:04 devguide -rwxr-xr-x 1 wena wena 535 Feb 19 00:05 prog.py drwxr-xr-x 14 wena wena 4096 Feb 7 00:59 pypy -rw-r--r-- 1 wena wena 741 Feb 18 01:01 rm-unused-function.patch $ ls --help Usage: ls [OPTION]... [FILE]... List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.
從以上的四個(gè)命令中,我們能夠了解以下幾個(gè)基本概念:
1)、ls命令在沒有參數(shù)的情況下也是可以運(yùn)行的,默認(rèn)打印出當(dāng)前目錄下的所有內(nèi)容。
2)、如果我們想讓它展示更多內(nèi)容,那么我們需要給它多一點(diǎn)參數(shù)。在這種情況下,我們想顯示一個(gè)不同的目錄,pypy。我們所做的就是明確了常見的定位參數(shù)(positional argument),這樣命名是因?yàn)樾枰绦蚋鶕?jù)參數(shù)在命令行中的位置而決定做什么。這個(gè)概念與命令cp更為接近,它的用法是cp src dest,src表示的是你想要拷貝的文件,dest表示你想要將文件拷貝到哪里。
3)、現(xiàn)在,我想要改變程序的行為。在我們的例子中,我想顯示文件的向西信息而不僅僅是文件名,參數(shù)-l 則是我們所知道的可選參數(shù)(optinal argument)
4)、最后一句是顯示幫助的文檔的一個(gè)片段,當(dāng)你遇到你從未使用過的命令時(shí),你可以通過它學(xué)習(xí)怎么使用。
2、基本認(rèn)識(shí)
我們從一個(gè)基本的程序開始(它什么也不做)
import argparse parser = argparse.ArgumentParser() parser.parse_args()
運(yùn)行結(jié)果:
$ python prog.py $ python prog.py --help usage: prog.py [-h] optional arguments: -h, --help show this help message and exit $ python prog.py --verbose usage: prog.py [-h] prog.py: error: unrecognized arguments: --verbose $ python prog.py foo usage: prog.py [-h] prog.py: error: unrecognized arguments: foo
結(jié)果分析:
1)、若不給參數(shù)而運(yùn)行這個(gè)程序,將不會(huì)得到任何結(jié)果。
2)、第二條命名顯示了使用的argparse的好處,你什么也沒做,卻得到了一個(gè)很好的幫助信息。
3)、我們無需人為設(shè)置--help參數(shù),就能得到一個(gè)良好的幫助信息。但是若給其他參數(shù)(比如foo)就會(huì)產(chǎn)生一個(gè)錯(cuò)誤。
3、位置參數(shù)
首先,給一個(gè)例子:
import argparse parser = argparse.ArgumentParser() parser.add_argument("echo") args = parser.parse_args() print args.echo
運(yùn)行結(jié)果:
$ python prog.py usage: prog.py [-h] echo prog.py: error: the following arguments are required: echo $ python prog.py --help usage: prog.py [-h] echo positional arguments: echo optional arguments: -h, --help show this help message and exit $ python prog.py foo foo
結(jié)果分析:
這次,我們?cè)黾恿艘粋€(gè)add_argument()方法,用來設(shè)置程序可接受的命令行參數(shù)。
現(xiàn)在要運(yùn)行程序,就必須設(shè)置一個(gè)參數(shù)。
parse_args()方法實(shí)際上從我們的命令行參數(shù)中返回了一些數(shù)據(jù),在上面的例子中是echo
這個(gè)像“魔法”一樣的過程,是argparse自動(dòng)完成的。
盡管自動(dòng)產(chǎn)生的幫助信息展示地很美觀,但是我們?nèi)匀粺o法只根據(jù)echo這個(gè)參數(shù)知道它是做什么的。所以,我們?cè)黾恿艘恍〇|西,使得它變得更有用。
import argparse parser = argparse.ArgumentParser() parser.add_argument("echo", help="echo the string you use here") args = parser.parse_args() print args.echo
運(yùn)行結(jié)果:
$ python prog.py -h usage: prog.py [-h] echo positional arguments: echo echo the string you use here optional arguments: -h, --help show this help message and exit
在此基礎(chǔ)上,我們?cè)俣喔淖円稽c(diǎn):(計(jì)算輸入?yún)?shù)square的平方)
import argparse parser = argparse.ArgumentParser() parser.add_argument("square", help="display a square of a given number") args = parser.parse_args() print args.square**2
下面是運(yùn)行結(jié)果:
$ python prog.py 4 Traceback (most recent call last): File "prog.py", line 5, in <module> print args.square**2 TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'
這個(gè)程序并不能正確運(yùn)行,因?yàn)閍rgparse會(huì)將輸入當(dāng)作字符串處理,所以我們需要設(shè)置它的類型:(type=int)
import argparse parser = argparse.ArgumentParser() parser.add_argument("square", help="display a square of a given number", type=int) args = parser.parse_args() print args.square**2
下面是運(yùn)行結(jié)果:
$ python prog.py 4 16 $ python prog.py four usage: prog.py [-h] square prog.py: error: argument square: invalid int value: 'four'
現(xiàn)在,這個(gè)程序能夠順利運(yùn)行,而且能夠處理一些錯(cuò)誤輸入。
以上就是關(guān)于Python命令行解析工具Argparse的簡(jiǎn)單使用教程,希望對(duì)大家有幫助。
- python中argparse模塊用法實(shí)例詳解
- Python解析命令行讀取參數(shù)--argparse模塊使用方法
- python argparse傳入布爾參數(shù)false不生效的解決
- Python的argparse庫(kù)使用詳解
- Python命令行解析器argparse詳解
- python argparser的具體使用
- Python參數(shù)解析模塊sys、getopt、argparse使用與對(duì)比分析
- Python編程argparse入門淺析
- Python如何使用argparse模塊處理命令行參數(shù)
- Python argparse模塊使用方法解析
- Python argparse模塊應(yīng)用實(shí)例解析
- python?命令行參數(shù)模塊argparse的實(shí)現(xiàn)
相關(guān)文章
Python運(yùn)行出現(xiàn)DeprecationWarning的問題及解決
這篇文章主要介紹了Python運(yùn)行出現(xiàn)DeprecationWarning的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07Django修改app名稱和數(shù)據(jù)表遷移方案實(shí)現(xiàn)
這篇文章主要介紹了Django修改app名稱和數(shù)據(jù)表遷移方案實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09利用Python通過獲取剪切板數(shù)據(jù)實(shí)現(xiàn)百度劃詞搜索功能
大家是不是嫌棄每次打開百度太麻煩?今天教大家利用Python通過獲取剪切板數(shù)據(jù)實(shí)現(xiàn)百度劃詞搜索功能,用程序直接打開網(wǎng)頁(yè),需要的朋友可以參考下2021-06-06Python使用Phantomjs截屏網(wǎng)頁(yè)的方法
今天小編就為大家分享一篇Python使用Phantomjs截屏網(wǎng)頁(yè)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-05-05十一個(gè)案例帶你吃透Python函數(shù)參數(shù)
這篇文章主要通過十一個(gè)案例帶大家一起了解一下Python中的函數(shù)參數(shù),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python有一定幫助,需要的可以參考一下2022-08-08

Python OpenCV實(shí)現(xiàn)人物動(dòng)漫化效果

Python configparser模塊應(yīng)用過程解析