diff --git a/calendar/teachingdates/config/cli.py b/calendar/teachingdates/config/cli.py index 472a24c..0966b42 100644 --- a/calendar/teachingdates/config/cli.py +++ b/calendar/teachingdates/config/cli.py @@ -18,15 +18,22 @@ raise TypeError("expected list, got {t}".format(t=str(type(words)))) -def switch_message(switches, context, severity="warning", required=False): +def switch_message(switches, context, required=False): switch_string = oxford_comma_list(switches) - return ("{prog}: {severity}: {switches} {verb} {required} for " - "{context}".format(prog=PROG, - severity=severity, - switches=switch_string, - verb="are" if len(switches) > 1 else "is", - required="required" if required else "ignored", - context=context)) + return ("{switches} {verb} {required} for {context}".format( + switches=switch_string, + verb="are" if len(switches) > 1 else "is", + required="required" if required else "ignored", + context=context)) + + +def warning(message): + # This is somewhat trivial, but does provide output equivalence + # with parser.error(). + print("{prog}: warning: {message}".format( + prog=PROG, + message=message + )) def parse_command_line(): @@ -127,37 +134,46 @@ args.style = style_map[args.style] if args.style == "calendar": + # --teaching-period is required + if not args.period: + parser.error(switch_message( + ["--teaching-period/-t"], + context="'{style}' style".format(style=args.style), + required=True)) # --paper is irrelevant if args.paper: args.paper = "" - print("{prog}: warning: --paper/-p is ignored for 'calendar' " - "style".format(prog=PROG)) - # --teaching-period is required - if not args.period: - parser.exit( - 2, "{prog}: error: --teaching-period/-t is required for " - "'calendar' style\n".format(prog=PROG)) + warning(switch_message( + ["--paper/-p"], + context="'{style}' style".format(style=args.style) + )) elif args.style == "iso": # both --paper and --teaching-period are irrelevant - if args.paper or args.period: + if any([args.paper, args.period]): args.paper = "" args.period = "iso" - print("{prog}: warning: --paper/-p and --teaching-period/-t are " - "ignored for 'iso' style".format(prog=PROG)) + warning(switch_message( + ["--paper/-p", "--teaching-period/-t"], + context="'{style}' style".format(style=args.style) + )) - elif args.style == "lecture": + elif args.style in ["lecture", "lab"]: + # both --paper and --teaching-period are required + if not all([args.paper, args.period]): + parser.error(switch_message( + ["-paper/-p", "--teaching-period/-t"], + context="'{style}' style".format(style=args.style), + required=True + )) # --end-of-week is irrelevant if args.end_of_week is not None: args.end_of_week = None - print("{prog}: warning: --end-of-week/-e is ignored for 'lecture' " - "style".format(prog=PROG)) - # both --paper and --teaching-period are required - if not args.paper: - parser.exit( - 2, - message="{prog}: error: -paper/-p and --teaching-period/-t " - "are required for 'lecture' style\n".format(prog=PROG)) + warning(switch_message( + ["--end-of-week/-e"], + context="'{style}' style".format(style=args.style) + )) + # ignored_args(["--end-of-week/-e"], args.style)) else: pass