-
Notifications
You must be signed in to change notification settings - Fork 6
/
cmd_line_args.py
50 lines (36 loc) · 1.27 KB
/
cmd_line_args.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
"""
usage:
python /home/xuananh/repo/python-note/cmd_line_args.py 8088 -d d -f f -n 1
or
python /home/xuananh/repo/python-note/cmd_line_args.py -d d -f f -n 1 8088
"""
import argparse
def parse_cmdline():
parser = argparse.ArgumentParser(
description="Sample parser command line argument in python")
parser.add_argument("port", nargs="?", type=int, default=8080)
parser.add_argument('-d', '--directory',
metavar='DIRECTORY',
dest='dir',
help='Enter a directory path',
required=True)
parser.add_argument('-f', '--file',
metavar='FILE',
dest='file',
help='Enter a file path',
required=True,
type=str)
parser.add_argument('-n', '--number',
metavar='NUMBER',
dest='number',
help='Enter a number',
required=True,
type=int)
args = parser.parse_args()
return args
if __name__ == '__main__':
args = parse_cmdline()
print(args.dir)
print(args.file)
print(args.number)
print(args.port)