{"id":13409880,"url":"https://github.com/docopt/docopt.php","last_synced_at":"2025-10-18T18:16:07.731Z","repository":{"id":3665211,"uuid":"4734015","full_name":"docopt/docopt.php","owner":"docopt","description":"Command line argument parser","archived":false,"fork":false,"pushed_at":"2025-03-26T03:38:47.000Z","size":168,"stargazers_count":300,"open_issues_count":1,"forks_count":25,"subscribers_count":14,"default_branch":"master","last_synced_at":"2025-04-19T04:33:29.054Z","etag":null,"topics":["cli","docopt","php"],"latest_commit_sha":null,"homepage":"http://docopt.org","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/docopt.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-MIT","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2012-06-21T02:38:28.000Z","updated_at":"2025-02-02T15:11:28.000Z","dependencies_parsed_at":"2025-04-12T01:06:48.346Z","dependency_job_id":"faf4829b-93aa-4075-b4fb-a9cd90969f9f","html_url":"https://github.com/docopt/docopt.php","commit_stats":{"total_commits":81,"total_committers":11,"mean_commits":7.363636363636363,"dds":0.3580246913580247,"last_synced_commit":"0e3db660cf2f2eb07a83253790b7d97cdb398826"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/docopt%2Fdocopt.php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/docopt%2Fdocopt.php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/docopt%2Fdocopt.php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/docopt%2Fdocopt.php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/docopt","download_url":"https://codeload.github.com/docopt/docopt.php/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254459088,"owners_count":22074605,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cli","docopt","php"],"created_at":"2024-07-30T20:01:03.865Z","updated_at":"2025-10-18T18:16:07.678Z","avatar_url":"https://github.com/docopt.png","language":"PHP","readme":"``docopt`` creates *beautiful* command-line interfaces\n======================================================================\n\n.. image:: https://travis-ci.org/docopt/docopt.php.svg?branch=master\n    :target: https://travis-ci.org/docopt/docopt.php\n\nThis is a straight PHP transliteration of Vladimir Keleshev's brilliant\n`docopt \u003chttps://github.com/docopt/docopt/\u003e`_ Python library. There are a\nfew artefacts in the code as a result that may seem inefficient and\nnon-idiomatic to PHP, this has been done to make integrating changes more\nefficient.\n\nAs a result, unless a bug is present only in the PHP version, pull requests\nare unlikely to be accepted unless they are themselves direct transliterations\nof bugfixes in the Python version. \n\n**This port has been marked version 1.0**. It is based on the Python version at\ncommit `a093f938b7f26564434f3c15a1dcc39e017ad638\n\u003chttps://github.com/docopt/docopt/commit/a093f938b7f26564434f3c15a1dcc39e017ad638\u003e`_\n(labelled **0.6.2**).\n\nIt has been quite stable for a long time and has barely been changed.  The Python version\nreceives only occasional bugfixes and keeping the version numbers pinned has been more\ntrouble than it has been worth.\n\nThere are also some major backward compatibility breaks. Rather than dwell in 0.x semver\nhell, the PHP port will liberally bump major numbers henceforth when BC breaks regardless\nof the reason.\n\n- The PHP API has changed slightly. ``Docopt\\docopt()`` has been renamed to\n  ``Docopt::handle()`` to fix autoloader support. See `issue #3\n  \u003chttps://github.com/docopt/docopt.php/pull/3\u003e`_.\n\n- Docopt.py also has a significant BC break. Existing users should read the information\n  below about Usage and Option sections. See `issue 102\n  \u003chttps://github.com/docopt/docopt/issues/102\u003e`_ for more info.\n\nPlease see the `Python version's README \u003chttps://github.com/docopt/docopt/blob/master/README.rst\u003e`_ \nfor details of any new and breaking changes that are not specific to the PHP version.\n\nThere is also at least one significant known issue with the upstream Python version. Due\nto the porting strategy used for the PHP version, it inherits the bug surface of the Python\nversion (and if it doesn't, that's actually a bug!):\n\n- Issues with multi-word argument and option values (`PHP report \u003chttps://github.com/docopt/docopt.php/issues/21\u003e`_,\n  `Upstream report \u003chttps://github.com/docopt/docopt/issues/207\u003e`_)\n\n-----\n\nIsn't it awesome how ``optparse`` and ``argparse`` generate help\nmessages based on your code?!\n\n*Hell no!*  You know what's awesome?  It's when the option parser *is*\ngenerated based on the beautiful help message that you write yourself!\nThis way you don't need to write this stupid repeatable parser-code,\nand instead can write only the help message--*the way you want it*.\n\n**docopt** helps you create most beautiful command-line interfaces\n*easily*:\n\n.. code:: php\n\n    \u003c?php\n    $doc = \u003c\u003c\u003cDOC\n    Naval Fate.\n   \n    Usage:\n      naval_fate.php ship new \u003cname\u003e...\n      naval_fate.php ship \u003cname\u003e move \u003cx\u003e \u003cy\u003e [--speed=\u003ckn\u003e]\n      naval_fate.php ship shoot \u003cx\u003e \u003cy\u003e\n      naval_fate.php mine (set|remove) \u003cx\u003e \u003cy\u003e [--moored | --drifting]\n      naval_fate.php (-h | --help)\n      naval_fate.php --version\n   \n    Options:\n      -h --help     Show this screen.\n      --version     Show version.\n      --speed=\u003ckn\u003e  Speed in knots [default: 10].\n      --moored      Moored (anchored) mine.\n      --drifting    Drifting mine.\n   \n    DOC;\n    \n    require('path/to/src/docopt.php');\n    $args = Docopt::handle($doc, array('version'=\u003e'Naval Fate 2.0'));\n    foreach ($args as $k=\u003e$v)\n        echo $k.': '.json_encode($v).PHP_EOL;\n\n\nBeat that! The option parser is generated based on the docstring above\nthat is passed to ``docopt`` function.  ``docopt`` parses the usage\npattern (``\"Usage: ...\"``) and option descriptions (lines starting\nwith dash \"``-``\") and ensures that the program invocation matches the\nusage pattern; it parses options, arguments and commands based on\nthat. The basic idea is that *a good help message has all necessary\ninformation in it to make a parser*.\n\n\nInstallation\n======================================================================\n\nInstall ``docopt.php`` using `Composer \u003chttp://getcomposer.org\u003e`_::\n\n    composer require docopt/docopt\n\nAlternatively, you can just drop ``docopt.php`` file into your project--it is\nself-contained. `Get source on github \u003chttp://github.com/docopt/docopt.php\u003e`_.\n\n``docopt.php`` is tested with PHP 7; it should still work with PHP 5.3+ but this support\nwill become increasingly fragile and will at some point cease to be supported at all. You\nshould update to 7 as soon as you can.\n\n\nTesting\n======================================================================\n\nConfigure your repo for running tests::\n\n    ./dev-setup\n\nYou can run unit tests with the following command::\n\n    php test.php\n\nThis will run the Python language agnostic tests as well as the PHP\ndocopt tests.\n\n\nAPI\n======================================================================\n\n.. code:: php\n\n    \u003c?php\n    require('/path/to/src/docopt.php');\n    \n    // short form, simple API\n    $args = Docopt::handle($doc);\n   \n    // short form (5.4 or better)\n    $args = (new \\Docopt\\Handler)-\u003ehandle($sdoc);\n   \n    // long form, simple API (equivalent to short)\n    $params = array(\n        'argv'=\u003earray_slice($_SERVER['argv'], 1),\n        'help'=\u003etrue,\n        'version'=\u003enull,\n        'optionsFirst'=\u003efalse,\n    );\n    $args = Docopt::handle($doc, $params);\n    \n    // long form, full API\n    $handler = new \\Docopt\\Handler(array(\n        'help'=\u003etrue,\n        'optionsFirst'=\u003efalse,\n    ));\n    $handler-\u003ehandle($doc, $argv);\n\n\n``Docopt::handle()`` takes 1 required and 1 optional argument:\n\n- ``doc`` is a string that contains a **help message** that will be parsed to\n  create the option parser.  The simple rules of how to write such a\n  help message are given in next sections.  Here is a quick example of\n  such a string:\n\n.. code:: php\n    \n    \u003c?php\n    $doc = \u003c\u003c\u003cDOC\n    Usage: my_program.php [-hso FILE] [--quiet | --verbose] [INPUT ...]\n    \n    Options:\n      -h --help    show this\n      -s --sorted  sorted output\n      -o FILE      specify output file [default: ./test.txt]\n      --quiet      print less text\n      --verbose    print more text\n   \n    DOC;\n\n\n- ``params`` is an optional array of additional data to influence\n  ``docopt``. The following keys are supported: \n\n  - ``argv`` is an optional argument vector; by default ``docopt`` uses\n    the argument vector passed to your program (``$_SERVER['argv']``).\n    Alternatively you can supply a list of strings like ``array('--verbose',\n    '-o', 'hai.txt')``.\n\n  - ``help``, by default ``true``, specifies whether the parser should\n      automatically print the help message (supplied as ``doc``) and\n      terminate, in case ``-h`` or ``--help`` option is encountered\n      (options should exist in usage pattern, more on that below). If you\n      want to handle ``-h`` or ``--help`` options manually (as other\n      options), set ``help`` to ``false``.\n\n  - ``version``, by default ``null``, is an optional argument that\n    specifies the version of your program. If supplied, then, (assuming\n    ``--version`` option is mentioned in usage pattern) when parser\n    encounters the ``--version`` option, it will print the supplied\n    version and terminate.  ``version`` could be any printable object,\n    but most likely a string, e.g. ``\"2.1.0rc1\"``.\n\n    Note, when ``docopt`` is set to automatically handle ``-h``,\n    ``--help`` and ``--version`` options, you still need to mention\n    them in usage pattern for this to work. Also, for your users to\n    know about them.\n\n  - ``optionsFirst``, by default ``false``.  If set to ``true`` will\n    disallow mixing options and positional argument.  I.e. after first\n    positional argument, all arguments will be interpreted as positional\n    even if the look like options.  This can be used for strict\n    compatibility with POSIX, or if you want to dispatch your arguments\n    to other programs.\n\n``Docopt\\Handler-\u003ehandle()`` takes one required argument:\n\n- ``doc`` is a string that contains a **help message** that will be parsed to\n  create the option parser.  The simple rules of how to write such a\n  help message are given in next sections.  Here is a quick example of\n  such a string:\n\n.. code:: php\n    \n    \u003c?php\n    $doc = \u003c\u003c\u003cDOC\n    Usage: my_program.php [-hso FILE] [--quiet | --verbose] [INPUT ...]\n   \n    -h --help    show this\n    -s --sorted  sorted output\n    -o FILE      specify output file [default: ./test.txt]\n    --quiet      print less text\n    --verbose    print more text\n   \n    DOC;\n\n\nThe **return** value of ``handle()`` is a simple associative array with \noptions, arguments and commands as keys, spelled exactly like in your \nhelp message. Long versions of options are given priority. For example, \nif you invoke the top example as::\n\n    naval_fate.php ship Guardian move 100 150 --speed=15\n\nthe return dictionary will be:\n\n.. code:: php\n\n    \u003c?php\n    array(\n      '--drifting'=\u003efalse,         'mine'=\u003efalse,\n      '--help'=\u003efalse,             'move'=\u003etrue,\n      '--moored'=\u003efalse,           'new'=\u003efalse,\n      '--speed'=\u003e'15',             'remove'=\u003efalse,\n      '--version'=\u003efalse,          'set'=\u003efalse,\n      '\u003cname\u003e'=\u003earray('Guardian'), 'ship'=\u003etrue,\n      '\u003cx\u003e'=\u003e'100',                'shoot'=\u003efalse,\n      '\u003cy\u003e'=\u003e'150'\n    );\n\n\nHelp message format\n======================================================================\n\nHelp message consists of 2 sections:\n\n- Usage section, starting with ``Usage:`` e.g.::\n\n    Usage: my_program.php [-hso FILE] [--quiet | --verbose] [INPUT ...]\n\n- Option section, starting with ``Options:`` e.g.::\n\n    Options:\n      -h --help    show this\n      -s --sorted  sorted output\n      -o FILE      specify output file [default: ./test.txt]\n      --quiet      print less text\n      --verbose    print more text\n\nSections consist of a header and a body. The section body can begin on\nthe same line as the header, but if it spans multiple lines, it must be \nindented. A section is terminated by an empty line or a string with no\nindentation::\n\n    Section header: Section body\n    \n    Section header:\n      Section body, which is indented at least\n      one space or tab from the section header\n\n    Section header: Section body, which is indented at least\n      one space or tab from the section header\n\n\nUsage section format\n----------------------------------------------------------------------\n\nMinimum example::\n\n    Usage: my_program.php\n\n\nThe first word after ``usage:`` is interpreted as your program's name.\nYou can specify your program's name several times to signify several\nexclusive patterns::\n\n    Usage: my_program.php FILE\n           my_program.php COUNT FILE\n\nEach pattern can consist of the following elements:\n\n- **\u003carguments\u003e**, **ARGUMENTS**. Arguments are specified as either\n  upper-case words, e.g. ``my_program.php CONTENT-PATH`` or words\n  surrounded by angular brackets: ``my_program.php \u003ccontent-path\u003e``.\n  \n- **--options**.  Options are words started with dash (``-``), e.g.\n  ``--output``, ``-o``.  You can \"stack\" several of one-letter\n  options, e.g. ``-oiv`` which will be the same as ``-o -i -v``. The\n  options can have arguments, e.g.  ``--input=FILE`` or ``-i FILE`` or\n  even ``-iFILE``. However it is important that you specify option\n  descriptions if you want your option to have an argument, a default\n  value, or specify synonymous short/long versions of option (see next\n  section on option descriptions).\n  \n- **commands** are words that do *not* follow the described above\n  conventions of ``--options`` or ``\u003carguments\u003e`` or ``ARGUMENTS``,\n  plus two special commands: dash \"``-``\" and double dash \"``--``\"\n  (see below).\n\nUse the following constructs to specify patterns:\n\n- **[ ]** (brackets) **optional** elements.  e.g.: ``my_program.php\n  [-hvqo FILE]``\n  \n- **( )** (parens) **required** elements.  All elements that are *not*\n  put in **[ ]** are also required, e.g.: ``my_program.php\n  --path=\u003cpath\u003e \u003cfile\u003e...`` is the same as ``my_program.php\n  (--path=\u003cpath\u003e \u003cfile\u003e...)``.  (Note, \"required options\" might be not\n  a good idea for your users).\n  \n- **|** (pipe) **mutually exclusive** elements. Group them using **(\n  )** if one of the mutually exclusive elements is required:\n  ``my_program.php (--clockwise | --counter-clockwise) TIME``. Group\n  them using **[ ]** if none of the mutually-exclusive elements are\n  required: ``my_program.php [--left | --right]``.\n  \n- **...** (ellipsis) **one or more** elements. To specify that\n  arbitrary number of repeating elements could be accepted, use\n  ellipsis (``...``), e.g.  ``my_program.php FILE ...`` means one or\n  more ``FILE``-s are accepted.  If you want to accept zero or more\n  elements, use brackets, e.g.: ``my_program.php [FILE ...]``. Ellipsis\n  works as a unary operator on the expression to the left.\n  \n- **[options]** (case sensitive) shortcut for any options.  You can\n  use it if you want to specify that the usage pattern could be\n  provided with any options defined below in the option-descriptions\n  and do not want to enumerate them all in usage-pattern.\n  \"``[--]``\". Double dash \"``--``\" is used by convention to separate\n  positional arguments that can be mistaken for options. In order to\n  support this convention add \"``[--]``\" to you usage patterns.\n  \"``[-]``\". Single dash \"``-``\" is used by convention to signify that\n  ``stdin`` is used instead of a file. To support this add \"``[-]``\"\n  to you usage patterns. \"``-``\" act as a normal command.\n\nIf your pattern allows to match argument-less option (a flag) several\ntimes::\n\n    Usage: my_program.php [-v | -vv | -vvv]\n\nthen number of occurrences of the option will be counted. I.e.\n``args['-v']`` will be ``2`` if program was invoked as ``my_program\n-vv``. Same works for commands.\n\nIf your usage patterns allows to match same-named option with argument\nor positional argument several times, the matched arguments will be\ncollected into a list::\n\n    Usage: my_program.php \u003cfile\u003e \u003cfile\u003e --path=\u003cpath\u003e...\n\nI.e. invoked with ``my_program.php file1 file2 --path=./here\n--path=./there`` the returned dict will contain ``args['\u003cfile\u003e'] ==\n['file1', 'file2']`` and ``args['--path'] == ['./here', './there']``.\n\n\nOptions section format\n----------------------------------------------------------------------\n\nThe **Option section** is an optional section that contains a list of \noptions that can document or supplement your usage pattern.\n\nIt is necessary to list option descriptions in order to specify:\n\n- synonymous short and long options,\n- if an option has an argument,\n- if option's argument has a default value.\n\nThe rules are as follows:\n\n- Every line in the options section body that starts with one or more\n  horizontal whitespace characters, followed by ``-`` or ``--`` is treated\n  as an option description, e.g.::\n\n    Options:\n      --verbose   # GOOD\n      -o FILE     # GOOD\n    Other: --bad  # BAD, line does not start with dash \"-\"\n\n- To specify that option has an argument, put a word describing that\n  argument after space (or equals \"``=``\" sign) as shown below. Follow\n  either \u003cangular-brackets\u003e or UPPER-CASE convention for options'\n  arguments.  You can use comma if you want to separate options. In\n  the example below, both lines are valid, however you are recommended\n  to stick to a single style.::\n\n    -o FILE --output=FILE       # without comma, with \"=\" sign\n    -i \u003cfile\u003e, --input \u003cfile\u003e   # with comma, wihtout \"=\" sign\n\n- Use two spaces to separate options with their informal description::\n\n    --verbose More text.   # BAD, will be treated as if verbose option had\n                           # an argument \"More\", so use 2 spaces instead\n    -q        Quit.        # GOOD\n    -o FILE   Output file. # GOOD\n    --stdout  Use stdout.  # GOOD, 2 spaces\n\n- If you want to set a default value for an option with an argument,\n  put it into the option-description, in form ``[default:\n  \u003cmy-default-value\u003e]``::\n\n    --coefficient=K  The K coefficient [default: 2.95]\n    --output=FILE    Output file [default: test.txt]\n    --directory=DIR  Some directory [default: ./]\n\n- If the option is not repeatable, the value inside ``[default: ...]``\n  will be interpreted as string.  If it *is* repeatable, it will be\n  splited into a list on whitespace::\n\n    Usage: my_program.php [--repeatable=\u003carg\u003e --repeatable=\u003carg\u003e]\n                          [--another-repeatable=\u003carg\u003e]...\n                          [--not-repeatable=\u003carg\u003e]\n\n    # will be ['./here', './there']\n    --repeatable=\u003carg\u003e          [default: ./here ./there]\n\n    # will be ['./here']\n    --another-repeatable=\u003carg\u003e  [default: ./here]\n\n    # will be './here ./there', because it is not repeatable\n    --not-repeatable=\u003carg\u003e      [default: ./here ./there]\n\n\nExamples\n----------------------------------------------------------------------\n\nWe have an extensive list of `examples\n\u003chttps://github.com/docopt/docopt/tree/master/examples\u003e`_ which cover\nevery aspect of functionality of **docopt**.  Try them out, read the\nsource if in doubt.\n\n\nSubparsers, multi-level help and *huge* applications (like git)\n----------------------------------------------------------------------\n\nIf you want to split your usage-pattern into several, implement\nmulti-level help (with separate help-screen for each subcommand),\nwant to interface with existing scripts that don't use **docopt**, or\nyou're building the next \"git\", you will need the new ``options_first``\nparameter (described in API section above). To get you started quickly\nwe implemented a subset of git command-line interface as an example:\n`examples/git\n\u003chttps://github.com/docopt/docopt/tree/master/examples/git\u003e`_\n\n\nData validation\n----------------------------------------------------------------------\n\n**docopt** does one thing and does it well: it implements your\ncommand-line interface.  However it does not validate the input data.\nYou should supplement docopt with a validation library when your \nvalidation requirements extend beyond whether input is optional or required.\n\n\nDevelopment\n======================================================================\n\nSee the `Python version's page \u003chttp://github.com/docopt/docopt\u003e`_ for more info \non developing.\n","funding_links":[],"categories":["PHP"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdocopt%2Fdocopt.php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdocopt%2Fdocopt.php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdocopt%2Fdocopt.php/lists"}