python channel_builder.py -s /Users/graypn/ -d /Users/graypn/Documents -m 7 --out=report/xx.html
參數也分長格式和短格式
短格式:-s
長格式:--source
opts, args = getopt.getopt(sys.argv[1:], "hs:d:m:v:p:c:",
["help", "src=", "dst=", "major=", "version=", "platform=", "channels="])
這個 opts 是一個字典類型,getopt.getopt(x,y,z) x函數第一個傳入參sys.argv[1:],y第二個傳段short短數名"hs:d:m:v:p:c:",z第三個是long長參數名["help", "src=", "dst=", "major=", "version=", "platform=", "channels="]
short 用法就是 python test.py -h 111
long用法可以2種 --out=res/tt.html
或者直接 --out res/tt.html
關于第二個參數初學者會很困惑這啥意思:
其實是這樣的單獨一個字母表示該參數不帶值,一個字母加個:比如p:表示該參數后面帶值,"hf:-v"也可以寫成'-h-f:-v',到底什么意思呢:
C:\Users\Administrator\PycharmProjects\checkTest\src>python xx.py -h
this is --help
C:\Users\Administrator\PycharmProjects\checkTest\src>python xx.py -f
Traceback (most recent call last):
File "xx.py", line 22, in
opts, args = getopt.getopt(sys.argv[1:], '-hf:-v', ['help', 'filename=', 'version'])
File "C:\Python37\lib\getopt.py", line 95, in getopt
opts, args = do_shorts(opts, args[0][1:], shortopts, args[1:])
File "C:\Python37\lib\getopt.py", line 199, in do_shorts
opt)
getopt.GetoptError: option -f requires argument
C:\Users\Administrator\PycharmProjects\checkTest\src>python xx.py -h
this is --help
C:\Users\Administrator\PycharmProjects\checkTest\src>python xx.py -f=aaaa.txt
[*] Filename is =aaaa.txt
意思就是你短參數不需要給我指定值,給我我也沒發接受處理比如1000短參數給的值1000也沒用,長參數是你必須給我值我才可以進行傳參