forked from davidbp/python_tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04_casting_bool.py
More file actions
25 lines (22 loc) · 977 Bytes
/
Copy path04_casting_bool.py
File metadata and controls
25 lines (22 loc) · 977 Bytes
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
import argparse
def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
def parse_commandline():
parser = argparse.ArgumentParser(description="Parse inputs")
parser.add_argument('-n', '--name', type=str, required=True, help='Person name')
parser.add_argument('-a', '--age', type=int, required=False, help='Age of the person.')
parser.add_argument( '-ec', '--ever_convicted', type=str2bool, required=False, help='Boolean stating if the person has been convicted previously.')
args = parser.parse_args()
return args
if __name__ == "__main__":
exp_args = parse_commandline()
print("\n\tPerson name:", exp_args.name)
print("\tAge:", exp_args.age)
print("\tConvicted:", exp_args.ever_convicted)