這篇教程簡明地介紹了python標準庫推薦使用的命令列引數解析模組——argparse的使用。
基本概念
在這篇教程中我們通過乙個常見的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 20drwxr-xr-x 19 wena wena 4096 feb 18 18:51cpython
drwxr-xr-x 4 wena wena 4096 feb 8 12:04devguide
-rwxr-xr-x 1 wena wena 535 feb 19 00:05 prog.py
drwxr-xr-x 14 wena wena 4096 feb 7 00:59pypy
-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.
基本認識
我們從乙個基本的程式開始(它什麼也不做)
import執行結果:argparse
parser =argparse.argumentparser()
parser.parse_args()
$ 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
位置引數
首先,給乙個例子:
import執行結果:argparse
parser =argparse.argumentparser()
parser.add_argument(
"echo")
args =parser.parse_args()
print args.echo
$ 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
儘管自動產生的幫助資訊展示地很美觀,但是我們仍然無法只根據echo這個引數知道它是做什麼的。所以,我們增加了一些東西,使得它變得更有用。
import執行結果:argparse
parser =argparse.argumentparser()
parser.add_argument(
"echo
", help="
echo the string you use here")
args =parser.parse_args()
print args.echo
$ python prog.py -h在此基礎上,我們再多改變一點:(計算輸入引數square的平方)usage: prog.py [-h] echo
positional arguments:
echo
echo the string you use
here
optional arguments:
-h, --help show this help message and exit
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
$ python prog.py 4這個程式並不能正確執行,因為argparse會將輸入當作字串處理,所以我們需要設定它的型別:(type=int)traceback (most recent call
last
): file
"prog.py
", line 5, in
print args.square**2
typeerror: unsupported operand type(s)
for ** or pow(): '
str' and '
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
$ python prog.py 4現在,這個程式能夠順利執行,而且能夠處理一些錯誤輸入。16$ python prog.py four
usage: prog.py [-h] square
prog.py: error: argument square: invalid int value: 'four'
python 命令列解析工具argparse的認識
一 介紹 argparse 是python 中用於解析命令列引數和選項的標準模組。簡單的形容就是你寫完python程式之後,在終端下 linux系統 可以用命令列直接呼叫執行,並且可以設定相應的引數等等。二 如何使用 說太多廢話無用,我們需要掌握的是如何使用,能看懂別人寫的 即可。太多的理論知識作用...
Python 命令列解析工具 Argparse介紹
最近在研究pathon的命令列解析工具,argparse,它是python標準庫中推薦使用的編寫命令列程式的工具。以前老是做ui程式,今天試了下命令列程式,感覺相當好,不用再花大把時間去研究介面問題,尤其是vc 中尤其繁瑣。現在用python來實現命令列,核心計算模組可以用c自己寫擴充套件庫,效果挺...
學習 Python學習(一) Python問答
一.為什麼要使用python?python的主要特點有 1 軟體質量,python 具有很強的可讀性,因此在重用和維護方面就比較方便 2 編碼效率,python沒有編譯和鏈結庫的過程 3 程式移植性,不做任何修改,python可執行在windows和linux系統 4 豐富的支撐庫,python既可...