需要使用sys和getopt 库
def arg(_,__): #接收命令行参数
import sys
import getopt
'''参数:
_ :短参数str,列如:-f-g-p
__:长参数list,列如:['file','get','post']
返回示例:{'-g': 'aa', '-f': 'aaa', '-p': 'huagsu.py'}
'''
arg = getopt.getopt(sys.argv[1:],_,__)
arg[1].insert(0,arg[0][0][0])
ditcr = {}
for i in range(len(arg[1])):
if(i % 2 == 0):
ditcr[arg[1][i]] = arg[1][i-1]
return ditcr
示例:
命令:python ./Kamita_Tomoe/KamitaTomoe.py -g aaa -f huagsu.py -p aa
>>>arg = arg('-f-g-p',['file','get','post'])
>>>print(arg)
{'-g': 'aa', '-f': 'aaa', '-p': 'huagsu.py'}
标签:__,aa,python,py,参数,命令行,arg,getopt,接收
From: https://www.cnblogs.com/xhsz/p/17065290.html