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
(Sequence[str]
) –Flags in addition to
short
andlong
. -
group
(Optional[Group]
) –The group containing the argument.
-
long
(Optional[Union[AutoFlag, str]]
) –The long flag.
-
mutex
(Optional[MutexGroup]
) –The mutually exclusive group containing the argument.
-
short
(Optional[Union[AutoFlag, str]]
) –The short flag.
-
ty
(Optional[Base]
) –Stores type information for the argument.
aliases
class-attribute
instance-attribute
Flags in addition to short
and long
.
group
class-attribute
instance-attribute
The group containing the argument.
mutex
class-attribute
instance-attribute
mutex: Optional[MutexGroup] = None
The mutually exclusive group containing the argument.
short
class-attribute
instance-attribute
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
(Optional[str]
) –The group's description for the short help (
-h
). -
conflict_handler
(Optional[str]
) –The strategy for resolving conflicting optionals within this group.
-
long_about
(Optional[str]
) –The group's description for the long help (
--help
). -
title
(str
) –The title for the argument group in the help output.
about
class-attribute
instance-attribute
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
The strategy for resolving conflicting optionals within this group.
This is forwarded to argparse.
long_about
class-attribute
instance-attribute
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
).
clap.core.MutexGroup
dataclass
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
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:
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
(Optional[type]
) –Contains the class if it is a subcommand.