-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path46-Arguments_getopt.py
36 lines (26 loc) · 1.02 KB
/
46-Arguments_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
'''
author : Jaydatt
The getopt module is a parser for command-line options based on the convention established by the Unix getopt() function. It is in general used for parsing an argument sequence such as sys.argv. In other words, this module helps scripts to parse command-line arguments in sys.argv. It works similar to the C getopt() function for parsing command-line parameters.
'''
import getopt , sys
print('sys.argv : ',sys.argv)
def getData(argv):
try:
opts, args = getopt.getopt(argv[1:], "f:l:")
print('opts : ', opts )
print('args : ', args )
for opt, arg in opts:
if opt in ['-f']:
print('-f : ',arg)
elif opt in ['-l']:
print('-l : ',arg)
except:
print("Error")
getData(sys.argv)
# Command Line PS : D:\Python> py Arguments_getopt.py -f key -l val
# Output :
# sys.argv : ['Arguments_getopt.py', '-f', 'key', '-l', 'val']
# opts : [('-f', 'key'), ('-l', 'val')]
# args : []
# -f : key
# -l : val