hoodini.utils.cli_helpers

 1import click
 2
 3
 4class MutuallyExclusiveOption(click.Option):
 5    """
 6    A custom click.Option that refuses to let certain other options be provided
 7    at the same time. Use it like:
 8
 9        @click.option(
10            "--foo",
11            cls=MutuallyExclusiveOption,
12            mutually_exclusive=["bar", "baz"],
13            help="..."
14        )
15        @click.option(
16            "--bar",
17            cls=MutuallyExclusiveOption,
18            mutually_exclusive=["foo"],
19            help="..."
20        )
21    """
22
23    def __init__(self, *args, **kwargs):
24        self.mutually_exclusive = set(kwargs.pop("mutually_exclusive", []))
25        super().__init__(*args, **kwargs)
26
27    def handle_parse_result(self, ctx, opts, args):
28        if self.name in opts and opts.get(self.name) not in (None, False):
29            for forbidden in self.mutually_exclusive:
30                if forbidden in opts and opts.get(forbidden) not in (None, False):
31                    msg = (
32                        f"Illegal usage: '{self.name}' is mutually exclusive "
33                        f"with '{forbidden}'. You cannot use both at once."
34                    )
35                    raise click.UsageError(msg)
36        return super().handle_parse_result(ctx, opts, args)
class MutuallyExclusiveOption(click.core.Option):
 5class MutuallyExclusiveOption(click.Option):
 6    """
 7    A custom click.Option that refuses to let certain other options be provided
 8    at the same time. Use it like:
 9
10        @click.option(
11            "--foo",
12            cls=MutuallyExclusiveOption,
13            mutually_exclusive=["bar", "baz"],
14            help="..."
15        )
16        @click.option(
17            "--bar",
18            cls=MutuallyExclusiveOption,
19            mutually_exclusive=["foo"],
20            help="..."
21        )
22    """
23
24    def __init__(self, *args, **kwargs):
25        self.mutually_exclusive = set(kwargs.pop("mutually_exclusive", []))
26        super().__init__(*args, **kwargs)
27
28    def handle_parse_result(self, ctx, opts, args):
29        if self.name in opts and opts.get(self.name) not in (None, False):
30            for forbidden in self.mutually_exclusive:
31                if forbidden in opts and opts.get(forbidden) not in (None, False):
32                    msg = (
33                        f"Illegal usage: '{self.name}' is mutually exclusive "
34                        f"with '{forbidden}'. You cannot use both at once."
35                    )
36                    raise click.UsageError(msg)
37        return super().handle_parse_result(ctx, opts, args)

A custom click.Option that refuses to let certain other options be provided at the same time. Use it like:

@click.option(
    "--foo",
    cls=MutuallyExclusiveOption,
    mutually_exclusive=["bar", "baz"],
    help="..."
)
@click.option(
    "--bar",
    cls=MutuallyExclusiveOption,
    mutually_exclusive=["foo"],
    help="..."
)
MutuallyExclusiveOption(*args, **kwargs)
24    def __init__(self, *args, **kwargs):
25        self.mutually_exclusive = set(kwargs.pop("mutually_exclusive", []))
26        super().__init__(*args, **kwargs)
mutually_exclusive
def handle_parse_result(self, ctx, opts, args):
28    def handle_parse_result(self, ctx, opts, args):
29        if self.name in opts and opts.get(self.name) not in (None, False):
30            for forbidden in self.mutually_exclusive:
31                if forbidden in opts and opts.get(forbidden) not in (None, False):
32                    msg = (
33                        f"Illegal usage: '{self.name}' is mutually exclusive "
34                        f"with '{forbidden}'. You cannot use both at once."
35                    )
36                    raise click.UsageError(msg)
37        return super().handle_parse_result(ctx, opts, args)