diff --git a/conservancy_beancount/reconcile/helper.py b/conservancy_beancount/reconcile/helper.py index 646efd4..2aff9af 100644 --- a/conservancy_beancount/reconcile/helper.py +++ b/conservancy_beancount/reconcile/helper.py @@ -20,6 +20,7 @@ from dateutil.relativedelta import relativedelta import datetime import decimal import io +import sys import tempfile import textwrap import typing @@ -113,29 +114,41 @@ def reconciliation_report_path(account, end_date): return f'Financial/Controls/Reports-for-Treasurer/{end_date}_{account_name}_bank-reconciliation.csv' -# Parse all the arguments -parser = argparse.ArgumentParser(description='Reconciliation helper') -parser.add_argument('--beancount-file', required=True) -parser.add_argument('--account', help='Full account name, e.g. "Liabilities:CreditCard:AMEX"', required=True) -parser.add_argument('--prev-end-date', type=datetime.date.fromisoformat) -parser.add_argument('--cur-end-date', type=datetime.date.fromisoformat) -parser.add_argument('--month', help='YYYY-MM of ending month. Use with --period.') -parser.add_argument('--period', help='Months in the past to consider. Use with --month.', type=int, choices=[1, 3, 12]) -parser.add_argument('--statement-match') -parser.add_argument('--cost-function', default='COST') -parser.add_argument('--grep-output-filename') -# parser.add_argument('--report-group-regex') -args = parser.parse_args() +def parse_args(): + parser = argparse.ArgumentParser(description='Reconciliation helper') + parser.add_argument('--beancount-file', required=True) + parser.add_argument('--account', help='Full account name, e.g. "Liabilities:CreditCard:AMEX"', required=True) + parser.add_argument('--prev-end-date', type=datetime.date.fromisoformat) + parser.add_argument('--cur-end-date', type=datetime.date.fromisoformat) + parser.add_argument('--month', help='YYYY-MM of ending month. Use with --period.') + parser.add_argument('--period', help='Months in the past to consider. Use with --month.', type=int, choices=[1, 3, 12]) + parser.add_argument('--statement-match') + parser.add_argument('--cost-function', default='COST') + parser.add_argument('--grep-output-filename') + # parser.add_argument('--report-group-regex') + args = parser.parse_args() + if args.month or args.period: + if not (args.month and args.period): + parser.error('--month and --period must be used together') + else: + if not (args.cur_end_date and args.prev_end_date): + parser.error(' --prev-end-date and --cur-end-date must be used together') + return args + + +def beancount_file_exists(path): + return os.path.isfile(path) + + +args = parse_args() +if not beancount_file_exists(args.beancount_file): + sys.exit(f'Beancount file does not exist: {args.beancount_file}') if args.month or args.period: - if not (args.month and args.period): - parser.error('--month and --period must be used together') parsed_date = datetime.datetime.strptime(args.month, '%Y-%m').date() preDate = end_of_month(parsed_date - relativedelta(months=args.period)).isoformat() lastDateInPeriod = end_of_month(parsed_date).isoformat() month = args.month else: - if not (args.cur_end_date and args.prev_end_date): - parser.error(' --prev-end-date and --cur-end-date must be used together') preDate = args.prev_end_date lastDateInPeriod = args.cur_end_date.isoformat() month = args.cur_end_date.strftime('%Y-%m')