diff --git a/crawl.py b/crawl.py index ab9dbe9..e070e42 100755 --- a/crawl.py +++ b/crawl.py @@ -82,20 +82,37 @@ d | date: Print the date which the data is from. print(res.date) elif len(argv[0]) == 3 or len(argv[1]) == 3: # more than 3 letter abbreviations are invalid - if is_float(argv[0]): - # amount first -> convert to currency in argv[1] - print(fmt_and_calc( - cur=argv[1].upper(), - amt=float(argv[0]), - res=res, - direction=DIRECTION_FROM_EUR)) + cur_use = str + amt_use = float + dir_use = int + if len(argv) == 1 and not is_float(argv[0]): + # only currency specified + cur_use = argv[0].upper() + amt_use = 1 + dir_use = DIRECTION_TO_EUR + elif is_float(argv[0]): + # amount first -> convert EUR to currency in argv[1] + cur_use = argv[1].upper() + amt_use = float(argv[0]) + dir_use = DIRECTION_FROM_EUR elif is_float(argv[1]): - # currency first -> convert to EUR + # currency first -> convert argv[1] to EUR + cur_use = argv[0].upper() + amt_use = float(argv[1]) + dir_use = DIRECTION_TO_EUR + else: + raise ValueError + + # check that the currency exists + if cur_use not in res.rates: + raise ValueError + else: print(fmt_and_calc( - cur=argv[0].upper(), - amt=float(argv[1]), + cur=cur_use, + amt=amt_use, res=res, - direction=DIRECTION_TO_EUR)) + direction=dir_use + )) else: print("Not implemented: '" + " ".join(argv) + "'") except IndexError: @@ -103,6 +120,8 @@ d | date: Print the date which the data is from. pass else: print("Too few arguments: '" + " ".join(argv) + "'") + except ValueError: + print("The currency specified does not exist.") def is_float(string: str) -> bool: try: