-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathprog56_Command_Line_Utility.py
More file actions
32 lines (23 loc) · 994 Bytes
/
prog56_Command_Line_Utility.py
File metadata and controls
32 lines (23 loc) · 994 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
26
27
28
29
30
31
32
# Creating a Command Line Utility In Python
# to run this program open command prompt and goto inside this file path folder
# type python prog56_Command_Line_Utility.py --x 23 --y 20 --o add
import argparse
import sys
def calc(args):
if args.o == 'add':
return args.x + args.y
elif args.o == 'sub':
return args.x - args.y
elif args.o == 'mul':
return args.x * args.y
elif args.o == 'div':
return args.x / args.y
else:
return "Something went wrong !!!"
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--x', type=float, default=1.0, help="First argument should be any number.")
parser.add_argument('--y', type=float, default=1.0,help="Second argument should be any number." )
parser.add_argument('--o', type=str, default="add", help="This is operator for calculation.")
args = parser.parse_args()
sys.stdout.write(str(calc(args)))