forked from standardebooks/tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
roman2dec
executable file
·36 lines (27 loc) · 862 Bytes
/
roman2dec
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
#!/usr/bin/env python3
import argparse
import sys
import roman
import se
def main():
parser = argparse.ArgumentParser(description="Convert a Roman numeral to a decimal number.")
parser.add_argument("-n", "--no-newline", dest="newline", action="store_false", help="don't end output with a newline")
parser.add_argument("numbers", metavar="NUMERAL", nargs='+', help="a Roman numeral")
args = parser.parse_args()
lines = []
if not sys.stdin.isatty():
for line in sys.stdin:
lines.append(line.rstrip("\n"))
for line in args.numbers:
lines.append(line)
for line in lines:
try:
if args.newline:
print(roman.fromRoman(line.upper()))
else:
print(roman.fromRoman(line.upper()), end="")
except roman.InvalidRomanNumeralError:
se.print_error("Not a Roman numeral: {}".format(line))
exit(1)
if __name__ == "__main__":
main()