Skip to content

mutex_group

clap.mutex_group

mutex_group(
    parent_group: Optional[Group] = None,
    required: bool = False,
) -> MutexGroup

Create a mutually exclusive group of arguments.

It will be ensured that only one of the arguments in the mutually exclusive group is present on the command line. This is useful for options that conflict with each other, such as --verbose and --quiet.

Parameters:

  • parent_group (Optional[Group], default: None ) –

    The parent argument group to add this mutually exclusive group to. If None, the group will be added directly to the parser.

  • required (bool, default: False ) –

    Whether at least one of the mutually exclusive arguments must be present.

Example:

import clap
from clap import mutex_group

@clap.command
class Cli(clap.Parser):
    loglevel = mutex_group()
    verbose: bool = arg(long, mutex="loglevel")
    quiet: bool = arg(long, mutex="loglevel")
Source code in clap/api.py
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
def mutex_group(
    parent_group: Optional[Group] = None,
    required: bool = False,
) -> MutexGroup:
    """Create a mutually exclusive group of arguments.

    It will be ensured that only one of the arguments in the mutually
    exclusive group is present on the command line. This is useful for
    options that conflict with each other, such as `--verbose` and `--quiet`.

    Args:
        parent_group: The parent argument group to add this mutually exclusive group to.
            If `None`, the group will be added directly to the parser.
        required: Whether at least one of the mutually exclusive arguments must be present.

    Example:

    ```python
    import clap
    from clap import mutex_group

    @clap.command
    class Cli(clap.Parser):
        loglevel = mutex_group()
        verbose: bool = arg(long, mutex="loglevel")
        quiet: bool = arg(long, mutex="loglevel")
    ```
    """
    return MutexGroup(parent=parent_group, required=required)