From b601ef0c67675d3c72ce2277d99f5414e634cdab Mon Sep 17 00:00:00 2001 From: "Fredrick W. Warren" Date: Sun, 7 Sep 2025 10:23:39 -0600 Subject: [PATCH] added --months -m to accept a month or range of months --- main.py | 48 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index a72efe7..9e54462 100755 --- a/main.py +++ b/main.py @@ -165,29 +165,69 @@ def process_month(months, monthly_events, year, month, week_of_month, week, even debug_print("", DEBUG) return events - -def process_months(months, monthly_events, year): +def process_months(months, monthly_events, year, month_range): """process full year of months and return list of events""" events = [] for month in range(1, 13): + if month not in month_range: + continue debug_print(f"MONTH: {month}", DEBUG) for week_of_month, week in enumerate(months[month]): debug_print(f"{week_of_month} {week}", DEBUG) events = process_month(months, monthly_events, year, month, week_of_month, week, events) return events +def validate_months(ctx, param, value): + """ + Validates and processes the --months option. + Accepts a single number (1-12) or a range (1-3). + Defaults to 1-12 if no value is provided. + """ + if value is None: + return list(range(1, 13)) # Default behavior + + parts = value.split('-') + + if len(parts) == 1: + # Handle single month input + try: + month = int(parts[0]) + if 1 <= month <= 12: + return [month] + else: + raise click.BadParameter("Month must be between 1 and 12.", param=param) + except ValueError: + raise click.BadParameter("Invalid month format. Please use an integer between 1 and 12.", param=param) + + elif len(parts) == 2: + # Handle range input + try: + start, end = int(parts[0]), int(parts[1]) + if 1 <= start <= 12 and 1 <= end <= 12 and start <= end: + return list(range(start, end + 1)) + else: + raise click.BadParameter("Invalid month range. Both months must be between 1 and 12, and the start month must be less than or equal to the end month.", param=param) + except ValueError: + raise click.BadParameter("Invalid range format. Please use two integers separated by a hyphen (e.g., 1-3).", param=param) + + else: + # Handle invalid format + raise click.BadParameter("Invalid format. Use a single number (e.g., 3) or a range (e.g., 1-3).", param=param) + # @click.argument('filename', type=click.File('w'), default="calendar.csv", required=False) @click.command() @click.option("--debug", "-d", is_flag=True, default=False, help="Print debug output") @click.option('--year', "-y", type=int, default=date.today().year, help='Specify the year.') +@click.option('--months', '-m', 'month_range', callback=validate_months, default=None, + help='A single month (1-12) or a range (e.g., 1-3). Defaults to all months (1-12).') @click.argument('filename', default="calendar.csv", required=False) -def main(debug, year, filename): +def main(debug, year, month_range, filename): """Create a .csv file of church events for the year. Modify events.py to change events""" global DEBUG DEBUG = debug months = initalize_year(year) monthly_events = initalize_events(EVENT_LIST) - events = process_months(months, monthly_events, year) + events = process_months(months, monthly_events, year, month_range) # events = sort_events(events) write_calendar(events, filename) sys.exit(0)