-
Notifications
You must be signed in to change notification settings - Fork 0
/
4_python_scripts_argparse.py
33 lines (26 loc) · 1.12 KB
/
4_python_scripts_argparse.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
import argparse
def using_keyword_args(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")
if __name__ == '__main__':
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--name', type=str,
required=True,
help='A name')
parser.add_argument('--id', type=str,
required=True,
help='the id of the user')
parser.add_argument('--age', type=str,
help='the age of the user')
parser.add_argument('--email', type=str,
default="[email protected]",
help='the email of the user')
args = parser.parse_args()
using_keyword_args(name=args.name,
id=args.id,
age=args.age,
email=args.email)
# run from command line
# python 4_python_scripts_argparse.py --help
# python 4_python_scripts_argparse.py --name Adrian --id 32
# python 4_python_scripts_argparse.py --name Adrian --id 32 --age 39 --email [email protected]