PKG-INFO 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. Metadata-Version: 2.1
  2. Name: docopt
  3. Version: 0.6.2
  4. Summary: Pythonic argument parser, that will make you smile
  5. Home-page: http://docopt.org
  6. Author: Vladimir Keleshev
  7. Author-email: vladimir@keleshev.com
  8. License: MIT
  9. Keywords: option arguments parsing optparse argparse getopt
  10. Platform: UNKNOWN
  11. Classifier: Development Status :: 3 - Alpha
  12. Classifier: Topic :: Utilities
  13. Classifier: Programming Language :: Python :: 2.5
  14. Classifier: Programming Language :: Python :: 2.6
  15. Classifier: Programming Language :: Python :: 2.7
  16. Classifier: Programming Language :: Python :: 3.2
  17. Classifier: Programming Language :: Python :: 3.3
  18. Classifier: License :: OSI Approved :: MIT License
  19. License-File: LICENSE-MIT
  20. ``docopt`` creates *beautiful* command-line interfaces
  21. ======================================================================
  22. Video introduction to **docopt**: `PyCon UK 2012: Create *beautiful*
  23. command-line interfaces with Python <http://youtu.be/pXhcPJK5cMc>`_
  24. New in version 0.6.1:
  25. - Fix issue `#85 <https://github.com/docopt/docopt/issues/85>`_
  26. which caused improper handling of ``[options]`` shortcut
  27. if it was present several times.
  28. New in version 0.6.0:
  29. - New argument ``options_first``, disallows interspersing options
  30. and arguments. If you supply ``options_first=True`` to
  31. ``docopt``, it will interpret all arguments as positional
  32. arguments after first positional argument.
  33. - If option with argument could be repeated, its default value
  34. will be interpreted as space-separated list. E.g. with
  35. ``[default: ./here ./there]`` will be interpreted as
  36. ``['./here', './there']``.
  37. Breaking changes:
  38. - Meaning of ``[options]`` shortcut slightly changed. Previously
  39. it ment *"any known option"*. Now it means *"any option not in
  40. usage-pattern"*. This avoids the situation when an option is
  41. allowed to be repeated unintentionaly.
  42. - ``argv`` is ``None`` by default, not ``sys.argv[1:]``.
  43. This allows ``docopt`` to always use the *latest* ``sys.argv``,
  44. not ``sys.argv`` during import time.
  45. Isn't it awesome how ``optparse`` and ``argparse`` generate help
  46. messages based on your code?!
  47. *Hell no!* You know what's awesome? It's when the option parser *is*
  48. generated based on the beautiful help message that you write yourself!
  49. This way you don't need to write this stupid repeatable parser-code,
  50. and instead can write only the help message--*the way you want it*.
  51. **docopt** helps you create most beautiful command-line interfaces
  52. *easily*:
  53. .. code:: python
  54. """Naval Fate.
  55. Usage:
  56. naval_fate.py ship new <name>...
  57. naval_fate.py ship <name> move <x> <y> [--speed=<kn>]
  58. naval_fate.py ship shoot <x> <y>
  59. naval_fate.py mine (set|remove) <x> <y> [--moored | --drifting]
  60. naval_fate.py (-h | --help)
  61. naval_fate.py --version
  62. Options:
  63. -h --help Show this screen.
  64. --version Show version.
  65. --speed=<kn> Speed in knots [default: 10].
  66. --moored Moored (anchored) mine.
  67. --drifting Drifting mine.
  68. """
  69. from docopt import docopt
  70. if __name__ == '__main__':
  71. arguments = docopt(__doc__, version='Naval Fate 2.0')
  72. print(arguments)
  73. Beat that! The option parser is generated based on the docstring above
  74. that is passed to ``docopt`` function. ``docopt`` parses the usage
  75. pattern (``"Usage: ..."``) and option descriptions (lines starting
  76. with dash "``-``") and ensures that the program invocation matches the
  77. usage pattern; it parses options, arguments and commands based on
  78. that. The basic idea is that *a good help message has all necessary
  79. information in it to make a parser*.
  80. Also, `PEP 257 <http://www.python.org/dev/peps/pep-0257/>`_ recommends
  81. putting help message in the module docstrings.
  82. Installation
  83. ======================================================================
  84. Use `pip <http://pip-installer.org>`_ or easy_install::
  85. pip install docopt==0.6.2
  86. Alternatively, you can just drop ``docopt.py`` file into your
  87. project--it is self-contained.
  88. **docopt** is tested with Python 2.5, 2.6, 2.7, 3.2, 3.3 and PyPy.
  89. API
  90. ======================================================================
  91. .. code:: python
  92. from docopt import docopt
  93. .. code:: python
  94. docopt(doc, argv=None, help=True, version=None, options_first=False)
  95. ``docopt`` takes 1 required and 4 optional arguments:
  96. - ``doc`` could be a module docstring (``__doc__``) or some other
  97. string that contains a **help message** that will be parsed to
  98. create the option parser. The simple rules of how to write such a
  99. help message are given in next sections. Here is a quick example of
  100. such a string:
  101. .. code:: python
  102. """Usage: my_program.py [-hso FILE] [--quiet | --verbose] [INPUT ...]
  103. -h --help show this
  104. -s --sorted sorted output
  105. -o FILE specify output file [default: ./test.txt]
  106. --quiet print less text
  107. --verbose print more text
  108. """
  109. - ``argv`` is an optional argument vector; by default ``docopt`` uses
  110. the argument vector passed to your program (``sys.argv[1:]``).
  111. Alternatively you can supply a list of strings like ``['--verbose',
  112. '-o', 'hai.txt']``.
  113. - ``help``, by default ``True``, specifies whether the parser should
  114. automatically print the help message (supplied as ``doc``) and
  115. terminate, in case ``-h`` or ``--help`` option is encountered
  116. (options should exist in usage pattern, more on that below). If you
  117. want to handle ``-h`` or ``--help`` options manually (as other
  118. options), set ``help=False``.
  119. - ``version``, by default ``None``, is an optional argument that
  120. specifies the version of your program. If supplied, then, (assuming
  121. ``--version`` option is mentioned in usage pattern) when parser
  122. encounters the ``--version`` option, it will print the supplied
  123. version and terminate. ``version`` could be any printable object,
  124. but most likely a string, e.g. ``"2.1.0rc1"``.
  125. Note, when ``docopt`` is set to automatically handle ``-h``,
  126. ``--help`` and ``--version`` options, you still need to mention
  127. them in usage pattern for this to work. Also, for your users to
  128. know about them.
  129. - ``options_first``, by default ``False``. If set to ``True`` will
  130. disallow mixing options and positional argument. I.e. after first
  131. positional argument, all arguments will be interpreted as positional
  132. even if the look like options. This can be used for strict
  133. compatibility with POSIX, or if you want to dispatch your arguments
  134. to other programs.
  135. The **return** value is a simple dictionary with options, arguments
  136. and commands as keys, spelled exactly like in your help message. Long
  137. versions of options are given priority. For example, if you invoke the
  138. top example as::
  139. naval_fate.py ship Guardian move 100 150 --speed=15
  140. the return dictionary will be:
  141. .. code:: python
  142. {'--drifting': False, 'mine': False,
  143. '--help': False, 'move': True,
  144. '--moored': False, 'new': False,
  145. '--speed': '15', 'remove': False,
  146. '--version': False, 'set': False,
  147. '<name>': ['Guardian'], 'ship': True,
  148. '<x>': '100', 'shoot': False,
  149. '<y>': '150'}
  150. Help message format
  151. ======================================================================
  152. Help message consists of 2 parts:
  153. - Usage pattern, e.g.::
  154. Usage: my_program.py [-hso FILE] [--quiet | --verbose] [INPUT ...]
  155. - Option descriptions, e.g.::
  156. -h --help show this
  157. -s --sorted sorted output
  158. -o FILE specify output file [default: ./test.txt]
  159. --quiet print less text
  160. --verbose print more text
  161. Their format is described below; other text is ignored.
  162. Usage pattern format
  163. ----------------------------------------------------------------------
  164. **Usage pattern** is a substring of ``doc`` that starts with
  165. ``usage:`` (case *insensitive*) and ends with a *visibly* empty line.
  166. Minimum example:
  167. .. code:: python
  168. """Usage: my_program.py
  169. """
  170. The first word after ``usage:`` is interpreted as your program's name.
  171. You can specify your program's name several times to signify several
  172. exclusive patterns:
  173. .. code:: python
  174. """Usage: my_program.py FILE
  175. my_program.py COUNT FILE
  176. """
  177. Each pattern can consist of the following elements:
  178. - **<arguments>**, **ARGUMENTS**. Arguments are specified as either
  179. upper-case words, e.g. ``my_program.py CONTENT-PATH`` or words
  180. surrounded by angular brackets: ``my_program.py <content-path>``.
  181. - **--options**. Options are words started with dash (``-``), e.g.
  182. ``--output``, ``-o``. You can "stack" several of one-letter
  183. options, e.g. ``-oiv`` which will be the same as ``-o -i -v``. The
  184. options can have arguments, e.g. ``--input=FILE`` or ``-i FILE`` or
  185. even ``-iFILE``. However it is important that you specify option
  186. descriptions if you want for option to have an argument, a default
  187. value, or specify synonymous short/long versions of option (see next
  188. section on option descriptions).
  189. - **commands** are words that do *not* follow the described above
  190. conventions of ``--options`` or ``<arguments>`` or ``ARGUMENTS``,
  191. plus two special commands: dash "``-``" and double dash "``--``"
  192. (see below).
  193. Use the following constructs to specify patterns:
  194. - **[ ]** (brackets) **optional** elements. e.g.: ``my_program.py
  195. [-hvqo FILE]``
  196. - **( )** (parens) **required** elements. All elements that are *not*
  197. put in **[ ]** are also required, e.g.: ``my_program.py
  198. --path=<path> <file>...`` is the same as ``my_program.py
  199. (--path=<path> <file>...)``. (Note, "required options" might be not
  200. a good idea for your users).
  201. - **|** (pipe) **mutualy exclusive** elements. Group them using **(
  202. )** if one of the mutually exclusive elements is required:
  203. ``my_program.py (--clockwise | --counter-clockwise) TIME``. Group
  204. them using **[ ]** if none of the mutually-exclusive elements are
  205. required: ``my_program.py [--left | --right]``.
  206. - **...** (ellipsis) **one or more** elements. To specify that
  207. arbitrary number of repeating elements could be accepted, use
  208. ellipsis (``...``), e.g. ``my_program.py FILE ...`` means one or
  209. more ``FILE``-s are accepted. If you want to accept zero or more
  210. elements, use brackets, e.g.: ``my_program.py [FILE ...]``. Ellipsis
  211. works as a unary operator on the expression to the left.
  212. - **[options]** (case sensitive) shortcut for any options. You can
  213. use it if you want to specify that the usage pattern could be
  214. provided with any options defined below in the option-descriptions
  215. and do not want to enumerate them all in usage-pattern. -
  216. "``[--]``". Double dash "``--``" is used by convention to separate
  217. positional arguments that can be mistaken for options. In order to
  218. support this convention add "``[--]``" to you usage patterns. -
  219. "``[-]``". Single dash "``-``" is used by convention to signify that
  220. ``stdin`` is used instead of a file. To support this add "``[-]``"
  221. to you usage patterns. "``-``" act as a normal command.
  222. If your pattern allows to match argument-less option (a flag) several
  223. times::
  224. Usage: my_program.py [-v | -vv | -vvv]
  225. then number of occurences of the option will be counted. I.e.
  226. ``args['-v']`` will be ``2`` if program was invoked as ``my_program
  227. -vv``. Same works for commands.
  228. If your usage patterns allows to match same-named option with argument
  229. or positional argument several times, the matched arguments will be
  230. collected into a list::
  231. Usage: my_program.py <file> <file> --path=<path>...
  232. I.e. invoked with ``my_program.py file1 file2 --path=./here
  233. --path=./there`` the returned dict will contain ``args['<file>'] ==
  234. ['file1', 'file2']`` and ``args['--path'] == ['./here', './there']``.
  235. Option descriptions format
  236. ----------------------------------------------------------------------
  237. **Option descriptions** consist of a list of options that you put
  238. below your usage patterns.
  239. It is necessary to list option descriptions in order to specify:
  240. - synonymous short and long options,
  241. - if an option has an argument,
  242. - if option's argument has a default value.
  243. The rules are as follows:
  244. - Every line in ``doc`` that starts with ``-`` or ``--`` (not counting
  245. spaces) is treated as an option description, e.g.::
  246. Options:
  247. --verbose # GOOD
  248. -o FILE # GOOD
  249. Other: --bad # BAD, line does not start with dash "-"
  250. - To specify that option has an argument, put a word describing that
  251. argument after space (or equals "``=``" sign) as shown below. Follow
  252. either <angular-brackets> or UPPER-CASE convention for options'
  253. arguments. You can use comma if you want to separate options. In
  254. the example below, both lines are valid, however you are recommended
  255. to stick to a single style.::
  256. -o FILE --output=FILE # without comma, with "=" sign
  257. -i <file>, --input <file> # with comma, wihtout "=" sing
  258. - Use two spaces to separate options with their informal description::
  259. --verbose More text. # BAD, will be treated as if verbose option had
  260. # an argument "More", so use 2 spaces instead
  261. -q Quit. # GOOD
  262. -o FILE Output file. # GOOD
  263. --stdout Use stdout. # GOOD, 2 spaces
  264. - If you want to set a default value for an option with an argument,
  265. put it into the option-description, in form ``[default:
  266. <my-default-value>]``::
  267. --coefficient=K The K coefficient [default: 2.95]
  268. --output=FILE Output file [default: test.txt]
  269. --directory=DIR Some directory [default: ./]
  270. - If the option is not repeatable, the value inside ``[default: ...]``
  271. will be interpeted as string. If it *is* repeatable, it will be
  272. splited into a list on whitespace::
  273. Usage: my_program.py [--repeatable=<arg> --repeatable=<arg>]
  274. [--another-repeatable=<arg>]...
  275. [--not-repeatable=<arg>]
  276. # will be ['./here', './there']
  277. --repeatable=<arg> [default: ./here ./there]
  278. # will be ['./here']
  279. --another-repeatable=<arg> [default: ./here]
  280. # will be './here ./there', because it is not repeatable
  281. --not-repeatable=<arg> [default: ./here ./there]
  282. Examples
  283. ----------------------------------------------------------------------
  284. We have an extensive list of `examples
  285. <https://github.com/docopt/docopt/tree/master/examples>`_ which cover
  286. every aspect of functionality of **docopt**. Try them out, read the
  287. source if in doubt.
  288. Subparsers, multi-level help and *huge* applications (like git)
  289. ----------------------------------------------------------------------
  290. If you want to split your usage-pattern into several, implement
  291. multi-level help (whith separate help-screen for each subcommand),
  292. want to interface with existing scripts that don't use **docopt**, or
  293. you're building the next "git", you will need the new ``options_first``
  294. parameter (described in API section above). To get you started quickly
  295. we implemented a subset of git command-line interface as an example:
  296. `examples/git
  297. <https://github.com/docopt/docopt/tree/master/examples/git>`_
  298. Data validation
  299. ----------------------------------------------------------------------
  300. **docopt** does one thing and does it well: it implements your
  301. command-line interface. However it does not validate the input data.
  302. On the other hand there are libraries like `python schema
  303. <https://github.com/halst/schema>`_ which make validating data a
  304. breeze. Take a look at `validation_example.py
  305. <https://github.com/docopt/docopt/tree/master/examples/validation_example.py>`_
  306. which uses **schema** to validate data and report an error to the
  307. user.
  308. Development
  309. ======================================================================
  310. We would *love* to hear what you think about **docopt** on our `issues
  311. page <http://github.com/docopt/docopt/issues>`_
  312. Make pull requrests, report bugs, suggest ideas and discuss
  313. **docopt**. You can also drop a line directly to
  314. <vladimir@keleshev.com>.
  315. Porting ``docopt`` to other languages
  316. ======================================================================
  317. We think **docopt** is so good, we want to share it beyond the Python
  318. community!
  319. The follosing ports are available:
  320. - `Ruby port <http://github.com/docopt/docopt.rb>`_
  321. - `CoffeeScript port <http://github.com/docopt/docopt.coffee>`_
  322. - `Lua port <http://github.com/docopt/docopt.lua>`_
  323. - `PHP port <http://github.com/docopt/docopt.php>`_
  324. But you can always create a port for your favorite language! You are
  325. encouraged to use the Python version as a reference implementation. A
  326. Language-agnostic test suite is bundled with `Python implementation
  327. <http://github.com/docopt/docopt>`_.
  328. Porting discussion is on `issues page
  329. <http://github.com/docopt/docopt/issues>`_.
  330. Changelog
  331. ======================================================================
  332. **docopt** follows `semantic versioning <http://semver.org>`_. The
  333. first release with stable API will be 1.0.0 (soon). Until then, you
  334. are encouraged to specify explicitly the version in your dependency
  335. tools, e.g.::
  336. pip install docopt==0.6.2
  337. - 0.6.2 `Wheel <http://pythonwheels.com/>`_ support.
  338. - 0.6.1 Bugfix release.
  339. - 0.6.0 ``options_first`` parameter.
  340. **Breaking changes**: Corrected ``[options]`` meaning.
  341. ``argv`` defaults to ``None``.
  342. - 0.5.0 Repeated options/commands are counted or accumulated into a
  343. list.
  344. - 0.4.2 Bugfix release.
  345. - 0.4.0 Option descriptions become optional,
  346. support for "``--``" and "``-``" commands.
  347. - 0.3.0 Support for (sub)commands like `git remote add`.
  348. Introduce ``[options]`` shortcut for any options.
  349. **Breaking changes**: ``docopt`` returns dictionary.
  350. - 0.2.0 Usage pattern matching. Positional arguments parsing based on
  351. usage patterns.
  352. **Breaking changes**: ``docopt`` returns namespace (for arguments),
  353. not list. Usage pattern is formalized.
  354. - 0.1.0 Initial release. Options-parsing only (based on options
  355. description).