frompathlibimportPathfromtypingimportOptionalimporttyperfromicalendarimportCalendarfromrichimportprintfrom.calendar_mergerimportCalendarMergerapp=typer.Typer()# Define arguments and options outside of the functioncalendars_arg=typer.Argument(...,help="Paths to the calendar files to merge")output_opt=typer.Option("merged_calendar.ics","--output","-o",help="Output file path")prodid_opt=typer.Option(None,"--prodid",help="Product ID for the merged calendar")method_opt=typer.Option(None,"--method",help="Calendar method")
[docs]@app.command()defmain(calendars:list[Path]=calendars_arg,output:Path=output_opt,prodid:Optional[str]=prodid_opt,method:Optional[str]=method_opt,)->None:"""Merge multiple iCalendar files into one."""try:calendar_objects=[]forcalendar_pathincalendars:withopen(calendar_path,"rb")ascal_file:calendar_objects.append(Calendar.from_ical(cal_file.read()))merger=CalendarMerger(calendars=calendar_objects,prodid=prodid,method=method)merged_calendar=merger.merge()withopen(output,"wb")asoutput_file:output_file.write(merged_calendar.to_ical())print(f"[green]Successfully merged calendars into {output}[/green]")exceptExceptionase:print(f"[red]Error merging calendars: {e!s}[/red]")