From 63b0ad3c76bf2929df5d359b92f89a2a5f4350d6 Mon Sep 17 00:00:00 2001 From: Brett Smith Date: Wed, 17 May 2017 15:34:51 -0400 Subject: [PATCH] Configuration: Improve --help output throughout. --- oxrlib/config.py | 52 +++++++++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/oxrlib/config.py b/oxrlib/config.py index e4db88d..af712ad 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( - 'remainder', - nargs=argparse.REMAINDER, + '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( + '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: - self.args.amount = self._convert_or_error( - decimal.Decimal, self.args.remainder[0]) - self.args.from_currency = self._convert_or_error( - currency_code, self.args.remainder[1]) - if remain_len == 3: - self.args.to_currency = self._convert_or_error( - currency_code, self.args.remainder[2]) + currency_code, self.args.word1) else: - self.error("too many arguments") + self.args.amount = self._convert_or_error( + decimal.Decimal, self.args.word1) + self.args.from_currency = self._convert_or_error( + currency_code, self.args.word2) + if self.args.word3 is not None: + self.args.to_currency = self._convert_or_error( + currency_code, self.args.word3 or self.args.base) def _build_cache_loader(self): kwargs = dict(self.conffile.items('Cache'))