1
1
``docopt.cpp ``: A C++11 Port
2
2
============================
3
+
4
+ Contents
5
+ --------
6
+
7
+ .. contents ::
8
+ :local:
9
+ :depth: 3
10
+
3
11
docopt creates *beautiful * command-line interfaces
4
12
--------------------------------------------------
5
13
@@ -58,13 +66,40 @@ and instead can write only the help message--*the way you want it*.
58
66
Beat that! The option parser is generated based on the docstring above
59
67
that is passed to ``docopt::docopt `` function. ``docopt `` parses the usage
60
68
pattern (``"Usage: ..." ``) and option descriptions (lines starting
61
- with dash "``- ``") and ensures that the program invocation matches the
69
+ with a dash "``- ``") and ensures that the program invocation matches the
62
70
usage pattern; it parses options, arguments and commands based on
63
71
that. The basic idea is that *a good help message has all necessary
64
72
information in it to make a parser *.
65
73
74
+ Getting and using
75
+ -----------------
76
+
77
+ To get *docopt.cpp *, the simplest is to use `Conda <https://github.com/conda-forge/docopt.cpp-feedstock >`_::
78
+
79
+ conda install -c conda-forge docopt.cpp
80
+
81
+ Alternatively manual installation is done using (unix)::
82
+
83
+ git clone
84
+ cmake .
85
+ make install
86
+
87
+ To link *docopt.cpp *, the simplest is to use CMake. The general structure of your
88
+ ``CMakeLists.txt `` would be as follows::
89
+
90
+ cmake_minimum_required(VERSION 3.1)
91
+
92
+ project(example)
93
+
94
+ find_package(docopt COMPONENTS CXX REQUIRED)
95
+ include_directories(${DOCOPT_INCLUDE_DIRS})
96
+
97
+ add_executable(${PROJECT_NAME} ...)
98
+
99
+ target_link_libraries(${PROJECT_NAME} docopt)
100
+
66
101
C++11 port details
67
- ---------------------------------------------------
102
+ ------------------
68
103
69
104
This is a port of the ``docopt.py `` module (https://github.com/docopt/docopt),
70
105
and we have tried to maintain full feature parity (and code structure) as the
@@ -80,7 +115,7 @@ to work with docopt:
80
115
81
116
GCC-4.8 can work, but the std::regex module needs to be replaced with ``Boost.Regex ``.
82
117
In that case, you will need to define ``DOCTOPT_USE_BOOST_REGEX `` when compiling
83
- docopt, and link your code with the appropriated Boost libraries. A relativley
118
+ docopt, and link your code with the appropriated Boost libraries. A relatively
84
119
recent version of Boost is needed: 1.55 works, but 1.46 does not for example.
85
120
86
121
This port is licensed under the MIT license, just like the original module.
@@ -101,7 +136,7 @@ The differences from the Python port are:
101
136
some of the regex's had to be restructured and additional loops used.
102
137
103
138
API
104
- ---------------------------------------------------
139
+ ---
105
140
106
141
.. code :: c++
107
142
@@ -182,16 +217,15 @@ If any parsing error (in either the usage, or due to incorrect user inputs) is
182
217
encountered, the program will exit with exit code -1.
183
218
184
219
Note that there is another function that does not exit on error, and instead will
185
- propogate an exception that you can catch and process as you like. See the docopt.h file
220
+ propagate an exception that you can catch and process as you like. See the docopt.h file
186
221
for information on the exceptions and usage:
187
222
188
223
.. code :: c++
189
224
190
225
docopt::docopt_parse(doc, argv, help /* =true */, version / * =true */, options_first / * =false)
191
226
192
-
193
227
Help message format
194
- ---------------------------------------------------
228
+ -------------------
195
229
196
230
Help message consists of 2 parts:
197
231
@@ -210,7 +244,7 @@ Help message consists of 2 parts:
210
244
Their format is described below; other text is ignored.
211
245
212
246
Usage pattern format
213
- ----------------------------------------------------------------------
247
+ --------------------
214
248
215
249
**Usage pattern ** is a substring of ``doc `` that starts with
216
250
``usage: `` (case *insensitive *) and ends with a *visibly * empty line.
@@ -263,7 +297,7 @@ Use the following constructs to specify patterns:
263
297
- **| ** (pipe) **mutually exclusive ** elements. Group them using **(
264
298
) ** if one of the mutually exclusive elements is required:
265
299
``my_program (--clockwise | --counter-clockwise) TIME ``. Group
266
- them using **[ ] ** if none of the mutually- exclusive elements are
300
+ them using **[ ] ** if none of the mutually exclusive elements are
267
301
required: ``my_program [--left | --right] ``.
268
302
- **... ** (ellipsis) **one or more ** elements. To specify that
269
303
arbitrary number of repeating elements could be accepted, use
@@ -291,7 +325,7 @@ then number of occurrences of the option will be counted. I.e.
291
325
``args['-v'] `` will be ``2 `` if program was invoked as ``my_program
292
326
-vv ``. Same works for commands.
293
327
294
- If your usage patterns allows to match same-named option with argument
328
+ If your usage pattern allows to match same-named option with argument
295
329
or positional argument several times, the matched arguments will be
296
330
collected into a list::
297
331
@@ -301,9 +335,8 @@ I.e. invoked with ``my_program file1 file2 --path=./here
301
335
--path=./there `` the returned dict will contain ``args['<file>'] ==
302
336
['file1', 'file2'] `` and ``args['--path'] == ['./here', './there'] ``.
303
337
304
-
305
338
Option descriptions format
306
- ----------------------------------------------------------------------
339
+ --------------------------
307
340
308
341
**Option descriptions ** consist of a list of options that you put
309
342
below your usage patterns.
@@ -328,7 +361,7 @@ The rules are as follows:
328
361
argument after space (or equals "``= ``" sign) as shown below. Follow
329
362
either <angular-brackets> or UPPER-CASE convention for options'
330
363
arguments. You can use comma if you want to separate options. In
331
- the example below, both lines are valid, however you are recommended
364
+ the example below, both lines are valid. However you are recommended
332
365
to stick to a single style.::
333
366
334
367
-o FILE --output=FILE # without comma, with "=" sign
@@ -352,7 +385,7 @@ The rules are as follows:
352
385
353
386
- If the option is not repeatable, the value inside ``[default: ...] ``
354
387
will be interpreted as string. If it *is * repeatable, it will be
355
- splited into a list on whitespace::
388
+ split into a list on whitespace::
356
389
357
390
Usage: my_program [--repeatable=<arg> --repeatable=<arg>]
358
391
[--another-repeatable=<arg>]...
@@ -368,18 +401,18 @@ The rules are as follows:
368
401
--not-repeatable=<arg> [default: ./here ./there]
369
402
370
403
Examples
371
- ----------------------------------------------------------------------
404
+ --------
372
405
373
406
We have an extensive list of `examples
374
407
<https://github.com/docopt/docopt/tree/master/examples> `_ which cover
375
408
every aspect of functionality of **docopt **. Try them out, read the
376
409
source if in doubt.
377
410
378
- There are also very intersting applications and ideas at that page.
411
+ There are also very interesting applications and ideas at that page.
379
412
Check out the sister project for more information!
380
413
381
414
Subparsers, multi-level help and *huge * applications (like git)
382
- ----------------------------------------------------------------------
415
+ ---------------------------------------------------------------
383
416
384
417
If you want to split your usage-pattern into several, implement
385
418
multi-level help (with separate help-screen for each subcommand),
@@ -391,7 +424,8 @@ we implemented a subset of git command-line interface as an example:
391
424
<https://github.com/docopt/docopt/tree/master/examples/git> `_
392
425
393
426
Compiling the example / Running the tests
394
- ----------------------------------------------------------------------
427
+ -----------------------------------------
428
+
395
429
The original Python module includes some language-agnostic unit tests,
396
430
and these can be run with this port as well.
397
431
@@ -425,7 +459,7 @@ You can also compile the example shown at the start (included as example.cpp)::
425
459
shoot: false
426
460
427
461
Development
428
- ---------------------------------------------------
462
+ -----------
429
463
430
464
Comments and suggestions are *very * welcome! If you find issues, please
431
465
file them and help improve our code!
@@ -436,7 +470,7 @@ we might want to first negotiate these changes into the Python code first.
436
470
However, bring it up! Let's hear it!
437
471
438
472
Changelog
439
- ---------------------------------------------------
473
+ ---------
440
474
441
475
**docopt ** follows `semantic versioning <http://semver.org >`_. The
442
476
first release with stable API will be 1.0.0 (soon).
0 commit comments