Group
clap.group
group(
cls: Optional[type[T]] = None,
/,
*,
title: Optional[str] = None,
about: Optional[str] = None,
long_about: Optional[str] = None,
required: bool = False,
multiple: bool = True,
) -> Union[type[T], Callable[[type[T]], type[T]]]
Configure a class as an argument group.
Argument groups allow organizing related arguments together, both in help output and in code structure. Arguments within a group are accessed as nested attributes.
Parameters:
-
cls(Optional[type[T]], default:None) –The class to be decorated (when used without parentheses).
-
title(Optional[str], default:None) –The title for the argument group in the help output.
-
about(Optional[str], default:None) –The group's description for the short help (
-h). -
long_about(Optional[str], default:None) –The group's description for the long help (
--help). -
required(bool, default:False) –Require an argument from the group to be present when parsing. Note: Currently, it only works when
multiple = False. -
multiple(bool, default:True) –Allows more than one of the Args in this group to be used.
Example:
import clap
from clap import arg, long
from typing import Optional
@clap.group(title="Input Options")
class InputOpts:
input_file: Optional[str] = arg(long)
@clap.group(title="Output Options")
class OutputOpts:
output_file: Optional[str] = arg(long)
@clap.command
class Cli(clap.Parser):
input_opts: InputOpts
output_opts: OutputOpts
cli = Cli.parse()
print(cli.input_opts.input_file)
print(cli.output_opts.output_dir)
Source code in clap/api.py
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 | |
Nesting groups is not allowed. However, sometimes, a group may have some
arguments that have to be mutually exclusive. In this case,
Group can be used to create a group field, i.e., group =
Group(required=True, multiple=False), and those arguments can have this as
their group: verbose: bool = arg(long, group=group), quiet: bool = arg(long,
group=group). In the future, I will add a conflicts_with argument to
arg, which will be more general than this workaround.
clap.Group
dataclass
Group(
title: Optional[str] = None,
about: Optional[str] = None,
long_about: Optional[str] = None,
required: bool = False,
multiple: bool = True,
)
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(title="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). -
long_about(Optional[str]) –The group's description for the long help (
--help). -
multiple(bool) –Allows more than one of the Args in this group to be used.
-
required(bool) –Require an argument from the group to be present when parsing. See note below.
-
title(Optional[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.
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).
multiple
class-attribute
instance-attribute
multiple: bool = True
Allows more than one of the Args in this group to be used.
required
class-attribute
instance-attribute
required: bool = False
Require an argument from the group to be present when parsing. See note below.
Note: Currently, it only works when multiple = False.