docopt & results¶
The primary entry point, the mapping it returns, the inverse format_argv, and the tree-inspection helper.
See the Typed results and Round-trip guides.
docopt ¶
docopt(
doc: str | None,
argv: list[str] | tuple[str, ...] | str | None = ...,
help: bool = ...,
version: object = ...,
options_first: bool = ...,
*,
default_help: bool | None = ...,
suggest: bool = ...,
negative_numbers: bool = ...,
allow_abbrev: bool = ...,
allow_extra: bool = ...,
exit_code: int = ...,
complete: bool = ...,
schema: type[SchemaT],
config: Mapping[str, Any] | None = ...,
help_style: Literal["raw", "rich"] = ...,
) -> SchemaT
docopt(
doc: str | None,
argv: list[str] | tuple[str, ...] | str | None = ...,
help: bool = ...,
version: object = ...,
options_first: bool = ...,
*,
default_help: bool | None = ...,
suggest: bool = ...,
negative_numbers: bool = ...,
allow_abbrev: bool = ...,
allow_extra: bool = ...,
exit_code: int = ...,
complete: bool = ...,
schema: None = ...,
config: Mapping[str, Any] | None = ...,
help_style: Literal["raw", "rich"] = ...,
) -> Arguments
Parse argv against the command-line interface described in doc.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
doc
|
str | None
|
Description of the command-line interface (the usage message). |
required |
argv
|
list[str] | tuple[str, ...] | str | None
|
Argument vector to parse. |
None
|
help
|
bool
|
Set to False to disable automatic help on |
True
|
version
|
object
|
If truthy, printed when |
None
|
options_first
|
bool
|
Set to True to require options to precede positional arguments. |
False
|
default_help
|
bool | None
|
Alias for |
None
|
suggest
|
bool
|
On a failed parse, add a "did you mean ..." hint to the |
True
|
negative_numbers
|
bool
|
Treat tokens like |
False
|
allow_abbrev
|
bool
|
When False, a long option in |
True
|
allow_extra
|
bool
|
When True, leftover |
False
|
exit_code
|
int
|
Process status carried by a |
1
|
complete
|
bool
|
Answer shell completion requests (on by default). docopt inspects the environment
for a request from a |
True
|
schema
|
type[SchemaT] | None
|
If given (a dataclass, TypedDict, or pydantic model), the parsed result
is bound to it and returned as that type instead of an |
None
|
config
|
Mapping[str, Any] | None
|
A mapping (from a config file you loaded) to resolve |
None
|
help_style
|
Literal['raw', 'rich']
|
|
'raw'
|
Returns:
| Type | Description |
|---|---|
Arguments | SchemaT
|
An |
Arguments | SchemaT
|
|
Arguments | SchemaT
|
in |
Raises:
| Type | Description |
|---|---|
DocoptLanguageError
|
The usage message is malformed, or the schema disagrees
with it (see |
DocoptExit
|
The user-supplied |
Example
docopt("Usage: prog <host> <port>", "127.0.0.1 8080") returns an Arguments
mapping {"<host>": "127.0.0.1", "<port>": "8080"}. Passing schema=Args (a
dataclass with host: str and port: int) instead returns an Args whose
port is an int.
Source code in src/docopt2/_core.py
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 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 | |
Arguments ¶
Mapping of parsed element names ("--flag", "<arg>", "command") to their values
(str | bool | int | list[str] | None). This is the back-compat return type, narrowed by the typed API.
provided is the set of names actually supplied in argv, so a defaulted value is
distinguishable from an explicit one. extra holds leftover tokens kept by allow_extra=True.
Source code in src/docopt2/_core.py
was_given ¶
source ¶
Where name's value was resolved from: the command line, [env:], [config:], or the default.
Answers "why is this value what it is?" for layered configuration. A name never resolved from a
fallback reports Source.DEFAULT.
Source code in src/docopt2/_core.py
Source ¶
Where a resolved value came from, in the precedence order docopt2 applies (highest first).
format_argv ¶
Synthesize a canonical argv that docopt parses back to result.
This is the inverse of parsing. Given an Arguments mapping returned by
docopt(doc, ...), return an argv token list (no program name) that round-trips:
docopt(doc, format_argv(result, doc), help=False, complete=False) == result.
The canonical form emits every element that carries a value, in usage order, with options in long
--name=value form. That is what the user supplied, plus whatever [env:] or [config:] resolved.
It is a valid argv, not necessarily the shortest or the one originally typed. A positional that would
read back as an option (-x, --) is written behind a --, with the options in front of it.
An env- or config-sourced value is emitted rather than skipped, so the argv reproduces the result on its own, without that environment. A persisted command that silently depended on an unrecorded variable would not reproduce the run. An off flag and a zero count are the exception: no token says them, so that part of the result is read from its source again.
Two things are omitted: an element left at its [default: ...], and one whose value is absent (an off
flag, a zero count). A source other than DEFAULT is not enough on its own, since an [env: V] flag
read as off has source ENV and value False, and emitting its name would parse back to True.
Where the omission does not parse back (the environment changed since the parse, or the mapping was built
by hand and records no sources), the defaults are written out as well.
The round trip is promised for one replay, the one the answer is checked with: help=False and
every other setting at its default. So a result that carries --help formats, and its argv parses
back only where --help is not acted on. A result that took another setting to produce
(options_first=True, negative_numbers=True) is formatted to parse back under the defaults.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
Arguments
|
An |
required |
doc
|
str
|
The same usage message that produced |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
An argv token list, without the program name. Each candidate usage line is generated and then |
list[str]
|
re-parsed to verify it round-trips, so the output is never a wrong argv, only a valid one. |
Raises:
| Type | Description |
|---|---|
ValueError
|
No usage pattern reproduces |
Source code in src/docopt2/_format.py
parse_tree ¶
Build the usage-pattern Pattern tree for doc without matching argv - repr() it,
walk it, or serialize with Pattern.to_dict to diff how a change affects parsing. The
[options] shortcut stays an OptionsShortcut node rather than expanded.