-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_commandline.py
More file actions
59 lines (45 loc) · 1.75 KB
/
add_commandline.py
File metadata and controls
59 lines (45 loc) · 1.75 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""
Author:
Michael Shaw
Background:
The goal of this program is to be able to add two values from the command line.
The program is named add_commandline. It expects two inputs and prints their sum.
Usage:
Command line: python add_commandline.py <input1> <input2>
Spyder: runfile('add_commandline.py', args='<input1> <input2>')
"""
import sys
import ast
def add_command_line_args(arg1, arg2):
"""
Adds two values passed as command line arguments.
Args:
arg1 (str): The string representation of the first argument.
arg2 (str): The string representation of the second argument.
Returns:
str: A formatted string reporting the types and the result of the addition.
"""
try:
# Safely evaluate the command-line arguments as Python literals
value1 = ast.literal_eval(arg1)
value2 = ast.literal_eval(arg2)
# Add the two inputs together
result = value1 + value2
# Return the formatted output string
return f"{type(value1)} + {type(value2)} results in {type(result)} with value {result}"
except (ValueError, SyntaxError) as e:
# Return an error message if evaluation fails
return f"Error: {e}"
def main():
# Check if exactly two additional arguments have been provided
if len(sys.argv) != 3:
print("Usage: python add_cml.py <input1> <input2>")
sys.exit(1) # Exit with a non-zero exit code to indicate an error
# Retrieve the arguments
arg1, arg2 = sys.argv[1], sys.argv[2]
# Perform the addition and get the result or an error message
output = add_command_line_args(arg1, arg2)
# Print the result or an error message
print(output)
if __name__ == '__main__':
main()