完善一下 python 脚本,让它支持更完善的参数(长参数、短参数)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import getopt, sys

def parse_args(args=sys.argv[1:]):
try:
opts, args = getopt.getopt(args, "hvc:w:", ["help", "short", "verbose", "critical_num=", "warning_num="])
except getopt.GetoptError as err:
print("Input parameter error")
sys.exit()

verbose = False
short = False
for opt, arg in opts:
if opt in ("-h", "--help"):
print("""
useage:
-h --help : print help
-v --verbose : show dmesg oom kill log
-c --critical_num : How many times is called a critical
-w --warning_num : How many times is called a warning
""")
sys.exit()
elif opt in ("-c", "--critical_num"):
critical_num = arg
elif opt in ("-w", "--warning_num"):
warning_num = arg
elif opt in ("-s"):
short = True
elif opt in ("-v"):
verbose = True

return warning_num, critical_num, short, verbose

if __name__ == '__main__':
print(parse_args())