query: Add --calendar-year and --fiscal-year shortcuts.

This commit is contained in:
Brett Smith 2021-03-09 10:13:08 -05:00
parent 6fa1278966
commit 5893d6a59a

View file

@ -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)