Skip to content

Commit 645f47e

Browse files
author
Amogh Singhal
authored
Create argparse_for_cli.py
1 parent fbba6c9 commit 645f47e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

argparse_for_cli.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import argparse
2+
import sys
3+
4+
def main():
5+
parser = argparse.ArgumentParser()
6+
parser.add_argument('--x', type=float, default=1.0,
7+
help='What is the first number?')
8+
parser.add_argument('--y', type=float, default=1.0,
9+
help='What is the second number?')
10+
parser.add_argument('--operation', type=str, default='add',
11+
help='What is the operation(add, sub, mul or div)?')
12+
args = parser.parse_args()
13+
sys.stdout.write(str(calc(args)))
14+
15+
def calc(args):
16+
if args.operation == 'add':
17+
return args.x + args.y
18+
if args.operation == 'sub':
19+
return args.x - args.y
20+
if args.operation == 'mul':
21+
return args.x * args.y
22+
if args.operation == 'div':
23+
return args.x / args.y
24+
25+
if __name__ == '__main__':
26+
main()

0 commit comments

Comments
 (0)