詳解Python中argpasrse模塊的基本使用


點(diǎn)擊藍(lán)字關(guān)注我們



argpasrse模塊的使用
import argparseparser = argparse.ArgumentParser(prog = 'ls',description='Process some int',add_help = True)parser.add_argument('path',nargs='?', default='.', help='file path')# 位置參數(shù)parser.add_argument('-l',dest='list', action='store_true')parser.add_argument('-a', '--all', action='store_true')args = parser.parse_args() # 解析:如:/etc ---> e t c 把其當(dāng)作一個(gè)可迭代對象了,所以可以這樣輸入 ('/etc',)parser.print_help() # windows 使用這個(gè)調(diào)試print('-------------------------------------')print( args.all, args.list, args.path) #None None /etc
在win下,模擬插入位置參數(shù),如 /etc 可以在run-->Edit configuration下或者在:args = parser.parse_args('/etc')
① 默認(rèn)會(huì)打印幫助信息,默認(rèn)提供 '''
py文件:
import argparseparser = argparse.ArgumentParser(description='Process some int')args = parser.parse_args()parser.print_help() # windows 使用這個(gè)調(diào)試
打印信息:
usage: homework_解析_9-4.py [-h]Process some intoptional arguments:-h, --help show this help message and exit
② ArgumentParser下的內(nèi)容
def __init__(self,prog=None,描述程序的,sys.args[0]也就是輸入的命令, 如:a.pyusage=None,程序使用方式description=None,epilog=None,parents=[],formatter_class=HelpFormatter,prefix_chars='-',fromfile_prefix_chars=None,argument_default=None, 缺省值conflict_handler='error',add_help=True,默認(rèn)的幫助信息,-h,--help Fals表示取消allow_abbrev=True):
③ 取消help
import argparseparser = argparse.ArgumentParser(prog = 'ls', # 給命令起個(gè)名字 lsdescription='Process some int',add_help = False # 改為False)args = parser.parse_args()parser.print_help()?#?windows??使用這個(gè)調(diào)試
打印信息:
usage: lsProcess?some?int
沒有取消的信息:
usage: ls [-h] # 中括號(hào)表示可選,linux特色Process some intoptional arguments:??-h,?--help??show?this?help?message?and?exit
④ 必須提供參數(shù),也就是說執(zhí)行l(wèi)s.py的時(shí)候,后面要跟 路徑 如:/etc
import argparseparser = argparse.ArgumentParser(prog = 'ls',description='Process some int',add_help = True)parser.add_argument('path')#
打印信息:
args = parser.parse_args()usage: ls [-h] pathls:?error:?the?following?arguments?are?required:?path
⑤ 長選項(xiàng)
import argparseparser = argparse.ArgumentParser(prog = 'ls',description='Process some int',add_help = True)parser.add_argument('path')parser.add_argument('-l')parser.add_argument('-a','--all')args?=?parser.parse_args()
打印信息:
usage: ls [-h] [-l L] [-a ALL] pathProcess some intpositional arguments:pathoptional arguments:-h, --help show this help message and exit-l L??-a?ALL,?--all?ALL
⑥ namespace,sys.argv[1:],沒有提供 ,就是None,提供了,則傳到namespace
import argparseparser = argparse.ArgumentParser(prog = 'ls',description='Process some int',add_help = True)parser.add_argument('path')parser.add_argument('-l')parser.add_argument('-a','--all')args = parser.parse_args(('/etc',)) # 解析:如:/etc ---> e t c 把其當(dāng)作一個(gè)可迭代對象了,所以可以這樣輸入 ('/etc',)parser.print_help() # windows 使用這個(gè)調(diào)試print(args)
打印信息:
usage: ls [-h] [-l L] [-a ALL] pathProcess some intpositional arguments:pathoptional arguments:-h, --help show this help message and exit-l L-a ALL, --all ALLNamespace(all=None, l=None, path='/etc')
⑦ 獲取參數(shù)對應(yīng)的屬性的值 ,通過namespace,但是參數(shù)如果有--all這樣的,只能用args.all'''
args = parser.parse_args('/etc'.split()) # 解析:如:/etc ---> e t c 把其當(dāng)作一個(gè)可迭代對象了,所以可以這樣輸入 ('/etc',)print(?args.all,?args.l,?args.path)?#None?None?/etc
打印信息:
None?None?/etc⑧ -l -a 默認(rèn)必須帶參,如何不帶參'''
幫助文檔:
add_argument() method?name or flags - Either a name or a list of option strings, e.g. foo or -f, --foo.?action - The basic type of action to be taken when this argument is encountered at the command line.?nargs - The number of command-line arguments that should be consumed.?const - A constant value required by some action and nargs selections.?default - The value produced if the argument is absent from the command line.?type - The type to which the command-line argument should be converted.?choices - A container of the allowable values for the argument.?required - Whether or not the command-line option may be omitted (optionals only).?help - A brief description of what the argument does.?dest - The name of the attribute to be added to the object returned by parse_args().parser.add_argument('path',nargs='?')# 位置參數(shù)打印:usage: ls [-h] [-l L] [-a ALL] [path] 此時(shí)path 加中括號(hào)了,可有可無parser.add_argument('path',nargs='?', default='.')# 位置參數(shù),加了一個(gè)默認(rèn)值,當(dāng)前目錄#### ? 表示可有可無,+ 至少一個(gè),*可以任意個(gè),數(shù)字表示必須指定數(shù)目parser.add_argument('-l', action='store_true') # 不需要傳參了
打印信息:
usage:?ls?[-h]?[-l]?[-a?ALL]?[path]?????????parser.add_argument('-l', action='store_true')args = parser.parse_args('-l'.split())print(?args.all,?args.l,?args.path)?
打印信息:
因?yàn)閟tore_true,所以給了 -l 則打印true,否則為 false
如果store_false,不給-l 就打印true
parser.add_argument('-l', action='store_const', const=23)args?=?parser.parse_args('-l'.split())?
打印信息:
如果給了,就打印 23,不給,打印None
⑨ 每個(gè)命令提供一個(gè)help信息 '''
parser.add_argument('path',nargs='?',?default='.',?help='file?path')#?位置參數(shù)打印信息:
??path????????file?path⑩ 給namespace的屬性提供一個(gè)方便使用的名字,如args.l 使用args.list'''
parser.add_argument('-l',dest='list', action='store_true')print( args.all, args.list, args.path) #None None /etc
打印信息:
False?False?.?這個(gè)名字只是在namespace中用,不會(huì)影響命令調(diào)用時(shí)的 -l
?? - end -??
微信號(hào) : coding_club
●?掃碼關(guān)注我們
覺得內(nèi)容還不錯(cuò)的話,給我點(diǎn)個(gè)“在看”唄


