2021-07-16 06:05:01 +00:00
|
|
|
# Mobile Verification Toolkit (MVT)
|
2023-09-09 15:55:27 +00:00
|
|
|
# Copyright (c) 2021-2023 The MVT Authors.
|
2021-08-01 19:11:08 +00:00
|
|
|
# Use of this software is governed by the MVT License 1.1 that can be found at
|
|
|
|
# https://license.mvt.re/1.1/
|
2021-07-16 06:05:01 +00:00
|
|
|
|
|
|
|
# From: https://gist.github.com/stanchan/bce1c2d030c76fe9223b5ff6ad0f03db
|
|
|
|
|
2021-08-14 16:56:33 +00:00
|
|
|
from click import Option, UsageError
|
2021-07-30 09:40:09 +00:00
|
|
|
|
2021-07-16 06:05:01 +00:00
|
|
|
|
|
|
|
class MutuallyExclusiveOption(Option):
|
2021-09-10 13:18:13 +00:00
|
|
|
"""This class extends click to support mutually exclusive options."""
|
2021-07-16 06:05:01 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.mutually_exclusive = set(kwargs.pop("mutually_exclusive", []))
|
2022-08-12 14:20:16 +00:00
|
|
|
help_msg = kwargs.get("help", "")
|
2021-07-16 06:05:01 +00:00
|
|
|
if self.mutually_exclusive:
|
|
|
|
ex_str = ", ".join(self.mutually_exclusive)
|
2023-06-01 21:40:26 +00:00
|
|
|
kwargs["help"] = (
|
|
|
|
f"{help_msg} NOTE: This argument is mutually exclusive with arguments"
|
|
|
|
f"[{ex_str}]."
|
|
|
|
)
|
2022-08-12 14:20:16 +00:00
|
|
|
|
|
|
|
super().__init__(*args, **kwargs)
|
2021-07-16 06:05:01 +00:00
|
|
|
|
|
|
|
def handle_parse_result(self, ctx, opts, args):
|
|
|
|
if self.mutually_exclusive.intersection(opts) and self.name in opts:
|
|
|
|
raise UsageError(
|
2022-08-16 11:39:55 +00:00
|
|
|
f"Illegal usage: `{self.name}` is mutually exclusive "
|
|
|
|
f"with arguments `{', '.join(self.mutually_exclusive)}`."
|
2021-07-16 06:05:01 +00:00
|
|
|
)
|
|
|
|
|
2022-08-12 14:20:16 +00:00
|
|
|
return super().handle_parse_result(ctx, opts, args)
|