-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstd_in_sum_cli.py
59 lines (49 loc) · 1.48 KB
/
std_in_sum_cli.py
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
"""
This script is used to read input from the standard input.
This input are integer separated by space.
There might multiple lines of input.
The resulting output is the sum of all the integers.
"""
import argparse
from sys import stdin, stdout, stderr
from std_in_sum import split_lines, cast_to_integers
def read_lines(file_in) -> list:
"""
Read lines from the standard input
:param file_in: File object with input data
:return: List of lines read from the standard input.
"""
return file_in.read().splitlines()
def main() -> None:
"""
Main function to read the input from the standard input and print the sum of the integers
"""
parser = argparse.ArgumentParser(
description="Sum integers from an input file"
# file argument or stdin
)
parser.add_argument(
"input",
nargs="?",
type=argparse.FileType("r"),
default=stdin,
help="File with integers separated by space, default is stdin"
)
parser.add_argument(
"output",
nargs="?",
type=argparse.FileType("w"),
default=stdout,
help="File with the sum of the integers, default is stdout"
)
args = parser.parse_args()
lines = read_lines(args.input)
literals = split_lines(lines)
integers = cast_to_integers(literals)
print(sum(integers), file=args.output)
if __name__ == "__main__":
try:
main()
except Exception as e:
print(e, file=stderr)
exit(1)