MCPcopy
hub / github.com/docopt/docopt / docopt

Function docopt

docopt.py:493–579  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

491
492
493def 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 = ''&#x27;
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 ''&#x27;
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 --------

Callers 15

test_commandsFunction · 0.90
test_matching_parenFunction · 0.90
test_allow_double_dashFunction · 0.90
test_docoptFunction · 0.90
test_language_errorsFunction · 0.90
test_issue_40Function · 0.90

Calls 12

printable_usageFunction · 0.85
parse_defaultsFunction · 0.85
parse_patternFunction · 0.85
formal_usageFunction · 0.85
parse_argvFunction · 0.85
TokenStreamClass · 0.85
extrasFunction · 0.85
DictClass · 0.85
DocoptExitClass · 0.85
fixMethod · 0.80
flatMethod · 0.45
matchMethod · 0.45

Tested by 15

test_commandsFunction · 0.72
test_matching_parenFunction · 0.72
test_allow_double_dashFunction · 0.72
test_docoptFunction · 0.72
test_language_errorsFunction · 0.72
test_issue_40Function · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…