-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path02_test_argparse.py
More file actions
32 lines (24 loc) · 1.08 KB
/
Copy path02_test_argparse.py
File metadata and controls
32 lines (24 loc) · 1.08 KB
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
import argparse
import math
def build_argument_parser():
"""
This function creates an `argparse.Namespace` object named `args` storing
the information passed by the command-line interface when calling the script.
argparse.Namespace:
Simple class used by default by parse_args() to create an object holding attributes and return it.
#### Example
python test_argparse.py -r 12 -H 1
Cylinder volume is 452.389342117
"""
parser = argparse.ArgumentParser(description="Calculate Cylinder Volume")
parser.add_argument('-r', '--radius', type=float, metavar='', required=True, help='Radius of the cylinder')
parser.add_argument('-H', '--height', type=float, metavar='', required=True, help='height of the cylinder')
args = parser.parse_args()
return args
def cylinder_volume(radius, height ):
return math.pi * (radius**2) * height
if __name__ == "__main__":
args = build_argument_parser()
radius, height = args.radius, args.height
volume = cylinder_volume(radius, height)
print("Cylinder volume is {}".format(volume))