diff --git a/khal/cli.py b/khal/cli.py index e1020fcdc..be1146f4e 100644 --- a/khal/cli.py +++ b/khal/cli.py @@ -43,6 +43,25 @@ from io import StringIO +def build_collection(conf, include_calendar, exclude_calendar): + collection = khalendar.CalendarCollection() + for name, cal in conf['calendars'].items(): + if (not exclude_calendar and name in include_calendar) or \ + (not include_calendar and name not in exclude_calendar): + collection.append(khalendar.Calendar( + name=name, + dbpath=conf['sqlite']['path'], + path=cal['path'], + readonly=cal['readonly'], + color=cal['color'], + unicode_symbols=conf['locale']['unicode_symbols'], + local_tz=conf['locale']['local_timezone'], + default_tz=conf['locale']['default_timezone'] + )) + collection._default_calendar_name = conf['default']['default_calendar'] + return collection + + def _get_cli(): @click.group(invoke_without_command=True) @click.option('--config', '-c', default=None, metavar='PATH', @@ -85,22 +104,8 @@ def cli(ctx, config, verbose, include_calendar, exclude_calendar): if include_calendar and exclude_calendar: raise click.UsageError('Can\'t use both -a and -d.') - - ctx.obj['collection'] = collection = khalendar.CalendarCollection() - for name, cal in conf['calendars'].items(): - if (not exclude_calendar and name in include_calendar) or \ - (not include_calendar and name not in exclude_calendar): - collection.append(khalendar.Calendar( - name=name, - dbpath=conf['sqlite']['path'], - path=cal['path'], - readonly=cal['readonly'], - color=cal['color'], - unicode_symbols=conf['locale']['unicode_symbols'], - local_tz=conf['locale']['local_timezone'], - default_tz=conf['locale']['default_timezone'] - )) - collection._default_calendar_name = conf['default']['default_calendar'] + ctx.obj['include_calendar'] = include_calendar + ctx.obj['exclude_calendar'] = exclude_calendar if not ctx.invoked_subcommand: command = conf['default']['default_command'] @@ -115,12 +120,29 @@ def cli(ctx, config, verbose, include_calendar, exclude_calendar): dates_arg = click.argument('dates', nargs=-1) time_args = lambda f: dates_arg(events_option(days_option(f))) + include_option = click.option( + '--include-calendar', '-a', multiple=True, metavar='CAL', + help=('Include the given calendar. Can be specified multiple times.')) + exclude_option = click.option( + '--exclude-calendar', '-d', multiple=True, metavar='CAL', + help=('Exclude the given calendar. Can be specified multiple times.')) + + calendar_options = lambda f: include_option(exclude_option(f)) + @cli.command() @time_args + @calendar_options @click.pass_context - def calendar(ctx, days, events, dates): + def calendar(ctx, days, events, dates, include_calendar, exclude_calendar): + include_calendar = list(include_calendar) + list(ctx.obj['include_calendar']) + exclude_calendar = list(exclude_calendar) + list(ctx.obj['exclude_calendar']) + if include_calendar and exclude_calendar: + raise click.UsageError('Can\'t use both -a and -d.') + + collection = build_collection( + ctx.obj['conf'], include_calendar, exclude_calendar) controllers.Calendar( - ctx.obj['collection'], + collection, date=dates, firstweekday=ctx.obj['conf']['locale']['firstweekday'], encoding=ctx.obj['conf']['locale']['encoding'], @@ -132,10 +154,17 @@ def calendar(ctx, days, events, dates): @cli.command() @time_args + @calendar_options @click.pass_context - def agenda(ctx, days, events, dates): + def agenda(ctx, days, events, dates, include_calendar, exclude_calendar): + include_calendar = list(include_calendar) + list(ctx.obj['include_calendar']) + exclude_calendar = list(exclude_calendar) + list(ctx.obj['exclude_calendar']) + if include_calendar and exclude_calendar: + raise click.UsageError('Can\'t use both -a and -d.') + collection = build_collection( + ctx.obj['conf'], include_calendar, exclude_calendar) controllers.Agenda( - ctx.obj['collection'], + collection, date=dates, firstweekday=ctx.obj['conf']['locale']['firstweekday'], encoding=ctx.obj['conf']['locale']['encoding'],