11``docopt `` creates *beautiful * command-line interfaces
22======================================================================
33
4+ .. image :: https://travis-ci.org/docopt/docopt.php.svg?branch=master
5+
46This is a straight PHP transliteration of Vladimir Keleshev's brilliant
57`docopt <https://github.com/docopt/docopt/ >`_ Python library. There are a
68few artefacts in the code as a result that may seem inefficient and
@@ -9,34 +11,34 @@ efficient.
911
1012As a result, unless a bug is present only in the PHP version, pull requests
1113are unlikely to be accepted unless they are themselves direct transliterations
12- of bugfixes in the Python version.
14+ of bugfixes in the Python version.
1315
16+ **This port has been marked version 1.0.0 RC1 **. It is based on the Python version at
17+ commit `1937a1c9041e0f580d2890d38bb71c0a0623847f
18+ <https://github.com/docopt/docopt/commit/d5b96f878abbda51b62f1e28010d2b42b19a27dc> `_
19+ (labelled **0.6.1 **).
1420
15- Video introduction to **docopt **: `PyCon UK 2012: Create *beautiful*
16- command-line interfaces with Python <http://youtu.be/pXhcPJK5cMc> `_
21+ It has been quite stable for a long time and has barely been changed. The Python version
22+ receives only occasional bugfixes and keeping the version numbers pinned has been more
23+ trouble than it has been worth.
1724
18- New in version 0.6.0:
25+ There are also some major backward compatibility breaks. Rather than dwell in 0.x semver
26+ hell, the PHP port will liberally bump major numbers henceforth when BC breaks regardless
27+ of the reason.
1928
20- - New argument ``options_first ``, disallows interspersing options
21- and arguments. If you supply ``options_first=True `` to
22- ``docopt ``, it will interpret all arguments as positional
23- arguments after first positional argument.
29+ - The PHP API has changed slightly. ``Docopt\docopt() `` has been renamed to
30+ ``Docopt::handle() `` to fix autoloader support. See `issue #3
31+ <https://github.com/docopt/docopt.php/pull/3> `_.
2432
25- - If option with argument could be repeated, its default value
26- will be interpreted as space-separated list. E.g. with
27- ``[default: ./here ./there] `` will be interpreted as
28- ``['./here', './there'] ``.
33+ - Docopt.py also has a significant BC break. Existing users should read the information
34+ below about Usage and Option sections. See `issue 102
35+ <https://github.com/docopt/docopt/issues/102> `_ for more info.
2936
30- Breaking changes:
3137
32- - Meaning of ``[options] `` shortcut slightly changed. Previously
33- it ment *"any known option" *. Now it means *"any option not in
34- usage-pattern" *. This avoids the situation when an option is
35- allowed to be repeated unintentionaly.
38+ Please see the `Python version's README <https://github.com/docopt/docopt/blob/master/README.rst >`_
39+ for details of any new and breaking changes that are not specific to the PHP version.
3640
37- - ``argv `` is ``None `` by default, not ``sys.argv[1:] ``.
38- This allows ``docopt `` to always use the *latest * ``sys.argv ``,
39- not ``sys.argv `` during import time.
41+ -----
4042
4143Isn't it awesome how ``optparse `` and ``argparse `` generate help
4244messages based on your code?!
@@ -73,8 +75,7 @@ and instead can write only the help message--*the way you want it*.
7375 DOC;
7476
7577 require('path/to/src/docopt.php');
76- $handler = new \Docopt\Handler(array('version'=>'Naval Fate 2.0'));
77- $args = $handler->handle($doc);
78+ $args = Docopt::handle($doc, array('version'=>'Naval Fate 2.0'));
7879 foreach ($args as $k=>$v)
7980 echo $k.': '.json_encode($v).PHP_EOL;
8081
@@ -98,7 +99,7 @@ Create a ``composer.json`` file for your project
9899
99100 {
100101 " require" : {
101- " docopt/docopt" : " 0.6.0 "
102+ " docopt/docopt" : " 1.0.0-rc1 "
102103 }
103104 }
104105
@@ -107,10 +108,25 @@ Install using composer::
107108 php composer.phar install
108109
109110
110- Alternatively, you can just drop `docopt.php ` file into your project--it is
111+ Alternatively, you can just drop `` docopt.php ` ` file into your project--it is
111112self-contained. `Get source on github <http://github.com/docopt/docopt.php >`_.
112113
113- ``docopt.php `` is tested with PHP 5.3
114+ ``docopt.php `` is tested with PHP 5.4 and PHP 5.3.
115+
116+
117+ Testing
118+ ======================================================================
119+
120+ Configure your repo for running tests::
121+
122+ ./dev-setup
123+
124+ You can run unit tests with the following command::
125+
126+ php test.php
127+
128+ This will run the Python language agnostic tests as well as the PHP
129+ docopt tests.
114130
115131
116132API
@@ -121,24 +137,53 @@ API
121137 <?php
122138 require('/path/to/src/docopt.php');
123139
140+ // short form, simple API
141+ $args = Docopt::handle($doc);
142+
124143 // short form (5.4 or better)
125144 $args = (new \Docopt\Handler)->handle($sdoc);
126145
127- // long form (equivalent to short)
146+ // long form, simple API (equivalent to short)
128147 $params = array(
129148 'argv'=>array_slice($_SERVER['argv'], 1),
130149 'help'=>true,
131150 'version'=>null,
132151 'optionsFirst'=>false,
133152 );
134- $handler = new \Docopt\Handler($params);
135- $args = $handler->handle($doc);
153+ $args = Docopt::handle($doc, $params);
154+
155+ // long form, full API
156+ $handler = new \Docopt\Handler(array(
157+ 'help'=>true,
158+ 'optionsFirst'=>false,
159+ ));
160+ $handler->handle($doc, $argv);
136161
137162
138- The constructor for ``Docopt\Handler `` takes one optional argument (the
139- defaults are listed in the above example):
163+ ``Docopt::handle() `` takes 1 required and 1 optional argument:
164+
165+ - ``doc `` is a string that contains a **help message ** that will be parsed to
166+ create the option parser. The simple rules of how to write such a
167+ help message are given in next sections. Here is a quick example of
168+ such a string:
140169
141- - ``params `` is an array of additional data to influence
170+ .. code :: php
171+
172+ <?php
173+ $doc = <<<DOC
174+ Usage: my_program.php [-hso FILE] [--quiet | --verbose] [INPUT ...]
175+
176+ Options:
177+ -h --help show this
178+ -s --sorted sorted output
179+ -o FILE specify output file [default: . /test.txt]
180+ --quiet print less text
181+ --verbose print more text
182+
183+ DOC;
184+
185+
186+ - ``params`` is an optional array of additional data to influence
142187 ``docopt``. The following keys are supported:
143188
144189 - ``argv`` is an optional argument vector; by default ``docopt`` uses
@@ -221,27 +266,39 @@ the return dictionary will be:
221266 Help message format
222267======================================================================
223268
224- Help message consists of 2 parts :
269+ Help message consists of 2 sections :
225270
226- - Usage pattern, e.g.::
271+ - Usage section, starting with `` Usage: `` e.g.::
227272
228273 Usage: my_program.php [-hso FILE] [--quiet | --verbose] [INPUT ...]
229274
230- - Option descriptions, e.g.::
275+ - Option section, starting with `` Options: `` e.g.::
231276
232- -h --help show this
233- -s --sorted sorted output
234- -o FILE specify output file [default: ./test.txt]
235- --quiet print less text
236- --verbose print more text
277+ Options:
278+ -h --help show this
279+ -s --sorted sorted output
280+ -o FILE specify output file [default: ./test.txt]
281+ --quiet print less text
282+ --verbose print more text
283+
284+ Sections consist of a header and a body. The section body can begin on
285+ the same line as the header, but if it spans multiple lines, it must be
286+ indented. A section is terminated by an empty line or a string with no
287+ indentation::
288+
289+ Section header: Section body
290+
291+ Section header:
292+ Section body, which is indented at least
293+ one space or tab from the section header
294+
295+ Section header: Section body, which is indented at least
296+ one space or tab from the section header
237297
238- Their format is described below; other text is ignored.
239298
240- Usage pattern format
299+ Usage section format
241300----------------------------------------------------------------------
242301
243- **Usage pattern ** is a substring of ``doc `` that starts with
244- ``usage: `` (case *insensitive *) and ends with a *visibly * empty line.
245302Minimum example::
246303
247304 Usage: my_program.php
@@ -259,14 +316,16 @@ Each pattern can consist of the following elements:
259316- **<arguments> **, **ARGUMENTS **. Arguments are specified as either
260317 upper-case words, e.g. ``my_program.php CONTENT-PATH `` or words
261318 surrounded by angular brackets: ``my_program.php <content-path> ``.
319+
262320- **--options **. Options are words started with dash (``- ``), e.g.
263321 ``--output ``, ``-o ``. You can "stack" several of one-letter
264322 options, e.g. ``-oiv `` which will be the same as ``-o -i -v ``. The
265323 options can have arguments, e.g. ``--input=FILE `` or ``-i FILE `` or
266324 even ``-iFILE ``. However it is important that you specify option
267- descriptions if you want for option to have an argument, a default
325+ descriptions if you want your option to have an argument, a default
268326 value, or specify synonymous short/long versions of option (see next
269327 section on option descriptions).
328+
270329- **commands ** are words that do *not * follow the described above
271330 conventions of ``--options `` or ``<arguments> `` or ``ARGUMENTS ``,
272331 plus two special commands: dash "``- ``" and double dash "``-- ``"
@@ -276,29 +335,33 @@ Use the following constructs to specify patterns:
276335
277336- **[ ] ** (brackets) **optional ** elements. e.g.: ``my_program.php
278337 [-hvqo FILE] ``
338+
279339- **( ) ** (parens) **required ** elements. All elements that are *not *
280340 put in **[ ] ** are also required, e.g.: ``my_program.php
281341 --path=<path> <file>... `` is the same as ``my_program.php
282342 (--path=<path> <file>...) ``. (Note, "required options" might be not
283343 a good idea for your users).
284- - **| ** (pipe) **mutualy exclusive ** elements. Group them using **(
344+
345+ - **| ** (pipe) **mutually exclusive ** elements. Group them using **(
285346 ) ** if one of the mutually exclusive elements is required:
286347 ``my_program.php (--clockwise | --counter-clockwise) TIME ``. Group
287348 them using **[ ] ** if none of the mutually-exclusive elements are
288349 required: ``my_program.php [--left | --right] ``.
350+
289351- **... ** (ellipsis) **one or more ** elements. To specify that
290352 arbitrary number of repeating elements could be accepted, use
291353 ellipsis (``... ``), e.g. ``my_program.php FILE ... `` means one or
292354 more ``FILE ``-s are accepted. If you want to accept zero or more
293355 elements, use brackets, e.g.: ``my_program.php [FILE ...] ``. Ellipsis
294356 works as a unary operator on the expression to the left.
357+
295358- **[options] ** (case sensitive) shortcut for any options. You can
296359 use it if you want to specify that the usage pattern could be
297360 provided with any options defined below in the option-descriptions
298- and do not want to enumerate them all in usage-pattern. -
361+ and do not want to enumerate them all in usage-pattern.
299362 "``[--] ``". Double dash "``-- ``" is used by convention to separate
300363 positional arguments that can be mistaken for options. In order to
301- support this convention add "``[--] ``" to you usage patterns. -
364+ support this convention add "``[--] ``" to you usage patterns.
302365 "``[-] ``". Single dash "``- ``" is used by convention to signify that
303366 ``stdin `` is used instead of a file. To support this add "``[-] ``"
304367 to you usage patterns. "``- ``" act as a normal command.
@@ -308,7 +371,7 @@ times::
308371
309372 Usage: my_program.php [-v | -vv | -vvv]
310373
311- then number of occurences of the option will be counted. I.e.
374+ then number of occurrences of the option will be counted. I.e.
312375``args['-v'] `` will be ``2 `` if program was invoked as ``my_program
313376-vv ``. Same works for commands.
314377
@@ -323,11 +386,11 @@ I.e. invoked with ``my_program.php file1 file2 --path=./here
323386['file1', 'file2'] `` and ``args['--path'] == ['./here', './there'] ``.
324387
325388
326- Option descriptions format
389+ Options section format
327390----------------------------------------------------------------------
328391
329- **Option descriptions ** consist of a list of options that you put
330- below your usage patterns .
392+ The **Option section ** is an optional section that contains a list of
393+ options that can document or supplement your usage pattern .
331394
332395It is necessary to list option descriptions in order to specify:
333396
@@ -337,8 +400,9 @@ It is necessary to list option descriptions in order to specify:
337400
338401The rules are as follows:
339402
340- - Every line in ``doc `` that starts with ``- `` or ``-- `` (not counting
341- spaces) is treated as an option description, e.g.::
403+ - Every line in the options section body that starts with one or more
404+ horizontal whitespace characters, followed by ``- `` or ``-- `` is treated
405+ as an option description, e.g.::
342406
343407 Options:
344408 --verbose # GOOD
@@ -353,7 +417,7 @@ The rules are as follows:
353417 to stick to a single style.::
354418
355419 -o FILE --output=FILE # without comma, with "=" sign
356- -i <file>, --input <file> # with comma, wihtout "=" sing
420+ -i <file>, --input <file> # with comma, wihtout "=" sign
357421
358422- Use two spaces to separate options with their informal description::
359423
@@ -372,7 +436,7 @@ The rules are as follows:
372436 --directory=DIR Some directory [default: ./]
373437
374438- If the option is not repeatable, the value inside ``[default: ...] ``
375- will be interpeted as string. If it *is * repeatable, it will be
439+ will be interpreted as string. If it *is * repeatable, it will be
376440 splited into a list on whitespace::
377441
378442 Usage: my_program.php [--repeatable=<arg> --repeatable=<arg>]
@@ -388,6 +452,7 @@ The rules are as follows:
388452 # will be './here ./there', because it is not repeatable
389453 --not-repeatable=<arg> [default: ./here ./there]
390454
455+
391456Examples
392457----------------------------------------------------------------------
393458
@@ -396,11 +461,12 @@ We have an extensive list of `examples
396461every aspect of functionality of **docopt **. Try them out, read the
397462source if in doubt.
398463
464+
399465Subparsers, multi-level help and *huge * applications (like git)
400466----------------------------------------------------------------------
401467
402468If you want to split your usage-pattern into several, implement
403- multi-level help (whith separate help-screen for each subcommand),
469+ multi-level help (with separate help-screen for each subcommand),
404470want to interface with existing scripts that don't use **docopt **, or
405471you're building the next "git", you will need the new ``options_first ``
406472parameter (described in API section above). To get you started quickly
0 commit comments