python add argument list_python模块介绍- argparse:命令行选项及参数解析
#承接軟件自動(dòng)化實(shí)施與培訓(xùn)等gtalk:ouyangchongwu#gmail.com qq 37391319 博客: http://blog.csdn.net/oychw
#版權(quán)所有,轉(zhuǎn)載刊登請(qǐng)來(lái)函聯(lián)系
#自動(dòng)化測(cè)試和python群組: http://groups.google.com/group/automation_testing_python
#參考資料:《The Python Standard Library by Example》
#實(shí)驗(yàn)環(huán)境:Python 2.7.3 CentOS release 6.2(Final) 32bits
14.3 argparse:命令行選項(xiàng)及參數(shù)解析
作用:命令行選項(xiàng)及參數(shù)解析。
Python版本:2.7及以后版本
argparse模塊于Python2.7引入,旨在替換optparse。
14.3.1建立解析器
import argparse
parser = argparse.ArgumentParser(
description=’This is a PyMOTW sample program’,
)
argparse是一個(gè)完整的參數(shù)處理庫(kù)。參數(shù)可以根據(jù)add_argument()的action選項(xiàng)觸發(fā)不同action。支持的action有存儲(chǔ)參數(shù)(單個(gè),或作為列表的一部分);存儲(chǔ)常量的值(對(duì)布爾開(kāi)關(guān)true/false有特殊處理)。默認(rèn)動(dòng)作是存儲(chǔ)參數(shù)值。支持type(指定存儲(chǔ)類(lèi)型)和dest(指定存儲(chǔ)變量)等參數(shù)。
然后使用函數(shù)parse_args()進(jìn)行參數(shù)解析,這個(gè)函數(shù)的輸入默認(rèn)是sys.argv[1:],也可以使用其他字符串列表。選項(xiàng)使用GNU/POSIX語(yǔ)法處理,可以混合選項(xiàng)和參數(shù)值。parse_args的返回值是一個(gè)包含命令參數(shù)的Namespace。所有參數(shù)以屬性的形式存在,比如args.myoption。
下面是一個(gè)簡(jiǎn)單的示例:
import argparse
parser = argparse.ArgumentParser(description='Short sampleapp')
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'])
執(zhí)行結(jié)果:
# pythonargparse_short.py
Namespace(a=True, b='val', c=3)
長(zhǎng)參數(shù)也可以進(jìn)行同樣處理:
import argparse
parser = argparse.ArgumentParser(
description='Examplewith 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', '--witharg2=3' ]
)
執(zhí)行結(jié)果:
# python argparse_long.py
Namespace(noarg=True, witharg='val', witharg2=3)
不同于optparse,argparse可以很好地處理非可選參數(shù)(沒(méi)有’-‘等開(kāi)頭的參數(shù)):
import argparse
parser = argparse.ArgumentParser(
description='Examplewith nonoptional arguments',
)
parser.add_argument('count', action="store",type=int)
parser.add_argument('units', action="store")
print parser.parse_args()
沒(méi)有指定類(lèi)型的,默認(rèn)是字符串。執(zhí)行結(jié)果:
# python argparse_arguments.py 3inches
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 intvalue: 'some'
# python argparse_arguments.py
usage: argparse_arguments.py [-h] count units
argparse_arguments.py: error: too few arguments
參數(shù)action有:
store:默認(rèn)action模式,存儲(chǔ)值到指定變量。
store_const:存儲(chǔ)值在參數(shù)的const部分指定,多用于實(shí)現(xiàn)非布爾的命令行flag。
store_true / store_false:布爾開(kāi)關(guān)。可以2個(gè)參數(shù)對(duì)應(yīng)一個(gè)變量。
append:存儲(chǔ)值到列表,該參數(shù)可以重復(fù)使用。
append_const:存儲(chǔ)值到列表,存儲(chǔ)值在參數(shù)的const部分指定。
version 輸出版本信息然后退出。
下面是各種action的示例:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-s', action='store',
dest='simple_value',
help='Storea 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??? = %r' % results.simple_value
print 'constant_value? = %r' % results.constant_value
print 'boolean_switch? = %r' % results.boolean_switch
print 'collection????? = %r' % results.collection
print 'const_collection = %r' % results.const_collection
執(zhí)行結(jié)果:
# 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 --version
argparse_action.py 1.0
# 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']
ArgumentParser函數(shù)中的選項(xiàng)prefix_chars可以指定前綴。默認(rèn)使用UNIX風(fēng)格,命令行使用‘-’作為前綴。可以使用windows的’/’或者其他符號(hào)。
import argparse
parser = argparse.ArgumentParser(
description='Changethe option prefix characters',
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()
執(zhí)行結(jié)果:
root@SZX-SRV-AUTOMATION argparse]# pythonargparse_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
# python argparse_prefix_chars.py --noarg
usage: argparse_prefix_chars.py [-h] [-a] [+a] [//noarg]
argparse_prefix_chars.py: error: unrecognized arguments:--noarg
處理配置文件中的參數(shù):
mport argparse
from ConfigParser import ConfigParser
import shlex
parser = argparse.ArgumentParser(description='Short sampleapp')
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)
執(zhí)行結(jié)果:
# python argparse_with_shlex.py
Config? : -a -b 2
Arg List: ['-a', '-b', '2']
Results : Namespace(a=True, b='2', c=None)
其中ini文件的內(nèi)容如下:
# vi argparse_with_shlex.ini
[cli]
options = -a -b 2
上面例子使用了ConfigParser來(lái)讀取配置,再用shlex來(lái)切割參數(shù)。可以通過(guò)fromfile_prefix_chars 告知argparse輸入?yún)?shù)為文件。
import argparse
#from ConfigParser import ConfigParser
#import shlex
parser = argparse.ArgumentParser(description='Short sampleapp',
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'])
執(zhí)行結(jié)果:
# python argparse_fromfile_prefix_chars.py
Namespace(a=True, b='2', c=None)
其中argparse_fromfile_prefix_chars.txt文件的內(nèi)容如下:
# vi argparse_fromfile_prefix_chars.txt
-a
-b
2
14.3.2自動(dòng)生成選項(xiàng)
Argparse會(huì)自動(dòng)生成的幫助和版本信息。ArgumentParser的add_help參數(shù)控制幫助的生成,默認(rèn)是開(kāi)啟。
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()
下例就關(guān)閉幫助:
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()
執(zhí)行結(jié)果:
$ 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
版本也可以進(jìn)行類(lèi)似的配置:
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’
執(zhí)行結(jié)果:
$ 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
$ python argparse_with_version.py --version
1.0
14.3.3組織解析器
公共解析器:通過(guò)父子類(lèi)來(lái)實(shí)現(xiàn)。見(jiàn)argparse_parent_base.py:
import argparse
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument('--user', action="store")
parser.add_argument('--password', action="store")
子類(lèi):
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()
注意:父類(lèi)關(guān)閉了help。子類(lèi)卻默認(rèn)開(kāi)啟了help。執(zhí)行結(jié)果:
# python argparse_uses_parent.py -h
usage: argparse_uses_parent.py [-h] [--user USER] [--passwordPASSWORD]
[--local-arg]
optional arguments:
-h, --help?????????? show this help message and exit
--user USER
--password PASSWORD
--local-arg
參數(shù)分組:默認(rèn)有可選參數(shù)和必選參數(shù)組。前面的用戶(hù)名和密碼就可以分組:
argparse_parent_with_group.py
import argparse
parser = argparse.ArgumentParser(add_help=False)
group = parser.add_argument_group(’authentication’)
group.add_argument(’--user’, action="store")
group.add_argument(’--password’, action="store")
子類(lèi):
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()
執(zhí)行結(jié)果:
# 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 help message and exit
--local-arg
authentication:
--user USER
--password PASSWORD
使用add_mutually_exclusive_group()可以添加互斥選項(xiàng):
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()
執(zhí)行結(jié)果:
# 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: notallowed with argument –a
嵌套解析:
import argparse
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(help='commands')
# A list command
list_parser = subparsers.add_parser(
'list', help='Listcontents')
list_parser.add_argument(
'dirname',action='store',
help='Directory tolist')
# A create command
create_parser = subparsers.add_parser(
'create',help='Create a directory')
create_parser.add_argument(
'dirname',action='store',
help='New directoryto create')
create_parser.add_argument(
'--read-only',default=False, action='store_true',
help='Setpermissions 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 thecontents of the directory, too',
)
print parser.parse_args()
執(zhí)行結(jié)果:
# 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 thedirectory
# python argparse_subparsers.py delete -r foo
Namespace(dirname='foo', recursive=True)
14.3.4 高級(jí)參數(shù)處理
可變參數(shù):數(shù)字N代表N的參數(shù),?0或者1個(gè)參數(shù)。*0或者多個(gè)參數(shù)。+1或者多個(gè)參數(shù)。
import argparse
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(help='commands')
# A list command
list_parser = subparsers.add_parser(
'list', help='Listcontents')
list_parser.add_argument(
'dirname',action='store',
help='Directory tolist')
# A create command
create_parser = subparsers.add_parser(
'create',help='Create a directory')
create_parser.add_argument(
'dirname',action='store',
help='New directoryto create')
create_parser.add_argument(
'--read-only',default=False, action='store_true',
help='Setpermissions 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 thecontents of the directory, too',
)
print parser.parse_args()
執(zhí)行結(jié)果:
# 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 THREETHREE
--optional [OPTIONAL]
--all [ALL [ALL ...]]
--one-or-moreONE_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 3argument(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
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 atleast one argument
參數(shù)類(lèi)型:
importargparse
parser = argparse.ArgumentParser()
parser.add_argument('-i', type=int)
parser.add_argument('-f', type=float)
parser.add_argument('--file', type=file)
try:
printparser.parse_args()
except IOError, msg:
parser.error(str(msg))
執(zhí)行結(jié)果:
$ 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 -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 -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可以指定參數(shù)的選項(xiàng):
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--mode', choices=('read-only','read-write'))
print parser.parse_args()
執(zhí)行結(jié)果:
# 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')
argparse.FileType可以指定文件的模式和buffer:
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 'Outputfile:', results.o
except IOError, msg:
parser.error(str(msg))
執(zhí)行結(jié)果:
# 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 -otmp_file.txt
Input file:
Output file:
# 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:
自定義action是argparse.Action的子類(lèi)可以處理add_argument中的參數(shù)定義相關(guān)的參數(shù),并返回一個(gè)可調(diào)用對(duì)象。構(gòu)造函數(shù)會(huì)處理參數(shù)定義,僅僅需要處理__call__函數(shù)。__call__函數(shù)中parser代表解釋器,namespace用于返回解釋結(jié)果,value為要處理的參數(shù),option_string用于觸發(fā)action(對(duì)可選參數(shù),永遠(yuǎn)是None。
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'Initializing CustomAction'
for name,valuein 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'Processing CustomAction for "%s"' % self.dest
print ' parser = %s' % id(parser)
print '? values = %r' % values
print '? option_string = %r' % option_string
# Do somearbitrary processing of the input values
ifisinstance(values, list):
values = [v.upper() for v in values ]
else:
values =values.upper()
# Save theresults in the namespace using the destination
# variable givento our constructor.
setattr(namespace, self.dest, values)
parser = argparse.ArgumentParser()
parser.add_argument('-a', action=CustomAction)
parser.add_argument('-m', nargs='*', action=CustomAction)
results = parser.parse_args(['-a', 'value',
'-m', 'multivalue',
'second'])
print results
執(zhí)行結(jié)果:
# python argparse_custom_action.py
Initializing CustomAction
dest = 'a'
option_strings =['-a']
required = False
Initializing CustomAction
dest = 'm'
nargs = '*'
option_strings =['-m']
required = False
Processing CustomAction for "a"
parser = 3076247052
values = 'value'
option_string = '-a'
Processing CustomAction for "m"
parser = 3076247052
values =['multivalue', 'second']
option_string = '-m'
Namespace(a='VALUE', m=['MULTIVALUE', 'SECOND'])
總結(jié)
以上是生活随笔為你收集整理的python add argument list_python模块介绍- argparse:命令行选项及参数解析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: int 为什么是2147483647_2
- 下一篇: redis同步效率秒_redis过期策略