diff options
author | Olivier Gayot <olivier.gayot@sigexec.com> | 2023-02-11 17:34:02 +0100 |
---|---|---|
committer | Olivier Gayot <olivier.gayot@sigexec.com> | 2023-02-11 17:48:15 +0100 |
commit | ff3c765f1c33f3adbda07f2d828542099f9cbb26 (patch) | |
tree | 91fde559f512a711724c00943af007500beed828 /monitor-menu.py | |
parent | eaa1b5a3d73fdf295c38aea528732024202178c6 (diff) |
Add support for matching against an identifier
Signed-off-by: Olivier Gayot <olivier.gayot@sigexec.com>
Diffstat (limited to 'monitor-menu.py')
-rwxr-xr-x | monitor-menu.py | 30 |
1 files changed, 22 insertions, 8 deletions
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__': |