arg
clap.arg
arg(
short_or_long: Optional[AutoFlag] = None,
long_or_short: Optional[AutoFlag] = None,
/,
*,
short: Optional[Union[str, bool]] = None,
long: Optional[Union[str, bool]] = None,
aliases: Optional[Sequence[str]] = None,
group: Optional[Group] = None,
action: Optional[Union[type, ArgAction]] = None,
num_args: Optional[NargsType] = None,
default_missing_value: Optional[U] = None,
default_value: Optional[U] = None,
choices: Optional[Sequence[str]] = None,
required: Optional[bool] = None,
help: Optional[str] = None,
long_help: Optional[str] = None,
value_name: Optional[str] = None,
deprecated: bool = False,
) -> Any
Create a command-line argument.
Parameters:
-
short_or_long(Optional[AutoFlag], default:None) –Use
clap.shortorclap.longto automatically create the short or long version of the argument. -
long_or_short(Optional[AutoFlag], default:None) –Use
clap.shortorclap.longto automatically create the short or long version of the argument. -
short(Optional[Union[str, bool]], default:None) –The short version of the argument without the preceding
-. SpecifyTrueto automatically create it. -
long(Optional[Union[str, bool]], default:None) –The long version of the argument without the preceding
--. SpecifyTrueto automatically create it. -
aliases(Optional[Sequence[str]], default:None) –Additional flags for the argument.
-
group(Optional[Group], default:None) –The group to which the argument is added.
-
action(Optional[Union[type, ArgAction]], default:None) –How to react to an argument when parsing it.
-
num_args(Optional[NargsType], default:None) –The number of arguments parsed per occurrence.
-
default_missing_value(Optional[U], default:None) –The value for the argument when the flag is present but no value is specified.
-
default_value(Optional[U], default:None) –The value for the argument when not present.
-
choices(Optional[Sequence[str]], default:None) –A sequence of valid choices for the argument.
-
required(Optional[bool], default:None) –Whether the argument must be present.
-
help(Optional[str], default:None) –The description of the argument for short help (
-h). -
long_help(Optional[str], default:None) –The description of the argument for long help (
--help). -
value_name(Optional[str], default:None) –The placeholder for the argument's value in the help message / usage.
-
deprecated(bool, default:False) –Whether this argument is deprecated and should not be used.
Examples:
import clap
from clap import ArgAction, ColorChoice, arg, long, short
@clap.command
class Cli:
verbose: bool = arg(short, long)
include_hidden: bool = arg(short="H", long="hidden")
additional_patterns: list[str] = arg(long="and", action=ArgAction.Append)
color: ColorChoice = arg(
long,
value_name="WHEN",
default_value=ColorChoice.Auto,
default_missing_value=ColorChoice.Always,
num_args="?",
)
Source code in clap/api.py
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 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 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 | |
arg() returns an Arg object. However, the type stub for this function has return type Any. This way, type checkers are fooled, and the library gets complete information.
clap.short
module-attribute
short = Short
Generate short from the first character in the case-converted field name.