102
102
103
103
docopt::docopt(doc, argv, help /* =true */, version / * ="" */, options_first / * =false */)
104
104
105
- ``docopt `` takes 1 required and 4 optional arguments:
105
+ ``docopt `` takes 2 required and 3 optional arguments:
106
106
107
107
- ``doc `` is a string that contains a **help message ** that will be parsed to
108
108
create the option parser. The simple rules of how to write such a
112
112
113
113
.. code :: c++
114
114
115
- R"(Usage: my_program.py [-hso FILE] [--quiet | --verbose] [INPUT ...]
115
+ R"(Usage: my_program [-hso FILE] [--quiet | --verbose] [INPUT ...]
116
116
117
117
-h --help show this
118
118
-s --sorted sorted output
@@ -173,14 +173,26 @@ the return dictionary will be:
173
173
" <x>" : " 100" , " shoot" : false,
174
174
" <y>" : " 150" }
175
175
176
+ If any parsing error (in either the usage, or due to incorrect user inputs) is
177
+ encountered, the program will exit with exit code -1.
178
+
179
+ Note that there is another function that does not exit on error, and instead will
180
+ propogate an exception that you can catch and process as you like. See the docopt.h file
181
+ for information on the exceptions and usage:
182
+
183
+ .. code :: c++
184
+
185
+ docopt::docopt_parse(doc, argv, help /* =true */, version / * =true */, options_first / * =false)
186
+
187
+
176
188
Help message format
177
189
---------------------------------------------------
178
190
179
191
Help message consists of 2 parts:
180
192
181
193
- Usage pattern, e.g.::
182
194
183
- Usage: my_program.py [-hso FILE] [--quiet | --verbose] [INPUT ...]
195
+ Usage: my_program [-hso FILE] [--quiet | --verbose] [INPUT ...]
184
196
185
197
- Option descriptions, e.g.::
186
198
@@ -219,8 +231,8 @@ exclusive patterns:
219
231
Each pattern can consist of the following elements:
220
232
221
233
- **<arguments> **, **ARGUMENTS **. Arguments are specified as either
222
- upper-case words, e.g. ``my_program.py CONTENT-PATH `` or words
223
- surrounded by angular brackets: ``my_program.py <content-path> ``.
234
+ upper-case words, e.g. ``my_program CONTENT-PATH `` or words
235
+ surrounded by angular brackets: ``my_program <content-path> ``.
224
236
- **--options **. Options are words started with dash (``- ``), e.g.
225
237
``--output ``, ``-o ``. You can "stack" several of one-letter
226
238
options, e.g. ``-oiv `` which will be the same as ``-o -i -v ``. The
@@ -236,23 +248,23 @@ Each pattern can consist of the following elements:
236
248
237
249
Use the following constructs to specify patterns:
238
250
239
- - **[ ] ** (brackets) **optional ** elements. e.g.: ``my_program.py
251
+ - **[ ] ** (brackets) **optional ** elements. e.g.: ``my_program
240
252
[-hvqo FILE] ``
241
253
- **( ) ** (parens) **required ** elements. All elements that are *not *
242
- put in **[ ] ** are also required, e.g.: ``my_program.py
243
- --path=<path> <file>... `` is the same as ``my_program.py
254
+ put in **[ ] ** are also required, e.g.: ``my_program
255
+ --path=<path> <file>... `` is the same as ``my_program
244
256
(--path=<path> <file>...) ``. (Note, "required options" might be not
245
257
a good idea for your users).
246
258
- **| ** (pipe) **mutually exclusive ** elements. Group them using **(
247
259
) ** if one of the mutually exclusive elements is required:
248
- ``my_program.py (--clockwise | --counter-clockwise) TIME ``. Group
260
+ ``my_program (--clockwise | --counter-clockwise) TIME ``. Group
249
261
them using **[ ] ** if none of the mutually-exclusive elements are
250
- required: ``my_program.py [--left | --right] ``.
262
+ required: ``my_program [--left | --right] ``.
251
263
- **... ** (ellipsis) **one or more ** elements. To specify that
252
264
arbitrary number of repeating elements could be accepted, use
253
- ellipsis (``... ``), e.g. ``my_program.py FILE ... `` means one or
265
+ ellipsis (``... ``), e.g. ``my_program FILE ... `` means one or
254
266
more ``FILE ``-s are accepted. If you want to accept zero or more
255
- elements, use brackets, e.g.: ``my_program.py [FILE ...] ``. Ellipsis
267
+ elements, use brackets, e.g.: ``my_program [FILE ...] ``. Ellipsis
256
268
works as a unary operator on the expression to the left.
257
269
- **[options] ** (case sensitive) shortcut for any options. You can
258
270
use it if you want to specify that the usage pattern could be
@@ -268,7 +280,7 @@ Use the following constructs to specify patterns:
268
280
If your pattern allows to match argument-less option (a flag) several
269
281
times::
270
282
271
- Usage: my_program.py [-v | -vv | -vvv]
283
+ Usage: my_program [-v | -vv | -vvv]
272
284
273
285
then number of occurrences of the option will be counted. I.e.
274
286
``args['-v'] `` will be ``2 `` if program was invoked as ``my_program
@@ -278,9 +290,9 @@ If your usage patterns allows to match same-named option with argument
278
290
or positional argument several times, the matched arguments will be
279
291
collected into a list::
280
292
281
- Usage: my_program.py <file> <file> --path=<path>...
293
+ Usage: my_program <file> <file> --path=<path>...
282
294
283
- I.e. invoked with ``my_program.py file1 file2 --path=./here
295
+ I.e. invoked with ``my_program file1 file2 --path=./here
284
296
--path=./there `` the returned dict will contain ``args['<file>'] ==
285
297
['file1', 'file2'] `` and ``args['--path'] == ['./here', './there'] ``.
286
298
@@ -337,7 +349,7 @@ The rules are as follows:
337
349
will be interpreted as string. If it *is * repeatable, it will be
338
350
splited into a list on whitespace::
339
351
340
- Usage: my_program.py [--repeatable=<arg> --repeatable=<arg>]
352
+ Usage: my_program [--repeatable=<arg> --repeatable=<arg>]
341
353
[--another-repea
6D4E
table=<arg>]...
342
354
[--not-repeatable=<arg>]
343
355
@@ -378,20 +390,34 @@ Compiling the example / Running the tests
378
390
The original Python module includes some language-agnostic unit tests,
379
391
and these can be run with this port as well.
380
392
381
- For example, with the clang compiler on OSX::
393
+ The tests are a Python driver that uses the testcases.docopt file to then invoke
394
+ a C++ test case runner (run_testcase.cpp)::
382
395
383
396
$ clang++ --std=c++11 --stdlib=libc++ docopt.cpp run_testcase.cpp -o run_testcase
384
397
$ python run_tests.py
385
398
PASS (175)
386
399
387
- You can also compile the example show at the start (also included as
388
- example.cpp)::
400
+ You can also compile the example shown at the start (included as example.cpp)::
389
401
390
- $ clang++ clang++ --std=c++11 --stdlib=libc++ -I . docopt.cpp examples/naval_fate.cpp -o naval_fate
402
+ $ clang++ --std=c++11 --stdlib=libc++ -I . docopt.cpp examples/naval_fate.cpp -o naval_fate
391
403
$ ./naval_fate --help
392
404
[ ... ]
393
405
$ ./naval_fate ship Guardian move 100 150 --speed=15
394
- [ ... ]
406
+ --drifting: false
407
+ --help: false
408
+ --moored: false
409
+ --speed: "15"
410
+ --version: false
411
+ <name>: ["Guardian"]
412
+ <x>: "100"
413
+ <y>: "150"
414
+ mine: false
415
+ move: true
416
+ new: false
417
+ remove: false
418
+ set: false
419
+ ship: true
420
+ shoot: false
395
421
396
422
Development
397
423
---------------------------------------------------
0 commit comments