{"id":28726882,"url":"https://github.com/docopt/docopt.java","last_synced_at":"2025-06-15T13:09:34.065Z","repository":{"id":18821205,"uuid":"22036185","full_name":"docopt/docopt.java","owner":"docopt","description":"Java port of docopt","archived":false,"fork":false,"pushed_at":"2016-10-10T15:03:39.000Z","size":244,"stargazers_count":157,"open_issues_count":9,"forks_count":18,"subscribers_count":17,"default_branch":"master","last_synced_at":"2024-05-09T19:19:23.890Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","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","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-07-20T15:33:31.000Z","updated_at":"2024-04-29T00:45:52.000Z","dependencies_parsed_at":"2022-09-24T18:31:23.619Z","dependency_job_id":null,"html_url":"https://github.com/docopt/docopt.java","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/docopt/docopt.java","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/docopt%2Fdocopt.java","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/docopt%2Fdocopt.java/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/docopt%2Fdocopt.java/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/docopt%2Fdocopt.java/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/docopt","download_url":"https://codeload.github.com/docopt/docopt.java/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/docopt%2Fdocopt.java/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259981502,"owners_count":22941150,"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":[],"created_at":"2025-06-15T13:09:28.075Z","updated_at":"2025-06-15T13:09:34.051Z","avatar_url":"https://github.com/docopt.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"``docopt.java`` is a Java port of docopt\n======================================================================\n\nIsn't it awesome how ``Apache Commons CLI`` and the dozens of other Java command\nline parsers generate help messages 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.java** helps you create most beautiful command-line interfaces\n*easily*:\n\n.. code:: java\n\n  import java.util.Map;\n      \n  import org.docopt.Docopt;\n      \n  public final class NavalFate {\n      \n  private static final String doc =\n      \"Naval Fate.\\n\"\n      + \"\\n\"\n      + \"Usage:\\n\"\n      + \"  naval_fate ship new \u003cname\u003e...\\n\"\n      + \"  naval_fate ship \u003cname\u003e move \u003cx\u003e \u003cy\u003e [--speed=\u003ckn\u003e]\\n\"\n      + \"  naval_fate ship shoot \u003cx\u003e \u003cy\u003e\\n\"\n      + \"  naval_fate mine (set|remove) \u003cx\u003e \u003cy\u003e [--moored | --drifting]\\n\"\n      + \"  naval_fate (-h | --help)\\n\"\n      + \"  naval_fate --version\\n\"\n      + \"\\n\"\n      + \"Options:\\n\"\n      + \"  -h --help     Show this screen.\\n\"\n      + \"  --version     Show version.\\n\"\n      + \"  --speed=\u003ckn\u003e  Speed in knots [default: 10].\\n\"\n      + \"  --moored      Moored (anchored) mine.\\n\"\n      + \"  --drifting    Drifting mine.\\n\"\n      + \"\\n\";\n  \n      public static void main(String[] args) {\n        Map\u003cString, Object\u003e opts =\n          new Docopt(doc).withVersion(\"Naval Fate 2.0\").parse(args);\n        System.out.println(opts);\n      }\n  }\n\nDifferences from the reference implementation\n======================================================================\n\n- Because Java does not support optional or named arguments, this port uses the\n  Builder pattern to configure the parser instead of a simple method call.\n\n- Because Java does not provide a way (native) way to read a class's Javadoc,\n  there is no idiomatic way to supply the ``doc`` or ``version`` arguments.\n  This implementation provides convenience methods to read these values from\n  streams (e.g. files within the JAR) in addition to accepting String arguments.\n\n- Because Java does not provide a way to get command line arguments other than\n  in a ``main`` method, the ``argv`` parameter is required.\n  \n- Exiting the application when parsing arguments has been made optional. See the\n  ``withExit`` method.  \n\nInstallation\n======================================================================\n\nYou can build a JAR using `Maven http://maven.apache.org/` and include it as a\ndependency in your project. **docopt.java** is not currently available from Maven\ncentral.\n\nAlternatevely, you can just copy the ``org.docopt`` package into your project--it\nis self-contained.\n\n**docopt.java** is tested with Java 6 and Java 7.\n\nAPI\n======================================================================\n\n.. code:: java\n\n  import org.docopt.Docopt;\n  \n.. code:: java\n\n  public Docopt(String doc)\n\n``Docopt`` 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:: java\n\n  static final String doc =\n      \"Usage: my_program [-hso FILE] [--quiet | --verbose] [INPUT ...]\\n\"\n      + \"\\n\"\n      + \"-h --help    show this\\n\"\n      + \"-s --sorted  sorted output\\n\"\n      + \"-o FILE      specify output file [default: ./test.txt]\\n\"\n      + \"--quiet      print less text\\n\"\n      + \"--verbose    print more text\\n\"\n      + \"\\n\";\n\n.. code:: java\n\n  public Docopt(String doc)\n  public Docopt(InputStream doc)\n  public Docopt(InputStream doc, Charset charset)\n\nConstructs an option parser from the ``doc`` argument or throws a\n``DocoptLanguageError`` if it is malformed. If ``doc`` is an ``InputStream``,\nthe stream is read using the specified ``CharSet`` (``UTF-8`` by default).\n\n.. code:: java\n\n  public Map\u003cString, Object\u003e parse(List\u003cString\u003e argv)\n  public Map\u003cString, Object\u003e parse(String... argv)\n  \n``parse`` takes one required argument:\n\n- ``argv`` is an argument vector. The vector may be given as a ``List`` or as an\n  array of ``Strings``. *Note that calling this method with no argument is\n  equivalent to a giving an empty array!*\n\nThe **return** value is a ``Map`` with options, arguments, and commands as keys,\nspelled exactly like in your help message. Long versions of options are given\npriority. For example, if you invoke the top example as::\n\n  naval_fate.py ship Guardian move 100 150 --speed=15\n\nthe return ``Map`` will be:\n\n.. code:: java\n\n  {--version=false,     remove=false,\n   --speed=15,          ship=true,\n   \u003cname\u003e=[Guardian],   set=false,\n   \u003cy\u003e=150,             \u003cx\u003e=100,\n   --moored=false,      new=false,\n   --drifting=false,    shoot=false,\n   mine=false,          --help=false,\n   move=true}\n\n.. code:: java\n\n  public Docopt withHelp(boolean help)\n\n``withHelp`` takes one required argument:\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), invoke ``withHelp(false)``.\n\n    Note, when ``docopt`` is set to automatically handle the ``-h`` and\n    ``--help`` options, you still need to mention them in usage pattern for this\n    to work. Also, for your users to know about them.\n\n.. code:: java\n\n  public Docopt withVersion(String version)\n\n- ``version``, by default ``null``, specifies the version of your program. If\n  supplied, then, (assuming ``--version`` option is mentioned in usage pattern)\n  when parser encounters the ``--version`` option, it will print the supplied\n  version and terminate.\n\n    Note, when ``docopt`` is set to automatically handle the ``--version``\n    option, you still need to mention it in usage pattern for this to work.\n    Also, for your users to know about them.\n\n.. code:: java\n\n  public Docopt withOptionsFirst(boolean optionsFirst)\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.. code:: java\n\n  public Docopt withExit(boolean exit)\n\n- ``exit``, by default ``true``. If set to ``false`` will cause ``parse`` to\n  throw a ``DocoptExit`` exception instead of terminating the application.\n\nHelp message format\n======================================================================\n\nHelp message consists of 2 parts:\n\n- Usage pattern, e.g.::\n\n    Usage: my_program [-hso FILE] [--quiet | --verbose] [INPUT ...]\n\n- Option descriptions, e.g.::\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\nTheir format is described below; other text is ignored.\n\nUsage pattern format\n----------------------------------------------------------------------\n\n**Usage pattern** is a substring of ``doc`` that starts with\n``usage:`` (case *insensitive*) and ends with a *visibly* empty line.\nMinimum example:\n\n.. code:: java\n\n    static final String USAGE = \"Usage: my_program\";\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.. code:: java\n\n  static final String USAGE = \n      \"Usage: my_program FILE\\n\" +\n      \"       my_program 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 CONTENT-PATH`` or words\n  surrounded by angular brackets: ``my_program \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 the option (see\n  next 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\n  [-hvqo FILE]``\n\n- **( )** (parens) **required** elements.  All elements that are *not*\n  put in **[ ]** are also required, e.g.: ``my_program\n  --path=\u003cpath\u003e \u003cfile\u003e...`` is the same as ``my_program\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 (--clockwise | --counter-clockwise) TIME``. Group\n  them using **[ ]** if none of the mutually-exclusive elements are\n  required: ``my_program [--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 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 [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\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 your usage patterns.\n\n- \"``[-]``\". Single dash \"``-``\" is used by convention to signify that\n  ``stdin`` is used instead of a file. To support this add \"``[-]``\"\n  to your usage patterns. \"``-``\" acts as a normal command.\n\nIf your pattern allows to match argument-less option (a flag) several\ntimes::\n\n  Usage: my_program [-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 \u003cfile\u003e \u003cfile\u003e --path=\u003cpath\u003e...\n\nI.e. invoked with ``my_program 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\nOption descriptions format\n----------------------------------------------------------------------\n\n**Option descriptions** consist of a list of options that you put\nbelow your usage patterns.\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 ``doc`` that starts with ``-`` or ``--`` (not counting\n  spaces) is treated 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, without \"=\" sing\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 [--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\nChangelog\n======================================================================\n\n**docopt.java** follows `semantic versioning \u003chttp://semver.org\u003e`_.\n\n- 0.6.0 Initial port based on version 0.6.1 of the `reference implementation\n  \u003chttps://github.com/docopt/docopt\u003e`_. All language agnostic tests pass.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdocopt%2Fdocopt.java","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdocopt%2Fdocopt.java","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdocopt%2Fdocopt.java/lists"}