-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathargparse_1.py
More file actions
31 lines (24 loc) · 1.18 KB
/
Copy pathargparse_1.py
File metadata and controls
31 lines (24 loc) · 1.18 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 # Why: Handles command-line args, avoids manual parsing
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Simple calculator for basic arithmetic operations.') # Why: Shows help/usage for users
parser.add_argument("number1", help='First number', type=int) # Why: Forces user to provide an int
parser.add_argument("number2", help='Second number', type=int) # Why: Ensures both numbers are present
parser.add_argument("operation", help='Operation (add, subtract, multiply)', choices=["add", "subtract", "multiply"]) # Why: Prevents invalid operations
args = parser.parse_args() # Why: Converts input to usable vars
print(f'Number 1: {args.number1}')
print(f'Number 2: {args.number2}')
print(f'Operation: {args.operation}')
n1 = args.number1
n2 = args.number2
result = None
# Why: Simple branching, avoids eval/injection
if args.operation == 'add':
result = n1 + n2
elif args.operation == 'subtract':
result = n1 - n2
elif args.operation == 'multiply':
result = n1 * n2
else:
print(f'Invalid operation: {args.operation}')
exit(1)
print(f'Result: {result}')