getopt模块
import getopt
import sys
try:
    opts, args = getopt.getopt(sys.argv[1:], 's:d:', ['source=', 'destination='])
except getopt.GetoptError as e:
    sys.exit(3)
# check if the parameters are valid
for o, a in opts:
    if o in ('-s', '--source'):
        source_address = a
    elif o in ('-d', '--destination')
        destination_address = a
    else:
        print('Error: INVALID parameters.')
1.
s:d:即表示的是短参数,即使用-s或者-d2.['source=', 'destination=']即表示的是长参数,即使用--source=或者--destination=
可以实现像平时的脚本附带参数的命令,如:
python getopt_test.py -s 192.168.1.1 -d 1.1.1.1
或者:
python getopt_test.py --source=192.168.1.1 --destination=1.1.1.1
1.
opts中存放的是[('-s', '192.168.1.1'), ('-d', '1.1.1.1')]或者是[('--source', '192.168.1.1'), ('--destination', '1.1.1.1')]2.args存放的是[]
