From 5893d6a59a2bec193f033c9f6cee085a33b4d2b4 2021-03-09 15:39:12 From: Brett Smith Date: 2021-03-09 15:39:12 Subject: [PATCH] query: Add --calendar-year and --fiscal-year shortcuts. --- diff --git a/conservancy_beancount/reports/query.py b/conservancy_beancount/reports/query.py index 9ca96113e0960d15187257d67790bb4d0c64135b..b2fe7cb15bb87919bce532dfe27206cc32d16315 100644 --- a/conservancy_beancount/reports/query.py +++ b/conservancy_beancount/reports/query.py @@ -419,6 +419,35 @@ class ReportFormat(enum.Enum): ODS = 'ods' +class SetCYDates(argparse.Action): + def __call__(self, + parser: argparse.ArgumentParser, + namespace: argparse.Namespace, + values: Union[Sequence[Any], str, None]=None, + option_string: Optional[str]=None, + ) -> None: + value = cliutil.year_or_date_arg(str(values)) + if isinstance(value, int): + value = datetime.date(value, 1, 1) + namespace.start_date = value + namespace.stop_date = cliutil.diff_year(value, 1) + + +class SetFYDates(argparse.Action): + def __call__(self, + parser: argparse.ArgumentParser, + namespace: argparse.Namespace, + values: Union[Sequence[Any], str, None]=None, + option_string: Optional[str]=None, + ) -> None: + value = cliutil.year_or_date_arg(str(values)) + namespace.start_date = value + if isinstance(value, int): + namespace.stop_date = value + else: + namespace.stop_date = value + datetime.timedelta(days=1) + + def parse_arguments(arglist: Optional[Sequence[str]]=None) -> argparse.Namespace: parser = argparse.ArgumentParser(prog=PROGNAME) cliutil.add_version_argument(parser) @@ -438,6 +467,20 @@ full date, and %(prog)s will use the fiscal year for that date. type=cliutil.year_or_date_arg, help="""End loading entries at this fiscal year. You can specify a full date, and %(prog)s will use the fiscal year for that date. +""") + parser.add_argument( + '--calendar-year', '--cy', + action=SetCYDates, + metavar='YEAR', + help="""Shortcut to set --begin and --end to load a single calendar year. +You can specify a full date, or just a year to start from January 1. +""") + parser.add_argument( + '--fiscal-year', '--fy', + action=SetFYDates, + metavar='YEAR', + help="""Shortcut to set --begin and --end to load a single fiscal year. +You can specify a full date, and %(prog)s will use the fiscal year for that date. """) cliutil.add_rewrite_rules_argument(parser) format_arg = cliutil.EnumArgument(ReportFormat)