Skip to content

Core

clap.core.Arg dataclass

Arg(
    short: Optional[Union[AutoFlag, str]] = None,
    long: Optional[Union[AutoFlag, str]] = None,
    help: Optional[str] = None,
    long_help: Optional[str] = None,
    value_name: Optional[str] = None,
    aliases: Sequence[str] = list(),
    ty: Optional[Base] = None,
    group: Optional[Group] = None,
    mutex: Optional[MutexGroup] = None,
    action: Optional[Union[ArgAction, type]] = None,
    num_args: Optional[NargsType] = None,
    default_missing_value: Optional[Any] = None,
    default_value: Optional[Any] = None,
    choices: Optional[Sequence[str]] = None,
    required: Optional[bool] = None,
    deprecated: Optional[bool] = None,
    dest: Optional[str] = None,
)

Attributes:

aliases class-attribute instance-attribute

aliases: Sequence[str] = field(default_factory=list)

Flags in addition to short and long.

group class-attribute instance-attribute

group: Optional[Group] = None

The group containing the argument.

long class-attribute instance-attribute

long: Optional[Union[AutoFlag, str]] = None

The long flag.

mutex class-attribute instance-attribute

mutex: Optional[MutexGroup] = None

The mutually exclusive group containing the argument.

short class-attribute instance-attribute

short: Optional[Union[AutoFlag, str]] = None

The short flag.

ty class-attribute instance-attribute

ty: Optional[Base] = None

Stores type information for the argument.

clap.core.Group dataclass

Group(
    title: str,
    about: Optional[str] = None,
    long_about: Optional[str] = None,
    conflict_handler: Optional[str] = None,
)

Family of related arguments.

Example:

from pathlib import Path

import clap
from clap import Group, arg

@clap.command
class Cli(clap.Parser):
    output_options = Group("Output Options")
    """Configure output settings."""
    output_dir: Path = arg(long="output", group=output_options, value_name="DIR")
    """Path to output directory"""

Attributes:

about class-attribute instance-attribute

about: Optional[str] = None

The group's description for the short help (-h).

If Group.long_about is not specified, this message will be displayed for --help.

conflict_handler class-attribute instance-attribute

conflict_handler: Optional[str] = None

The strategy for resolving conflicting optionals within this group.

This is forwarded to argparse.

long_about class-attribute instance-attribute

long_about: Optional[str] = None

The group's description for the long help (--help).

If not set, Group.about will be used for long help in addition to short help (-h).

title instance-attribute

title: str

The title for the argument group in the help output.

clap.core.MutexGroup dataclass

MutexGroup(
    parent: Optional[Group] = None, required: bool = False
)

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.

Example:

import clap
from clap import MutexGroup

@clap.command
class Cli(clap.Parser):
    loglevel = MutexGroup()
    verbose: bool = arg(long, mutex=loglevel)
    quiet: bool = arg(long, mutex=loglevel)

Attributes:

  • parent (Optional[Group]) –

    The parent argument group to add this mutually exclusive group to.

  • required (bool) –

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

parent class-attribute instance-attribute

parent: Optional[Group] = None

The parent argument group to add this mutually exclusive group to.

If None, the group will be added directly to the parser.

required class-attribute instance-attribute

required: bool = False

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

clap.core.AutoFlag

Bases: Enum

Attributes:

  • Long

    Generate long from the case-converted field name.

  • Short

    Generate short from the first character in the case-converted field name.

Long class-attribute instance-attribute

Long = auto()

Generate long from the case-converted field name.

Alias: long.

Short class-attribute instance-attribute

Short = auto()

Generate short from the first character in the case-converted field name.

Alias: short.

clap.core.Command dataclass

Command(
    name: str,
    aliases: Sequence[str] = list(),
    usage: Optional[str] = None,
    author: Optional[str] = None,
    version: Optional[str] = None,
    long_version: Optional[str] = None,
    about: Optional[str] = None,
    long_about: Optional[str] = None,
    before_help: Optional[str] = None,
    before_long_help: Optional[str] = None,
    after_help: Optional[str] = None,
    after_long_help: Optional[str] = None,
    subcommand_help_heading: str = "Commands",
    subcommand_value_name: str = "COMMAND",
    color: Optional[ColorChoice] = None,
    styles: Optional[Styles] = None,
    help_template: Optional[str] = None,
    max_term_width: Optional[int] = None,
    propagate_version: bool = False,
    disable_version_flag: bool = False,
    disable_help_flag: bool = False,
    prefix_chars: str = "-",
    fromfile_prefix_chars: Optional[str] = None,
    conflict_handler: Optional[str] = None,
    allow_abbrev: Optional[bool] = None,
    exit_on_error: Optional[bool] = None,
    deprecated: Optional[bool] = None,
    args: dict[str, Arg] = dict(),
    groups: dict[Group, list[Arg]] = dict(),
    mutexes: defaultdict[
        MutexGroup, list[Arg]
    ] = lambda: defaultdict(list)(),
    subcommand_class: Optional[type] = None,
    subcommands: dict[str, Self] = dict(),
    subcommand_dest: Optional[str] = None,
    subparser_dest: Optional[str] = None,
    subcommand_required: bool = False,
)

Attributes:

subcommand_class class-attribute instance-attribute

subcommand_class: Optional[type] = None

Contains the class if it is a subcommand.