diff --git a/oxrlib/config.py b/oxrlib/config.py index 93eac18298f9cb5ad87781dbd1cf734a64a77438..00f6eb32357acdb37d8105a012353099fd28ab2c 100644 --- a/oxrlib/config.py +++ b/oxrlib/config.py @@ -6,6 +6,7 @@ import os.path import pathlib import babel +import babel.dates import babel.numbers from . import cache, loaders @@ -74,7 +75,7 @@ class Configuration: else: retval -= datetime.timedelta(days=replacements['day']) retval = retval.replace(**replacements) - return retval + return retval, replacements def _build_argparser(self): prog_parser = argparse.ArgumentParser() @@ -191,6 +192,7 @@ class Configuration: return default def _post_hook_historical(self): + self.args.date, date_spec = self.args.date year = self.args.date.year if year < 100: # Don't let the user specify ambiguous dates. @@ -223,6 +225,18 @@ class Configuration: self.args.to_currency = self._convert_or_error(currency_code, next_word) except StopIteration: self.args.to_currency = pref_currency + if ((len(date_spec) == 1) + and self.args.from_currency + and (self.args.amount is None)): + self.error(("ambiguous input: " + "Did you want rates for {args.from_currency} on {date}, " + "or to convert {amt} {args.from_currency} to {args.to_currency}?\n" + "Specify more of the date to disambiguate." + ).format( + args=self.args, + date=babel.dates.format_date(self.args.date), + amt=date_spec['day'], + )) def _build_cache_loader(self): kwargs = dict(self.conffile.items('Cache'))