From ff3c765f1c33f3adbda07f2d828542099f9cbb26 Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Sat, 11 Feb 2023 17:34:02 +0100 Subject: Add support for matching against an identifier Signed-off-by: Olivier Gayot --- monitor-menu.py | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) (limited to 'monitor-menu.py') diff --git a/monitor-menu.py b/monitor-menu.py index 8a77c41..d450ba8 100755 --- a/monitor-menu.py +++ b/monitor-menu.py @@ -45,6 +45,7 @@ class Profile: name: str monitors: list[Monitor] = dataclasses.field(default_factory=list) + identifier: str | None = None xrandr_opts: list[str] = dataclasses.field(default_factory=list) @classmethod @@ -112,7 +113,13 @@ class MonitorMenu: def main(): parser = argparse.ArgumentParser() parser.add_argument("--log-level", "--loglevel", type=LogLevel, default="info") - parser.add_argument("--index", type=int, required=False) + subparser = parser.add_subparsers(dest="command") + subparser.add_parser("run") + apply_parser = subparser.add_parser("apply") + identifier_group = apply_parser.add_mutually_exclusive_group(required=True) + identifier_group.add_argument("--index", type=int) + identifier_group.add_argument("--id", type=str) + args = parser.parse_args() logging.basicConfig(level=args.log_level.numerical_level) @@ -121,13 +128,20 @@ def main(): data = json.load(fh) profiles = [Profile.from_json_dict(item) for item in data] - if args.index is None: - MonitorMenu(profiles).run() - else: - try: - profiles[args.index].apply() - except IndexError: - raise NoMatchingProfile from None + match args.command: + case "run" | None: + MonitorMenu(profiles).run() + case "apply": + if args.index is not None: + try: + profiles[args.index].apply() + except IndexError: + raise NoMatchingProfile from None + else: + try: + next(filter(lambda p: p.identifier == args.id, profiles)).apply() + except StopIteration: + raise NoMatchingProfile from None if __name__ == '__main__': -- cgit v1.2.3