diff --git a/oxrlib/config.py b/oxrlib/config.py index e4db88d4e17ae2f96dc95327d8081c35b6c28f41..af712addd2b5a4253abf39d473ac6bac713b3141 100644 --- a/oxrlib/config.py +++ b/oxrlib/config.py @@ -57,7 +57,11 @@ class Configuration: ) subparsers = prog_parser.add_subparsers() - hist_parser = subparsers.add_parser('historical', aliases=['hist']) + hist_parser = subparsers.add_parser( + 'historical', aliases=['hist'], + usage='%(prog)s historical YYYY-MM-DD [[amount] code] [[in] code]', + help="Show a currency conversion or rate from a past date", + ) hist_parser.set_defaults( command='historical', amount=None, @@ -70,12 +74,24 @@ class Configuration: ) hist_parser.add_argument( 'date', - type=date_from('%Y-%m-%d'), metavar='YYYY-MM-DD', + type=date_from('%Y-%m-%d'), + help="Use rates from this date, in YYYY-MM-DD format" + ) + hist_parser.add_argument( + 'word1', nargs='?', metavar='first code', + help="Convert or show rates from this currency, in three-letter code format. " + "If not specified, show all rates on the given date.", ) hist_parser.add_argument( - 'remainder', - nargs=argparse.REMAINDER, + 'word2', nargs='?', metavar='amount', + help="Convert this amount of currency. If not specified, show rates.", ) + hist_parser.add_argument( + 'word3', nargs='?', metavar='second code', + help="Convert to this currency, in three-letter code format. " + "If not specified, defaults to the base currency.", + ) + hist_parser.add_argument('word4', nargs='?', help=argparse.SUPPRESS) return prog_parser @@ -101,25 +117,21 @@ class Configuration: if self.args.base is None: self.args.base = self.conffile.get('Historical', 'base') self.args.to_currency = self.args.base - remain_len = len(self.args.remainder) - if (remain_len > 3) and (self.args.remainder[2].lower() in self.PREPOSITIONS): - del self.args.remainder[2] - remain_len -= 1 - if remain_len == 0: + if self.args.word4 and (self.args.word3.lower() in self.PREPOSITIONS): + self.args.word3 = self.args.word4 + if self.args.word1 is None: pass - elif remain_len == 1: + elif self.args.word2 is None: self.args.from_currency = self._convert_or_error( - currency_code, self.args.remainder[0]) - elif remain_len < 4: + currency_code, self.args.word1) + else: self.args.amount = self._convert_or_error( - decimal.Decimal, self.args.remainder[0]) + decimal.Decimal, self.args.word1) self.args.from_currency = self._convert_or_error( - currency_code, self.args.remainder[1]) - if remain_len == 3: + currency_code, self.args.word2) + if self.args.word3 is not None: self.args.to_currency = self._convert_or_error( - currency_code, self.args.remainder[2]) - else: - self.error("too many arguments") + currency_code, self.args.word3 or self.args.base) def _build_cache_loader(self): kwargs = dict(self.conffile.items('Cache'))