argparse - 命令行選項與參數解析(譯)Mar 30, 2013
原文:argparse – Command line option and argument parsing
譯者:youngsterxyf
argparse模塊作為optparse的一個替代被添加到Python2.7。argparse的實現支持一些不易於添加到optparse以及要求向后不兼容API變化的特性,因此以一個新模塊添加到標准庫。
與optparse相比較
argparse的API類似於optparse,甚至在很多情況下通過更新所使用的類名和方法名,使用argparse作為一個簡單的替代。然而,有些地方在添加新特性時不能保持直接兼容性。
你必須視情況決定是否升級已有的程序。如果你已編寫了額外的代碼以彌補optparse的局限,也許你想升級程序以減少你需要維護的代碼量。若argparse在所有部署平台上都可用,那么新的程序應盡可能使用argparse。
設置一個解析器
使用argparse的第一步就是創建一個解析器對象,並告訴它將會有些什么參數。那么當你的程序運行時,該解析器就可以用於處理命令行參數。
解析器類是 ArgumentParser 。構造方法接收幾個參數來設置用於程序幫助文本的描述信息以及其他全局的行為或設置。
import argparse
parser = argparse.ArgumentParser(description='This is a PyMOTW sample program')
定義參數
argparse是一個全面的參數處理庫。參數可以觸發不同的動作,動作由 add_argument() 方法的 action 參數指定。 支持的動作包括保存參數(逐個地,或者作為列表的一部分),當解析到某參數時保存一個常量值(包括對布爾開關真/假值的特殊處理),統計某個參數出現的次數,以及調用一個回調函數。
默認的動作是保存參數值。在這種情況下,如果提供一個類型,那么在存儲之前會先把該參數值轉換成該類型。如果提供 dest 參數,參數值就保存為命令行參數解析時返回的命名空間對象中名為該 dest 參數值的一個屬性。
解析一個命令行
定義了所有參數之后,你就可以給 parse_args() 傳遞一組參數字符串來解析命令行。默認情況下,參數是從 sys.argv[1:]
中獲取,但你也可以傳遞自己的參數列表。選項是使用GNU/POSIX語法來處理的,所以在序列中選項和參數值可以混合。
parse_args() 的返回值是一個命名空間,包含傳遞給命令的參數。該對象將參數保存其屬性,因此如果你的參數 dest
是 "myoption"
,那么你就可以args.myoption
來訪問該值。
簡單示例
以下簡單示例帶有3個不同的選項:一個布爾選項(-a
),一個簡單的字符串選項(-b
),以及一個整數選項(-c
)。
import argparse
parser = argparse.ArgumentParser(description='Short sample app')
parser.add_argument('-a', action="store_true", default=False)
parser.add_argument('-b', action="store", dest="b")
parser.add_argument('-c', action="store", dest="c", type=int)
print parser.parse_args(['-a', '-bval', '-c', '3'])
有幾種方式傳遞值給單字符選項。以上例子使用了兩種不同的形式,-bval
和-c val
。
$ python argparse_short.py
Namespace(a=True, b='val', c=3)
在輸出中與'c'
關聯的值是一個整數,因為程序告訴ArgumentParser在保存之前先轉換該參數。
“長”選項名字,即選項的名字多於一個字符,以相同的方式進行處理。
import argparse
parser = argparse.ArgumentParser(description='Example with long option names')
parser.add_argument('--noarg', action="store_true", default=False)
parser.add_argument('--witharg', action="store", dest="witharg")
parser.add_argument('--witharg2', action="store", dest="witharg2", type=int)
print parser.parse_args(['--noarg', '--witharg', 'val', '--withargs=3'])
結果也類似:
$ python argparse_long.py
Namespace(noarg=True, witharg='val', witharg2=3)
argparse區別於optparse的一個地方是對非選項參數值的處理。optparse只進行選項解析,而argparse是一個全面的命令行參數解析工具,也處理非選項參數。
import argparse
parser = argparse.ArgumentParser(description='Example with non-optional arguments')
parser.add_argument('count', action="store", type=int)
parser.add_argument('units', action="store")
print parser.parse_args()
在這個例子中,“count”參數是一個整數,“units”參數存儲為一個字符串。其中任意一個參數若沒有在命令行中提供,或給定的值不能被轉換為正確的類型,就會報告一個錯誤。
$ python argparse_arguments.py 3 inches
Namespace(count=3, units='inches')
$ python argparse_arguments.py some inches
usage: argparse_arguments.py [-h] count units
argparse_arguments.py: error: argument count: invalid int value: 'some'
$ python argparse_arguments.py
usage: argparse_arguments.py [-h] count units
argparse_arguments.py: error: too few arguments
參數動作
argparse內置6種動作可以在解析到一個參數時進行觸發:
store
保存參數值,可能會先將參數值轉換成另一個數據類型。若沒有顯式指定動作,則默認為該動作。
store_const
保存一個被定義為參數規格一部分的值,而不是一個來自參數解析而來的值。這通常用於實現非布爾值的命令行標記。
store_ture
/store_false
保存相應的布爾值。這兩個動作被用於實現布爾開關。
append
將值保存到一個列表中。若參數重復出現,則保存多個值。
append_const
將一個定義在參數規格中的值保存到一個列表中。
version
打印關於程序的版本信息,然后退出
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-s', action='store', dest='simple_value',
help='Store a simple value')
parser.add_argument('-c', action='store_const', dest='constant_value',
const='value-to-store',
help='Store a constant value')
parser.add_argument('-t', action='store_true', default=False,
dest='boolean_switch',
help='Set a switch to true')
parser.add_argument('-f', action='store_false', default=False,
dest='boolean_switch',
help='Set a switch to false')
parser.add_argument('-a', action='append', dest='collection',
default=[],
help='Add repeated values to a list')
parser.add_argument('-A', action='append_const', dest='const_collection',
const='value-1-to-append',
default=[],
help='Add different values to list')
parser.add_argument('-B', action='append_const', dest='const_collection',
const='value-2-to-append',
help='Add different values to list')
parser.add_argument('--version', action='version', version='%(prog)s 1.0')
results = parser.parse_args()
print 'simple_value =', results.simple_value
print 'constant_value =', results.constant_value
print 'boolean_switch =', results.boolean_switch
print 'collection =', results.collection
print 'const_collection =', results.const_collection
$ python argparse_action.py -h
usage: argparse_action.py [-h] [-s SIMPLE_VALUE] [-c] [-t] [-f]
[-a COLLECTION] [-A] [-B] [--version]
optional arguments:
-h, --help show this help message and exit
-s SIMPLE_VALUE Store a simple value
-c Store a constant value
-t Set a switch to true
-f Set a switch to false
-a COLLECTION Add repeated values to a list
-A Add different values to list
-B Add different values to list
--version show program's version number and exit
$ python argparse_action.py -s value
simple_value = value
constant_value = None
boolean_switch = False
collection = []
const_collection = []
$ python argparse_action.py -c
simple_value = None
constant_value = value-to-store
boolean_switch = False
collection = []
const_collection = []
$ python argparse_action.py -t
simple_value = None
constant_value = None
boolean_switch = True
collection = []
const_collection = []
$ python argparse_action.py -f
simple_value = None
constant_value = None
boolean_switch = False
collection = []
const_collection = []
$ python argparse_action.py -a one -a two -a three
simple_value = None
constant_value = None
boolean_switch = False
collection = ['one', 'two', 'three']
const_collection = []
$ python argparse_action.py -B -A
simple_value = None
constant_value = None
boolean_switch = False
collection = []
const_collection = ['value-2-to-append', 'value-1-to-append']
$ python argparse_action.py --version
argparse_action.py 1.0
選項前綴
argparse選項的默認語法是基於Unix約定的,使用一個“-”前綴來表示命令行開關。argparse支持其他前綴,因此你可以使得你的程序遵照本地平台的默認語法(例如,在Window上使用“/”)或者遵循不同的約定。
import argparse
parser = argparse.ArgumentParser(description='Change the option prefix charaters',
prefix_chars='-+/')
parser.add_argument('-a', action="store_false", default=None,
help='Turn A off')
parser.add_argument('+a', action="store_true", default=None,
help='Turn A on')
parser.add_argument('//noarg', '++noarg', action="store_true", default=False)
print parser.parse_args()
將ArgumentParser 方法的prefix_chars 參數設置為一個字符串,該字符串包含所有允許用來表示選項的字符。需要理解的是雖然prefix_chars包含允許用於開關的字符,但單個參數定義只能使用一種給定的開關語法。這讓你可以對使用不同前綴的選項是否是別名(比如獨立於平台的命令行語法的情況)或替代選擇(例如,使用“+”表明打開一個開發,“-”則為關閉一個開關)進行顯式地控制。在上述例子中,+a
和-a
是不同的參數,//noarg
也可以 ++noarg
提供,但不是 --noarg
。
$ python argparse_prefix_chars.py -h
usage: argparse_prefix_chars.py [-h] [-a] [+a] [//noarg]
Change the option prefix characters
optional arguments
-h, --help show this help message and exit
-a Turn A off
+a Turn A on
//noarg,++noarg
$ python argparse_prefix_chars.py +a
Namespace(a=True, noarg=False)
$ python argparse_prefix_chars.py -a
Namespace(a=False, noarg=False)
$ python argparse_prefix_chars.py //noarg
Namespace(a=None, noarg=True)
$ python argparse_prefix_chars.py ++noarg
Namespace(a=None, noarg=True)
$ python argparse_prefix_chars.py --noarg
usage: argparse_prefix_chars.py [-h] [-a] [+a] [//noarg]
argparse_prefix_chars.py: error: unrecognized arguments: --noarg
參數來源
目前為止所見的例子中,提供給解析器的參數列表來自於顯式傳遞的一個列表,或隱式地從sys.argv獲取的。顯式傳遞列表在你使用argparse來處理類命令行但並不是來自命令行(比如來自一個配置文件)的指令之時比較有用。
import argparse
from ConfigParser import ConfigParser
import shlex
parser = argparse./span>ArgumentParser(description='Short sample app') parser.add_argument('-a', action="store_true", default=False) parser.add_argument('-b', action="store", dest="b") parser.add_argument('-c', action="store", dest="c", type=int) config = ConfigParser() config.read('argparse_witH_shlex.ini') config_value = config.get('cli', 'options') print 'Config: ', config_value argument_list = shlex.split(config_value) print 'Arg List:', argument_list print 'Results:', parser.parse_args(argument_list)
shlex使得切分存儲在配置文件中的字符串非常容易。
$ python argparse_with_shlex.py
Config: -a -b 2
Arg List: ['-a', '-b', '2']
Results: Namespace(a=True, b='2', c=None)
另一種自己處理配置文件的方法是使用fromfile_prefix_chars指定一個包含一組要待處理參數的輸入文件來告訴argparse怎樣識別參數。
import argparse
from ConfigParser import ConfigParser
import shlex
parser = argparse.ArgumentParser(description='Short sample app',
fromfile_prefix_chars='@'
)
parser.add_argument('-a', action="store_true", default=False)
parser.add_argument('-b', action="store", dest="b")
parser.add_argument('-c', action="store", dest="c", type=int)
print parser.parse_args(['@argparse_fromfile_prefix_chars.txt'])
該示例代碼在找到一個以@為前綴的參數時即停止往下讀取,然后從以該參數命名的文件中查找更多的參數。例如,輸入文件argparse_fromfile_prefix_chars.txt
包含一系列參數,一行一個:
-a
-b
2
那么處理該文件產生的輸出為:
$ python argparse_fromfile_prefix_chars.py
Namespace(a=True, b='2', c=None)
自動生成選項
經過配置argparse會自動添加選項用來生成幫助信息以及為你的應用程序顯示版本信息。
ArgumentParser的參數add_help 控制幫助信息相關的選項。
import argparse
parser = argparse.ArgumentParser(add_help=True)
parser.add_argument('-a', action="store_true", default=False)
parser.add_argument('-b', action="store", dest="b")
parser.add_argument('-c', action="store", dest="c", type=int)
print parser.parse_args()
幫助選項(-h和–help)默認是添加的,但可以通過將add_help設置為false來禁用。
import argparse
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument('-a', action="store_true", default=False)
parser.add_argument('-b', action="store", dest="b")
parser.add_argument('-c', action="store", dest="c", type=int)
print parser.parse_args()
雖然-h
和--help
是事實上的請求幫助的標准選項名稱,但一些應用或argparse的使用要么不需要提供幫助要么需要將這兩個選項名稱用於其他目標。
$ python argparse_with_help.py -h
usage: argparse_with_help.py [-h] [-a] [-b B] [-c C]
optional arguments:
-h, --help show this help message and exit
-a
-b B
-c C
$ python argparse_without_help.py -h
usage: argparse_without_help.py [-a] [-b B] [-c C]
argparse_without_help.py: error: unrecognized arguments: -h
當在ArgumentParser構造方法設置版本后,就會添加版本選項(-v
和--version
)。
import argparse
parser = argparse.ArgumentParser(version='1.0')
parser.add_argument('-a', action="store_true", default=False)
parser.add_argument('-b', action="store", dest="b")
parser.add_argument('-c', action="store", dest="c", type=int)
print parser.parse_args()
print 'This is not printed'
兩種形式的選項愛那個都會打印程序的版本字符串,然后立即退出程序。
$ python argparse_with_version.py -h
usage: argparse_with_version.py [-h] [-v] [-a] [-b B] [-c C]
optional arguments:
-h, --help show this help message and exit
-v, --version show program's version number and exit
-a
-b B
-c C
$ python argparse_with_version.py -v
1.0
解析器組
argparse包含若干特性用於組織你的參數解析器,使得實現更為簡單,也能提高輸出幫助信息的可用性。
共享解析器規則
我們常常需要實現一套命令行程序,這些程序都帶一組參數,只是在某些方面有特殊化。例如,如果所有程序都需要在用戶進行任何實際的操作之前對用戶進行認證,那么它們就都需要支持--user
和--password
選項。你可以共享的選項來定義一個“父母”解析器,然后令單個程序的解析器從該“父母”解析器繼承共享選項,這樣就不必顯式為每個ArgumentParser添加共享選項。
第一步是以共享的參數定義建立“父母”解析器。由於“父母”解析器的后代使用者會添加相同的幫助選項,從而會引發一個異常,所以在基礎解析器中我們關閉自動幫助選項生成。
import argparse
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument('--user', action="store")
parser.add_argument('--password', action="store")
接下來,以父母解析器集創建另一個解析器:
import argparse
import argparse_parent_base
parser = argparse.ArgumentParser(parents=[argparse_parent_base.parser])
parser.add_argument('--local-arg', action="store_true", default=False)
print parser.parse_args()
得到的程序帶有三個選項:
$ python argparse_uses_parent.py -h
usage: argparse_uses_parent.py [-h] [--user USER] [--password PASSWORD]
[--local-arg]
optional arguments:
-h, --help show this help message and exit
--user USER
--password PASSWORD
--local-arg
沖突的選項
前一個例子指出以相同的參數名字為一個解析器添加兩個參數處理器會引發一個異常。可以通過傳遞一個conflict_handler來改變沖突消除行為。argparse有兩個內置的沖突處理器error
(默認)和resolve
,resolve
會基於沖突選項的添加順序來選擇一個參數處理器。
import argparse
parser = argparse.ArgumentParser(conflict_handler='resolve')
parser.add_argument('-a', action="store")
parser.add_argument('-b', action="store", help="Short alone")
parser.add_argument('--long-b', '-b', action="store", help="Long and short together")
print parser.parse_args(['-h'])
由於最后一個處理器所給定的參數名已被使用,那么本例中獨立選項-b
將被--long-b
的別名所覆蓋。
$ python argparse_conflict_handler_resolve.py
usage: argparse_conflict_handler_resolve.py [-h] [-a A] [--long-b LONG_B]
optional arguments:
-h, --help show this help message and exit
-a A
--long-b LONG_B, -b LONG_B
Long and short together
切換add_argument()的調用順序就可以啟用獨立的選項:
import argparse
parser = argparse.ArgumentParser(conflict_handler='resolve')
parser.add_argument('-a', action="store")
parser.add_argument('--long-b', '-b', action="store", help='Long and short together')
parser.add_argument('-b', action="store", help='Short alone')
print parser.parse_args([-h])
現在兩個選項可以一起使用了。
$ python argparse_conflict_handler_resolve2.py
usage: argparse_conflict_handler_resolve2.py [-h] [-a A] [--long-b LONG_B] [-b B]
optional arguments:
-h, --help show this help message and exit
-a A
--long-b LONG_B Long and short together
-b B Short alone
參數群組
argparse能將參數定義組合成“群組”。默認情況下是使用兩個群組,一個是選項的群組,另一個是必須的與位置相關的參數群組。
import argparse
parser = argparse.ArgumentParser(description='Short sample app')
parser.add_argument('--optional', action="store_true", default=False)
parser.add_argument('positional', action="store")
print parser.parse_args()
群組在輸出的幫助信息中顯示為分開的“與位置相關的參數”和“可選參數”兩個部分:
$ python argparse_default_grouping.py
usage: argparse_default_grouping.py [-h] [--optional] positional
Short sample app
positional arguments:
positional
optional arguments:
-h, --help show this help message and exit
--optional
你可以調整群組來提高幫助信息中群組的邏輯性,這樣相關選項或值能記錄在一起。可以使用自定義群組來重寫之前的共享選項的示例,如此在幫助信息中身份認證的選項就可以顯示在一起。
在基礎解析器中使用add_argument_group()來創建一個“身份認證”群組,然后逐個添加身份認證相關的選項到該群組。
import argparse
parser = argparser.ArgumentParser(add_help=False)
group = parser.add_argument_group('authentication')
group.add_argument('--user', action="store")
group.add_argument('--password', action="store")
與之前一樣,程序使用基於群組的父母解析器列表作為parents的值。
import argparse
import argparse_parent_with_group
parser = argparse.ArgumentParser(parents=[argparse_parent_with_group.parser])
parser.add_argument('--local-arg', action="store_true", default=False)
print parser.parse_args()
現在輸出的幫助信息一起顯示身份認證選項。
$ python argparse_uses_parent_with_group.py -h
usage: argparse_uses_parent_with_group.py [-h] [--user USER] [--password PASSWORD] [--local-arg]
optional arguments:
-h, --help show this message and exit
--local-arg
authentication:
--user USER
--password PASSWORD
互斥選項
定義互斥的選項是選項分組特性的一個特例,使用add_mutually_exclusive_group()而不是add_argument_group()。
import argparse
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
group.add_argument('-a', action='store_true')
group.add_argument('-b', action="store_true")
print parser.parse_args()
argparse會為你強制執行互斥性,因此一次使用僅能給出該群組的選項中的一個。
$ python argparse_mutually_exclusive.py -h
usage: argparse_mutually_exclusive.py [-h] [-a | -b]
optional arguments:
-h, --help show this help message and exit
-a
-b
$ python argparse_mutually_exclusive.py -a
Namespace(a=True, b=False)
$ python argparse_mutually_exclusive.py -b
Namespace(a=False, b=True)
$ python argparse_mutually_exclusive.py -a -b
usage: argparse_mutually_exclusive.py [-h] [-a | -b]
argparse_mutually_exclusive.py: error: argument -b: not allowed with argument -a
嵌套解析器
上述的父母解析器方式是在相關命令之間共享選項的方式之一。另一種方式是將多個命令組合進一個程序中,使用子解析器來處理命令行的每個部分。就像svn
,hg
,以及其他帶有多個命令行行為或子命令的程序那樣。
一個用於處理文件系統目錄的程序可能會像這樣定義命令用於創建、刪除、以及列出一個目錄的內容:
import argparse
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(help='commands')
# A list command
list_parser = subparsers.add_parser('list', help='List contents')
list_parser.add_argument('dirname', action='store', help='Directory to list')
# A create command
create_parser = subparsers.add_parser('create', help='Create a directory')
create_parser.add_argument('dirname', action='store', help='New directory to create')
create_parser.add_argument('--read-only', default=False, action='store_true',
help='Set permissions to prevent writing to the directory')
# A delete command
delete_parser = subparsers.add_parser('delete', help='Remove a directory')
delete_parser.add_argument('dirname', action='store', help='The directory to remove')
delete_parser.add_argument('--recursive', '-r', default=False, action='store_true',
help='Remove the contents of the directory, too')
print parser.parse_args()
輸出的幫助信息顯示作為“命令”的命名子解析器能夠在命令行中作為位置參數進行指定。
$ python argparse_subparsers.py -h
usage: argparse_subparsers.py [-h] {list, create, delete} ...
positional arguments:
{list, create, delete} commands
list List contents
create Create a directory
delete Remove a directory
optional arguments:
-h, --help show this help message and exit
每個子解析器也有自己的幫助信息,描述那個命令的參數和選項。
$ python argparse_subparsers.py create -h
usage: argparse_subparsers.py create [-h] [--read-only] dirname
positional arguments:
dirname New directory to create
optional arguments:
-h, --help show this help message and exit
--read-only Set permissions to prevent writing to the directory
參數被解析后,parse_args()返回的Namespace對象僅包含與指定的命令相關的值。
$ python argparse_subparsers.py delete -r foo
Namespace(dirname='foo', recursive=True)
高級參數處理
至今為止的示例展示了簡單的布爾標識、字符串或數字參數選項、以及位置參數。對於變長參數列表、枚舉類型數據、以及常量,argparse支持復雜的參數規格。
可變形參列表
你可以配置單個參數的定義使其能夠匹配所解析的命令行的多個參數。根據需要或期望的參數個數,設置nargs為這些標識值之一:
值 含義
N 參數的絕對個數(例如:3)
? 0或1個參數
* 0或所有參數
+ 所有,並且至少一個參數
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--three', nargs=3)
parser.add_argument('--optional', nargs='?')
parser.add_argument('--all', nargs='*', dest='all')
parser.add_argument('--one-or-more', nargs='+')
print parser.parse_args()
解析器強制執行參數計數指令,生成一個精確的語法圖作為命令幫助文本的一部分。
$ python argparse_nargs.py -h
usage: argparse_nargs.py [-h] [--three THREE THREE THREE]
[--optional [OPTIONAL]] [--all [ALL [ALL ...]]]
[--one-or-more ONE_OR_MORE [ONE_OR_MORE ...]]
optional arguments:
-h, --help show this help message and exit
--three THREE THREE THREE
--optional [OPTIONAL]
--all [ALL [ALL ...]]
--one-or-more ONE_OR_MORE [ONE_OR_MORE ...]
$ python argparse_nargs.py
Namespace(all=None, one_or_more=None, optional=None, three=None)
$ python argparse_nargs.py --three
usage: argparse_nargs.py [-h] [--three THREE THREE THREE]
[--optional [OPTIONAL]] [--all [ALL [ALL ...]]]
[--one-or-more ONE_OR_MORE [ONE_OR_MORE ...]]
argparse_nargs.py: error: argument --three: expected 3 argument(s)
$ python argparse_nargs.py --three a b c
Namespace(all=None, one_or_more=None, optional=None, three=['a', 'b', 'c'])
$ python argparse_nargs.py --optional
Namespace(all=None, one_or_more=None, optional=None, three=None)
$ python argparse_nargs.py --optional with_value
Namespace(all=None, one_or_more=None, optional='with_value', three=None)
$ python argparse_nargs.py --all with multiple values
Namespace(all=['with', 'multiple', 'values'], one_or_more=None, optional=None, three=None)
$ python argparse_nargs.py --one-or-more with_value
Namespace(all=None, one_or_more=['with_value'], optional=None, three=None)
$ python argparse_nargs.py --one-or-more with multiple values
Namespace(all=None, one_or_more=['with', 'multiple', 'values'], optional=None, three=None)
$ python argparse_nargs.py --one-or-more
usage: argparse_nargs.py [-h] [--three THREE THREE THREE]
[--optional [OPTIONAL]] [--all [ALL [ALL ...]]]
[--one-or-more ONE_OR_MORE [ONE_OR_MORE ...]]
argparse_nargs.py: error: argument --one-or-more: expected at least one argument
參數類型
argparse將所有參數值都看作是字符串,除非你告訴它將字符串轉換成另一種數據類型。add_argument()的type參數以一個轉換函數作為值,被ArgumentParser用來將參數值從一個字符串轉換成另一種數據類型。
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-i', type=int)
parser.add_argument('-f', type=float)
parser.add_argument('--file', type=file)
try:
print parser.parse_args()
except IOError, msg:
parser.error(str(msg))
任何需要單個字符串參數的可調用對象都可以傳遞給type,包含內置類型如int(), float(), 以及file()。
$ python argparse_type.py -i 1
Namespace(f=None, file=None, i=1)
$ python argparse_type.py -f 3.14
Namespace(f=3.14, file=None, i=None)
$ python argparse_type.py --file argparse_type.py
Namespace(f=None, file=<open file 'argparse_type.py', mode 'r' at 0x1004de270>, i=None)
如果類型轉換失敗,argparse會引發一個異常。TypeError和ValueError會被自動捕獲,並為用戶轉換為一個簡單的錯誤消息。其他異常,如下面一個例子中輸入文件不存在,則其IOError必須由調用者來處理。
$ python argparse_type.py -i a
usage: argparse_type.py [-h] [-i I] [-f F] [--file FILE]
argparse_type.py: error: argument -i: invalid int value: 'a'
$ python argparse_type.py -f 3.14.15
usage: argparse_type.py [-h] [-i I] [-f F] [--file FILE]
argparse_type.py: error: argument -f: invalid float value: '3.14.15'
$ python argparse_type.py --file does_not_exist.txt
usage: argparse_type.py [-h] [-i I] [-f F] [--file FILE]
argparse_type.py: error: [Errno 2] No such file or directory: 'does_not_exist.txt'
要想將一個輸入參數限制為一個預定義集中的某個值,則使用choices參數。
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--mode', choices=('read-only', 'read-write'))
print parser.parse_args()
如果--mode
的參數值不是所允許的值中的一個,就會產生一個錯誤並停止執行。
$ python argparse_choices.py -h
usage: argparse_choices.py [-h] [--mode {read-only,read-write}]
optional arguments:
-h, --help show this help message and exit
--mode {read-only,read-write}
$ python argparse_choices.py --mode read-only
Namespace(mode='read-only')
$ python argparse_choices.py --mode invalid
usage: argparse_choices.py [-h] [--mode {read-only,read-write}]
argparse_choices.py: error: argument --mode: invalid choice: 'invalid'
(choose from 'read-only', 'read-write')
文件參數
雖然文件對象可以單個字符串參數值來實例化,但並不允許你指定訪問模式。FileType讓你能夠更加靈活地指定某個參數應該是個文件,包括其訪問模式和緩沖區大小。
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-i', metavar='in-file', type=argparse.FileType('rt'))
parser.add_argument('-o', metavar='out-file', type=argparse.FileType('wt'))
try:
results = parser.parse_args()
print 'Input file:', results.i
print 'Output file:', results.o
except IOError, msg:
parser.error(str(msg))
上例中與參數名關聯的值是一個打開文件句柄。在使用完該文件后應自己負責關閉該文件。
$ python argparse_FileType.py -h
usage: argparse_FileType.py [-h] [-i in-file] [-o out-file]
optional arguments:
-h, --help show this help message and exit
-i in-file
-o out-file
$ python argparse_FileType.py -i argparse_FileType.py -o temporary_file.\
txt
Input file: <open file 'argparse_FileType.py', mode 'rt' at 0x1004de270>
Output file: <open file 'temporary_file.txt', mode 'wt' at 0x1004de300>
$ python argparse_FileType.py -i no_such_file.txt
usage: argparse_FileType.py [-h] [-i in-file] [-o out-file]
argparse_FileType.py: error: argument -i: can't open 'no_such_file.txt': [Errno 2] No such file or directory: 'no_such_file.txt'
自定義動作
除了前面描述的內置動作之外,你也可以提供一個實現了Action API的對象來自定義動作。作為action傳遞給add_argument()的對象應接受描述所定義形參的實參,並返回一個可調用對象,作為parser的實參來處理形參,namespace存放解析的結果、參數值,以及觸發動作的option_string。
argparse提供了一個Action類作為要定義的新動作的基類。構造方法是處理參數定義的,所以你只要在子類中覆蓋call()。
import argparse
class CustomAction(argparse.Action):
def __init__(self,
option_strings,
dest,
nargs=None,
const=None,
default=None,
type=None,
choices=None,
required=False,
help=None,
metavar=None):
argparse.Action.__init__(self,
option_strings=option_strings,
dest=dest,
nargs=nargs,
const=const,
default=default,
type=type,
choices=choices,
required=required,
help=help,
metavar=metavar)
print
print 'Initializing CustomAction'
for name,value in sorted(locals().items()):
if name == 'self' or value is None:
continue
print ' %s = %r' % (name, value)
return
def __call__(self, parser, namespace, values, option_string=None):
print
print 'Processing CustomAction for "%s"' % self.dest
print ' parser = %s' % id(parser)
print ' values = %r' % values
print ' option_string = %r' % option_string
# Do some arbitrary processing of the input values
if isinstance(values, list):
values = [ v.upper() for v in values ]
else:
values = values.upper()
# Save the results in the namespace using the destination
# variable given to our constructor.
setattr(namespace, self.dest, values)
parser = argparse.ArgumentParser()
parser.add_argument('-a', action=CustomAction)
parser.add_argument('-m', nargs='*', action=CustomAction)
parser.add_argument('positional', action=CustomAction)
results = parser.parse_args(['-a', 'value', '-m' 'multi-value', 'positional-value'])
print
print results
values的類型取決於nargs的值。如果該參數允許多個值,則values會是一個列表,即使其僅包含一個列表項。
option_string的值也取決於原有的參數規范。對於位置相關的、必需的參數,option_string始終為None。
$ python argparse_custom_action.py
Initializing CustomAction
dest = 'a'
option_strings = ['-a']
required = False
Initializing CustomAction
dest = 'm'
nargs = '*'
option_strings = ['-m']
required = False
Initializing CustomAction
dest = 'positional'
option_strings = []
required = True
Processing CustomAction for "a"
parser = 4299616464
values = 'value'
option_string = '-a'
Processing CustomAction for "m"
parser = 4299616464
values = ['multi-value']
option_string = '-m'
Processing CustomAction for "positional"
parser = 4299616464
values = 'positional-value'
option_string = None
Namespace(a='VALUE', m=['MULTI-VALUE'], positional='POSITIONAL-VALUE')