Parse `argv` based on command-line interface described in `doc`. `docopt` creates your command-line interface based on its description that you pass as `doc`. Such description can contain --options, , commands, which could be [optional], (required), (mutually |
(doc, argv=None, help=True, version=None, options_first=False)
| 491 | |
| 492 | |
| 493 | def docopt(doc, argv=None, help=True, version=None, options_first=False): |
| 494 | """Parse `argv` based on command-line interface described in `doc`. |
| 495 | |
| 496 | `docopt` creates your command-line interface based on its |
| 497 | description that you pass as `doc`. Such description can contain |
| 498 | --options, <positional-argument>, commands, which could be |
| 499 | [optional], (required), (mutually | exclusive) or repeated... |
| 500 | |
| 501 | Parameters |
| 502 | ---------- |
| 503 | doc : str |
| 504 | Description of your command-line interface. |
| 505 | argv : list of str, optional |
| 506 | Argument vector to be parsed. sys.argv[1:] is used if not |
| 507 | provided. |
| 508 | help : bool (default: True) |
| 509 | Set to False to disable automatic help on -h or --help |
| 510 | options. |
| 511 | version : any object |
| 512 | If passed, the object will be printed if --version is in |
| 513 | `argv`. |
| 514 | options_first : bool (default: False) |
| 515 | Set to True to require options preceed positional arguments, |
| 516 | i.e. to forbid options and positional arguments intermix. |
| 517 | |
| 518 | Returns |
| 519 | ------- |
| 520 | args : dict |
| 521 | A dictionary, where keys are names of command-line elements |
| 522 | such as e.g. "--verbose" and "<path>", and values are the |
| 523 | parsed values of those elements. |
| 524 | |
| 525 | Example |
| 526 | ------- |
| 527 | >>> from docopt import docopt |
| 528 | >>> doc = ''' |
| 529 | Usage: |
| 530 | my_program tcp <host> <port> [--timeout=<seconds>] |
| 531 | my_program serial <port> [--baud=<n>] [--timeout=<seconds>] |
| 532 | my_program (-h | --help | --version) |
| 533 | |
| 534 | Options: |
| 535 | -h, --help Show this screen and exit. |
| 536 | --baud=<n> Baudrate [default: 9600] |
| 537 | ''' |
| 538 | >>> argv = ['tcp', '127.0.0.1', '80', '--timeout', '30'] |
| 539 | >>> docopt(doc, argv) |
| 540 | {'--baud': '9600', |
| 541 | '--help': False, |
| 542 | '--timeout': '30', |
| 543 | '--version': False, |
| 544 | '<host>': '127.0.0.1', |
| 545 | '<port>': '80', |
| 546 | 'serial': False, |
| 547 | 'tcp': True} |
| 548 | |
| 549 | See also |
| 550 | -------- |
searching dependent graphs…