python3 getopt用法
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 <module>
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也没用,长参数是你必须给我值我才可以进行传参