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: str = ...,
) -> 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: str = ...,
) -> 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, if a mistyped long option resembles a known one,
include a "did you mean ..." hint in the |
False
|
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
|
str
|
|
'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
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 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 457 458 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 | |
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)) == 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.
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.
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.
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.