-
Notifications
You must be signed in to change notification settings - Fork 0
/
8_getopt.py
51 lines (41 loc) · 913 Bytes
/
8_getopt.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/python
from __future__ import print_function
import getopt
import sys
class user_operations(object):
va=''
vb=''
def __init__(this):
pass
def __init__(this, va='', vb=''):
this.va = va
this.vb = vb
return
def __str__(this):
return 'va:'+this.va+' vb:'+this.vb
def usage(argv):
print("Usage: %s [-h | --help] [--a va] [--b vb]"%argv[0])
return
def main(argc, argv):
if argc <= 1:
usage(argv)
return 0
try:
opts, args = getopt.getopt(argv[1:], 'h', ['help', 'a=', 'b='])
#raise getopt.GetoptError, "error"
except getopt.GetoptError:
print("Invalid argument", file=sys.stderr);
return -1
user_opt = user_operations()
for opt, arg in opts:
if opt in ('-h', '--help'):
usage(argv)
return 0
if opt in '--a':
user_opt.va = arg
if opt in '--b':
user_opt.vb = arg
print(user_opt)
return 0
if __name__ == "__main__":
main(len(sys.argv), sys.argv)